aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorYehuda Katz <wycats@gmail.com>2009-02-24 17:25:21 -0800
committerYehuda Katz <wycats@gmail.com>2009-02-24 17:25:21 -0800
commitb1f078bddfecd40cce47b7db738620f2df2219c9 (patch)
treebe99345865c71cc37e920c87218972a33a24cfe7 /activesupport
parentd6b9f8410c990b3d68d1970f1461a1d385d098d7 (diff)
downloadrails-b1f078bddfecd40cce47b7db738620f2df2219c9.tar.gz
rails-b1f078bddfecd40cce47b7db738620f2df2219c9.tar.bz2
rails-b1f078bddfecd40cce47b7db738620f2df2219c9.zip
First, very early, AbstractController code. More to come
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/core_ext/class/inheritable_attributes.rb79
1 files changed, 79 insertions, 0 deletions
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 70fdde3a58..c121933050 100644
--- a/activesupport/lib/active_support/core_ext/class/inheritable_attributes.rb
+++ b/activesupport/lib/active_support/core_ext/class/inheritable_attributes.rb
@@ -138,3 +138,82 @@ class Class # :nodoc:
alias inherited_without_inheritable_attributes inherited
alias inherited inherited_with_inheritable_attributes
end
+
+class Class
+ # Defines class-level inheritable attribute reader. Attributes are available to subclasses,
+ # each subclass has a copy of parent's attribute.
+ #
+ # @param *syms<Array[#to_s]> Array of attributes to define inheritable reader for.
+ # @return <Array[#to_s]> Array of attributes converted into inheritable_readers.
+ #
+ # @api public
+ #
+ # @todo Do we want to block instance_reader via :instance_reader => false
+ # @todo It would be preferable that we do something with a Hash passed in
+ # (error out or do the same as other methods above) instead of silently
+ # 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)
+
+ ivars.each do |ivar|
+ self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
+ def self.#{ivar}
+ 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
+ end
+ RUBY
+ unless instance_reader == false
+ self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
+ def #{ivar}
+ self.class.#{ivar}
+ end
+ RUBY
+ end
+ end
+ end
+
+ # Defines class-level inheritable attribute writer. Attributes are available to subclasses,
+ # each subclass has a copy of parent's attribute.
+ #
+ # @param *syms<Array[*#to_s, Hash{:instance_writer => Boolean}]> Array of attributes to
+ # define inheritable writer for.
+ # @option syms :instance_writer<Boolean> if true, instance-level inheritable attribute writer is defined.
+ # @return <Array[#to_s]> An Array of the attributes that were made into inheritable writers.
+ #
+ # @api public
+ #
+ # @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[:instance_writer] if ivars.last.is_a?(Hash)
+ ivars.each do |ivar|
+ self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
+ def self.#{ivar}=(obj)
+ @#{ivar} = obj
+ end
+ RUBY
+ unless instance_writer == false
+ self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
+ def #{ivar}=(obj) self.class.#{ivar} = obj end
+ RUBY
+ end
+ end
+ end
+
+ # Defines class-level inheritable attribute accessor. Attributes are available to subclasses,
+ # each subclass has a copy of parent's attribute.
+ #
+ # @param *syms<Array[*#to_s, Hash{:instance_writer => Boolean}]> Array of attributes to
+ # define inheritable accessor for.
+ # @option syms :instance_writer<Boolean> if true, instance-level inheritable attribute writer is defined.
+ # @return <Array[#to_s]> An Array of attributes turned into inheritable accessors.
+ #
+ # @api public
+ def extlib_inheritable_accessor(*syms)
+ class_inheritable_reader(*syms)
+ class_inheritable_writer(*syms)
+ end
+end \ No newline at end of file