aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2010-04-22 12:00:13 +0200
committerJosé Valim <jose.valim@gmail.com>2010-04-22 12:00:13 +0200
commit4163ccec2343ee66e2488f067eab2a15260e1219 (patch)
tree70213bd4d187c88b2fd0bbf05a54eed7ec3a49f7 /activesupport
parenta8330c2006e90a6da8f621fdaf1156fa63b4049a (diff)
downloadrails-4163ccec2343ee66e2488f067eab2a15260e1219.tar.gz
rails-4163ccec2343ee66e2488f067eab2a15260e1219.tar.bz2
rails-4163ccec2343ee66e2488f067eab2a15260e1219.zip
Clean up the config object in ActionPack. Create config_accessor which just delegates to the config object, reducing the number of deprecations and add specific tests.
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/configurable.rb37
-rw-r--r--activesupport/test/configurable_test.rb42
2 files changed, 61 insertions, 18 deletions
diff --git a/activesupport/lib/active_support/configurable.rb b/activesupport/lib/active_support/configurable.rb
index 890f465ce1..f562e17c75 100644
--- a/activesupport/lib/active_support/configurable.rb
+++ b/activesupport/lib/active_support/configurable.rb
@@ -1,35 +1,36 @@
-require "active_support/concern"
+require 'active_support/concern'
+require 'active_support/ordered_options'
+require 'active_support/core_ext/kernel/singleton_class'
+require 'active_support/core_ext/module/delegation'
module ActiveSupport
module Configurable
extend ActiveSupport::Concern
module ClassMethods
- def get_config
- module_parts = name.split("::")
- modules = [Object]
- module_parts.each {|name| modules.push modules.last.const_get(name) }
- modules.reverse_each do |mod|
- return mod.const_get(:DEFAULT_CONFIG) if const_defined?(:DEFAULT_CONFIG)
- end
- {}
- end
-
def config
- self.config = get_config unless @config
- @config
+ @config ||= ActiveSupport::InheritableOptions.new(superclass.respond_to?(:config) ? superclass.config : {})
end
- def config=(hash)
- @config = ActiveSupport::OrderedOptions.new
- hash.each do |key, value|
- @config[key] = value
+ def configure
+ yield config
+ end
+
+ def config_accessor(*names)
+ names.each do |name|
+ code, line = <<-RUBY, __LINE__ + 1
+ def #{name}; config.#{name}; end
+ def #{name}=(value); config.#{name} = value; end
+ RUBY
+
+ singleton_class.class_eval code, __FILE__, line
+ class_eval code, __FILE__, line
end
end
end
def config
- self.class.config
+ @config ||= ActiveSupport::InheritableOptions.new(self.class.config)
end
end
end \ No newline at end of file
diff --git a/activesupport/test/configurable_test.rb b/activesupport/test/configurable_test.rb
new file mode 100644
index 0000000000..cef67e3cf9
--- /dev/null
+++ b/activesupport/test/configurable_test.rb
@@ -0,0 +1,42 @@
+require 'abstract_unit'
+require 'active_support/configurable'
+
+class ConfigurableActiveSupport < ActiveSupport::TestCase
+ class Parent
+ include ActiveSupport::Configurable
+ config_accessor :foo
+ end
+
+ class Child < Parent
+ end
+
+ setup do
+ Parent.config.clear
+ Parent.config.foo = :bar
+
+ Child.config.clear
+ end
+
+ test "adds a configuration hash" do
+ assert_equal({ :foo => :bar }, Parent.config)
+ end
+
+ test "configuration hash is inheritable" do
+ assert_equal :bar, Child.config.foo
+ assert_equal :bar, Parent.config.foo
+
+ Child.config.foo = :baz
+ assert_equal :baz, Child.config.foo
+ assert_equal :bar, Parent.config.foo
+ end
+
+ test "configuration hash is available on instance" do
+ instance = Parent.new
+ assert_equal :bar, instance.config.foo
+ assert_equal :bar, Parent.config.foo
+
+ instance.config.foo = :baz
+ assert_equal :baz, instance.config.foo
+ assert_equal :bar, Parent.config.foo
+ end
+end \ No newline at end of file