Skip to content

Commit d89be41

Browse files
Add examples of default values using blocks for mattr_reader, mattr_writer and mattr_accessor
1 parent 2837572 commit d89be41

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

activesupport/lib/active_support/core_ext/module/attribute_accessors.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,15 @@ class Module
4343
#
4444
# module HairColors
4545
# mattr_reader :hair_colors, default: [:brown, :black, :blonde, :red]
46+
# mattr_reader(:hair_styles) { [:long, :short] }
4647
# end
4748
#
4849
# class Person
4950
# include HairColors
5051
# end
5152
#
5253
# Person.new.hair_colors # => [:brown, :black, :blonde, :red]
54+
# Person.new.hair_styles # => [:long, :short]
5355
def mattr_reader(*syms, instance_reader: true, instance_accessor: true, default: nil, location: nil)
5456
raise TypeError, "module attributes should be defined directly on class, not singleton" if singleton_class?
5557
location ||= caller_locations(1, 1).first
@@ -107,13 +109,15 @@ def mattr_reader(*syms, instance_reader: true, instance_accessor: true, default:
107109
#
108110
# module HairColors
109111
# mattr_writer :hair_colors, default: [:brown, :black, :blonde, :red]
112+
# mattr_writer(:hair_styles) { [:long, :short] }
110113
# end
111114
#
112115
# class Person
113116
# include HairColors
114117
# end
115118
#
116119
# Person.class_variable_get("@@hair_colors") # => [:brown, :black, :blonde, :red]
120+
# Person.class_variable_get("@@hair_styles") # => [:long, :short]
117121
def mattr_writer(*syms, instance_writer: true, instance_accessor: true, default: nil, location: nil)
118122
raise TypeError, "module attributes should be defined directly on class, not singleton" if singleton_class?
119123
location ||= caller_locations(1, 1).first
@@ -192,13 +196,15 @@ def mattr_writer(*syms, instance_writer: true, instance_accessor: true, default:
192196
#
193197
# module HairColors
194198
# mattr_accessor :hair_colors, default: [:brown, :black, :blonde, :red]
199+
# mattr_accessor(:hair_styles) { [:long, :short] }
195200
# end
196201
#
197202
# class Person
198203
# include HairColors
199204
# end
200205
#
201206
# Person.class_variable_get("@@hair_colors") # => [:brown, :black, :blonde, :red]
207+
# Person.class_variable_get("@@hair_styles") # => [:long, :short]
202208
def mattr_accessor(*syms, instance_reader: true, instance_writer: true, instance_accessor: true, default: nil, &blk)
203209
location = caller_locations(1, 1).first
204210
mattr_reader(*syms, instance_reader: instance_reader, instance_accessor: instance_accessor, default: default, location: location, &blk)

0 commit comments

Comments
 (0)