aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2013-11-15 07:13:43 -0800
committerRafael Mendonça França <rafaelmfranca@gmail.com>2013-11-15 07:13:43 -0800
commit72551c29f26f757af72b0fefd689244fb6366c74 (patch)
treef776d3f501a1efa8b9e71c250295a95b457a3709 /activesupport/lib/active_support
parente7c90e646b48b7995401e3db963431664f045b00 (diff)
parent0e953c94b152abf0de1c04dba74c22f935bc85c7 (diff)
downloadrails-72551c29f26f757af72b0fefd689244fb6366c74.tar.gz
rails-72551c29f26f757af72b0fefd689244fb6366c74.tar.bz2
rails-72551c29f26f757af72b0fefd689244fb6366c74.zip
Merge pull request #10848 from 907th/add_block_to_cattr_reader
Fix #10847: allow to pass a block to `cattr_reader`.
Diffstat (limited to 'activesupport/lib/active_support')
-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