From de4891344ccc074f6d5693f4fac6ad610584e336 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 18 Aug 2014 00:22:34 -0300 Subject: Improve custom configuration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Hashes can be assigned 2. We don't need a special level anymore The method chain only works in the top level. If users need a second level they need to assign a OrderedOptions to the key: config.resque.server = ActiveSupport::OrderedOptions.new config.resque.server.url = "http://localhost" config.resque.server.port = 3000 [Rafael Mendonça França + Carlos Antonio da Silva] --- railties/lib/rails/application/configuration.rb | 14 +-------- railties/lib/rails/railtie/configuration.rb | 42 +++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 15 deletions(-) (limited to 'railties/lib/rails') diff --git a/railties/lib/rails/application/configuration.rb b/railties/lib/rails/application/configuration.rb index 782bc4b0f1..5e8f4de847 100644 --- a/railties/lib/rails/application/configuration.rb +++ b/railties/lib/rails/application/configuration.rb @@ -13,7 +13,7 @@ module Rails :railties_order, :relative_url_root, :secret_key_base, :secret_token, :serve_static_assets, :ssl_options, :static_cache_control, :session_options, :time_zone, :reload_classes_only_on_change, - :beginning_of_week, :filter_redirect, :x + :beginning_of_week, :filter_redirect attr_writer :log_level attr_reader :encoding @@ -48,7 +48,6 @@ module Rails @eager_load = nil @secret_token = nil @secret_key_base = nil - @x = Custom.new @assets = ActiveSupport::OrderedOptions.new @assets.enabled = true @@ -155,17 +154,6 @@ module Rails def annotations SourceAnnotationExtractor::Annotation end - - private - class Custom - def initialize - @configurations = Hash.new - end - - def method_missing(method, *args) - @configurations[method] ||= ActiveSupport::OrderedOptions.new - end - end end end end diff --git a/railties/lib/rails/railtie/configuration.rb b/railties/lib/rails/railtie/configuration.rb index eb3b2d8ef4..406ed5ac61 100644 --- a/railties/lib/rails/railtie/configuration.rb +++ b/railties/lib/rails/railtie/configuration.rb @@ -88,11 +88,49 @@ module Rails def method_missing(name, *args, &blk) if name.to_s =~ /=$/ - @@options[$`.to_sym] = args.first + key = $`.to_sym + value = args.first + + if value.is_a?(Hash) + @@options[key] = ChainedConfigurationOptions.new value + else + @@options[key] = value + end elsif @@options.key?(name) @@options[name] else - super + @@options[name] = ActiveSupport::OrderedOptions.new + end + end + + class ChainedConfigurationOptions < ActiveSupport::OrderedOptions # :nodoc: + def initialize(value = nil) + if value.is_a?(Hash) + value.each_pair { |k, v| set_value k, v } + else + super + end + end + + def method_missing(meth, *args) + if meth =~ /=$/ + key = $`.to_sym + value = args.first + + set_value key, value + else + self.fetch(meth) { super } + end + end + + private + + def set_value(key, value) + if value.is_a?(Hash) + value = self.class.new(value) + end + + self[key] = value end end end -- cgit v1.2.3