diff options
author | Mikel Lindsaar <raasdnil@gmail.com> | 2010-02-02 10:51:19 +1100 |
---|---|---|
committer | Mikel Lindsaar <raasdnil@gmail.com> | 2010-02-02 10:51:19 +1100 |
commit | d1eed079e1338249aa3b2881d07029ac4e71f4ed (patch) | |
tree | 18088ca9388e1b7bd27fffea8cb41fa52b618c34 /activesupport/lib/active_support/core_ext/class/attribute.rb | |
parent | ddf2b4add33d5e54c5f5e7adacadbb50d3fa7b52 (diff) | |
parent | b9edb0c60c11025311fb06f2e60b3354f1b6cb09 (diff) | |
download | rails-d1eed079e1338249aa3b2881d07029ac4e71f4ed.tar.gz rails-d1eed079e1338249aa3b2881d07029ac4e71f4ed.tar.bz2 rails-d1eed079e1338249aa3b2881d07029ac4e71f4ed.zip |
Merge branch 'master' of git://github.com/rails/rails
Diffstat (limited to 'activesupport/lib/active_support/core_ext/class/attribute.rb')
-rw-r--r-- | activesupport/lib/active_support/core_ext/class/attribute.rb | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/core_ext/class/attribute.rb b/activesupport/lib/active_support/core_ext/class/attribute.rb new file mode 100644 index 0000000000..d74219cb93 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/class/attribute.rb @@ -0,0 +1,36 @@ +require 'active_support/core_ext/object/metaclass' +require 'active_support/core_ext/module/delegation' + +class Class + # Declare a class-level attribute whose value is inheritable and + # overwritable by subclasses: + # + # class Base + # class_attribute :setting + # end + # + # class Subclass < Base + # end + # + # Base.setting = true + # Subclass.setting # => true + # Subclass.setting = false + # Subclass.setting # => false + # Base.setting # => true + # + # This matches normal Ruby method inheritance: think of writing an attribute + # on a subclass as overriding the reader method. + # + # For convenience, a query method is defined as well: + # + # Subclass.setting? # => false + def class_attribute(*attrs) + attrs.each do |attr| + metaclass.send(:define_method, attr) { } + metaclass.send(:define_method, "#{attr}?") { !!send(attr) } + metaclass.send(:define_method, "#{attr}=") do |value| + metaclass.send(:define_method, attr) { value } + end + end + end +end |