aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/rails/railtie
diff options
context:
space:
mode:
authorRafael Mendonça França <rafael.franca@plataformatec.com.br>2014-08-18 00:22:34 -0300
committerRafael Mendonça França <rafaelmfranca@gmail.com>2014-08-19 13:59:22 -0300
commitde4891344ccc074f6d5693f4fac6ad610584e336 (patch)
tree9d62508514bb9909e0766cba3020292c2d27bb17 /railties/lib/rails/railtie
parent0cb3bdb66e87981c1094edecfce9182ae8aeb240 (diff)
downloadrails-de4891344ccc074f6d5693f4fac6ad610584e336.tar.gz
rails-de4891344ccc074f6d5693f4fac6ad610584e336.tar.bz2
rails-de4891344ccc074f6d5693f4fac6ad610584e336.zip
Improve custom configuration
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]
Diffstat (limited to 'railties/lib/rails/railtie')
-rw-r--r--railties/lib/rails/railtie/configuration.rb42
1 files changed, 40 insertions, 2 deletions
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