aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib
diff options
context:
space:
mode:
authorAlexey Chernenkov <laise@pisem.net>2013-06-05 13:05:53 +0600
committerAlexey Chernenkov <laise@pisem.net>2013-11-15 19:54:51 +0600
commit0e953c94b152abf0de1c04dba74c22f935bc85c7 (patch)
treea916e031786e90c2fc96200b573d51e879e9ffb8 /activesupport/lib
parent81475534ed9e2e1b7bade50f1e1099975e385a46 (diff)
downloadrails-0e953c94b152abf0de1c04dba74c22f935bc85c7.tar.gz
rails-0e953c94b152abf0de1c04dba74c22f935bc85c7.tar.bz2
rails-0e953c94b152abf0de1c04dba74c22f935bc85c7.zip
Unify `cattr_*` interface: allow to pass a block to `cattr_reader`.
Example: class A cattr_reader(:defr) { 'default_reader_value' } end A.defr # => 'default_reader_value'
Diffstat (limited to 'activesupport/lib')
-rw-r--r--activesupport/lib/active_support/core_ext/class/attribute_accessors.rb11
1 files changed, 11 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb b/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb
index 34859617c9..6daa828b24 100644
--- a/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb
+++ b/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb
@@ -29,6 +29,16 @@ class Class
# end
#
# Person.new.hair_colors # => NoMethodError
+ #
+ # Also, you can pass a block to set up the attribute with a default value.
+ #
+ # class Person
+ # cattr_reader :hair_colors do
+ # [:brown, :black, :blonde, :red]
+ # end
+ # end
+ #
+ # Person.hair_colors # => [:brown, :black, :blonde, :red]
def cattr_reader(*syms)
options = syms.extract_options!
syms.each do |sym|
@@ -50,6 +60,7 @@ class Class
end
EOS
end
+ class_variable_set("@@#{sym}", yield) if block_given?
end
end