diff options
author | Michael Koziarski <michael@koziarski.com> | 2007-10-29 22:46:22 +0000 |
---|---|---|
committer | Michael Koziarski <michael@koziarski.com> | 2007-10-29 22:46:22 +0000 |
commit | e86d1decc185679ec6303f0ea5c25fdc30b57a71 (patch) | |
tree | aa339af8a7cd40f49c9b978dd47a9eb7d0567b99 /activesupport/lib | |
parent | 3ba23348de4c7765b67e3715d2ab84c998bb1cea (diff) | |
download | rails-e86d1decc185679ec6303f0ea5c25fdc30b57a71.tar.gz rails-e86d1decc185679ec6303f0ea5c25fdc30b57a71.tar.bz2 rails-e86d1decc185679ec6303f0ea5c25fdc30b57a71.zip |
Add new superclass_delegating_accessors. Similar to class inheritable attributes but with subtly different semantics. [Koz, tarmo]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8056 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/active_support/core_ext/class.rb | 1 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/class/delegating_attributes.rb | 40 |
2 files changed, 41 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/core_ext/class.rb b/activesupport/lib/active_support/core_ext/class.rb index 83eb8135f4..44ad6c8c08 100644 --- a/activesupport/lib/active_support/core_ext/class.rb +++ b/activesupport/lib/active_support/core_ext/class.rb @@ -1,3 +1,4 @@ require 'active_support/core_ext/class/attribute_accessors' require 'active_support/core_ext/class/inheritable_attributes' require 'active_support/core_ext/class/removal' +require 'active_support/core_ext/class/delegating_attributes' diff --git a/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb b/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb new file mode 100644 index 0000000000..f5f0ef8779 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb @@ -0,0 +1,40 @@ +# These class attributes behave something like the class +# inheritable accessors. But instead of copying the hash over at +# the time the subclass is first defined, the accessors simply +# delegate to their superclass unless they have been given a +# specific value. This stops the strange situation where values +# set after class definition don't get applied to subclasses. +class Class + def superclass_delegating_reader(*names) + class_name_to_stop_searching_on = self.superclass.name.blank? ? "Object" : self.superclass.name + names.each do |name| + class_eval <<-EOS + def self.#{name} + if defined?(@#{name}) + @#{name} + elsif superclass < #{class_name_to_stop_searching_on} && superclass.respond_to?(:#{name}) + superclass.#{name} + end + end + def #{name} + self.class.#{name} + end + EOS + end + end + + def superclass_delegating_writer(*names) + names.each do |name| + class_eval <<-EOS + def self.#{name}=(value) + @#{name} = value + end + EOS + end + end + + def superclass_delegating_accessor(*names) + superclass_delegating_reader(*names) + superclass_delegating_writer(*names) + end +end
\ No newline at end of file |