diff options
Diffstat (limited to 'activesupport/lib/active_support/core_ext/class')
-rw-r--r-- | activesupport/lib/active_support/core_ext/class/delegating_attributes.rb | 60 |
1 files changed, 39 insertions, 21 deletions
diff --git a/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb b/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb index 6c67df7f50..301c09fc73 100644 --- a/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb +++ b/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb @@ -1,39 +1,57 @@ require 'active_support/core_ext/object/blank' +require 'active_support/core_ext/object/duplicable' +require 'active_support/core_ext/array/extract_options' class Class def superclass_delegating_reader(*names) - class_name_to_stop_searching_on = superclass.name.blank? ? "Object" : superclass.name + class_to_stop_searching_on = superclass.name.blank? ? "Object" : superclass.name + options = names.extract_options! + names.each do |name| - class_eval(<<-EOS, __FILE__, __LINE__ + 1) - def self.#{name} # def self.only_reader - if defined?(@#{name}) # if defined?(@only_reader) - @#{name} # @only_reader - elsif superclass < #{class_name_to_stop_searching_on} && # elsif superclass < Object && - superclass.respond_to?(:#{name}) # superclass.respond_to?(:only_reader) - superclass.#{name} # superclass.only_reader - end # end - end # end - def #{name} # def only_reader - self.class.#{name} # self.class.only_reader - end # end - def self.#{name}? # def self.only_reader? - !!#{name} # !!only_reader - end # end - def #{name}? # def only_reader? - !!#{name} # !!only_reader - end # end + # def self.only_reader + # if defined?(@only_reader) + # @only_reader + # elsif superclass < Object && superclass.respond_to?(:only_reader) + # superclass.only_reader + # end + # end + class_eval <<-EOS, __FILE__, __LINE__ + 1 + def self.#{name} + if defined?(@#{name}) + @#{name} + elsif superclass < #{class_to_stop_searching_on} && superclass.respond_to?(:#{name}) + superclass.#{name} + end + end EOS + + unless options[:instance_reader] == false + class_eval <<-EOS, __FILE__, __LINE__ + 1 + def #{name} # def only_reader + self.class.#{name} # self.class.only_reader + end # end + def self.#{name}? # def self.only_reader? + !!#{name} # !!only_reader + end # end + def #{name}? # def only_reader? + !!#{name} # !!only_reader + end # end + EOS + end end end def superclass_delegating_writer(*names, &block) + options = names.extract_options! + names.each do |name| - class_eval(<<-EOS, __FILE__, __LINE__ + 1) + class_eval <<-EOS, __FILE__, __LINE__ + 1 def self.#{name}=(value) # def self.property=(value) @#{name} = value # @property = value end # end EOS - self.send("#{name}=", yield) if block_given? + + self.send(:"#{name}=", yield) if block_given? end end |