From 2e37effd7203cad84459661e11db2be44586cb4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 17 Oct 2009 12:54:03 -0300 Subject: Unify class_inheritable_accessor and extlib_inheritable_accessor and allow responder to be set in the class level. --- .../core_ext/class/inheritable_attributes.rb | 28 ++++++++++++---------- .../active_support/core_ext/object/duplicable.rb | 8 ++++++- 2 files changed, 23 insertions(+), 13 deletions(-) (limited to 'activesupport/lib/active_support/core_ext') diff --git a/activesupport/lib/active_support/core_ext/class/inheritable_attributes.rb b/activesupport/lib/active_support/core_ext/class/inheritable_attributes.rb index 8bac2dff19..d8e9768a5e 100644 --- a/activesupport/lib/active_support/core_ext/class/inheritable_attributes.rb +++ b/activesupport/lib/active_support/core_ext/class/inheritable_attributes.rb @@ -10,16 +10,19 @@ end # children, which is unlike the regular class-level attributes that are shared across the entire hierarchy. class Class # :nodoc: def class_inheritable_reader(*syms) + options = syms.extract_options! syms.each do |sym| next if sym.is_a?(Hash) class_eval(<<-EOS, __FILE__, __LINE__ + 1) - def self.#{sym} # def self.after_add - read_inheritable_attribute(:#{sym}) # read_inheritable_attribute(:after_add) - end # end - - def #{sym} # def after_add - self.class.#{sym} # self.class.after_add - end # end + def self.#{sym} # def self.after_add + read_inheritable_attribute(:#{sym}) # read_inheritable_attribute(:after_add) + end # end + # + #{" # + def #{sym} # def after_add + self.class.#{sym} # self.class.after_add + end # end + " unless options[:instance_reader] == false } # # the reader above is generated unless options[:instance_reader] == false EOS end end @@ -156,7 +159,7 @@ class Class # moving on). In particular, this makes the return value of this function # less useful. def extlib_inheritable_reader(*ivars) - instance_reader = ivars.pop[:reader] if ivars.last.is_a?(Hash) + options = ivars.extract_options! ivars.each do |ivar| self.class_eval <<-RUBY, __FILE__, __LINE__ + 1 @@ -164,10 +167,10 @@ class Class return @#{ivar} if self.object_id == #{self.object_id} || defined?(@#{ivar}) ivar = superclass.#{ivar} return nil if ivar.nil? && !#{self}.instance_variable_defined?("@#{ivar}") - @#{ivar} = ivar && !ivar.is_a?(Module) && !ivar.is_a?(Numeric) && !ivar.is_a?(TrueClass) && !ivar.is_a?(FalseClass) ? ivar.dup : ivar + @#{ivar} = ivar.duplicable? ? ivar.dup : ivar end RUBY - unless instance_reader == false + unless options[:instance_reader] == false self.class_eval <<-RUBY, __FILE__, __LINE__ + 1 def #{ivar} self.class.#{ivar} @@ -190,14 +193,15 @@ class Class # @todo We need a style for class_eval <<-HEREDOC. I'd like to make it # class_eval(<<-RUBY, __FILE__, __LINE__), but we should codify it somewhere. def extlib_inheritable_writer(*ivars) - instance_writer = ivars.pop[:writer] if ivars.last.is_a?(Hash) + options = ivars.extract_options! + ivars.each do |ivar| self.class_eval <<-RUBY, __FILE__, __LINE__ + 1 def self.#{ivar}=(obj) @#{ivar} = obj end RUBY - unless instance_writer == false + unless options[:instance_writer] == false self.class_eval <<-RUBY, __FILE__, __LINE__ + 1 def #{ivar}=(obj) self.class.#{ivar} = obj end RUBY diff --git a/activesupport/lib/active_support/core_ext/object/duplicable.rb b/activesupport/lib/active_support/core_ext/object/duplicable.rb index 1722726ca2..a2d4d50076 100644 --- a/activesupport/lib/active_support/core_ext/object/duplicable.rb +++ b/activesupport/lib/active_support/core_ext/object/duplicable.rb @@ -1,6 +1,6 @@ class Object # Can you safely .dup this object? - # False for nil, false, true, symbols, numbers, and class objects; true otherwise. + # False for nil, false, true, symbols, numbers, class and module objects; true otherwise. def duplicable? true end @@ -41,3 +41,9 @@ class Class #:nodoc: false end end + +class Module #:nodoc: + def duplicable? + false + end +end -- cgit v1.2.3