@@ -84,16 +84,17 @@ class Class
84
84
# To set a default value for the attribute, pass <tt>default:</tt>, like so:
85
85
#
86
86
# class_attribute :settings, default: {}
87
- def class_attribute ( *attrs )
88
- options = attrs . extract_options!
89
- instance_reader = options . fetch ( :instance_accessor , true ) && options . fetch ( :instance_reader , true )
90
- instance_writer = options . fetch ( :instance_accessor , true ) && options . fetch ( :instance_writer , true )
91
- instance_predicate = options . fetch ( :instance_predicate , true )
92
- default_value = options . fetch ( :default , nil )
93
-
87
+ def class_attribute (
88
+ *attrs ,
89
+ instance_accessor : true ,
90
+ instance_reader : instance_accessor ,
91
+ instance_writer : instance_accessor ,
92
+ instance_predicate : true ,
93
+ default : nil
94
+ )
94
95
attrs . each do |name |
95
96
singleton_class . silence_redefinition_of_method ( name )
96
- define_singleton_method ( name ) { nil }
97
+ define_singleton_method ( name ) { default }
97
98
98
99
singleton_class . silence_redefinition_of_method ( "#{ name } ?" )
99
100
define_singleton_method ( "#{ name } ?" ) { !!public_send ( name ) } if instance_predicate
@@ -102,9 +103,7 @@ def class_attribute(*attrs)
102
103
103
104
singleton_class . silence_redefinition_of_method ( "#{ name } =" )
104
105
define_singleton_method ( "#{ name } =" ) do |val |
105
- singleton_class . class_eval do
106
- redefine_method ( name ) { val }
107
- end
106
+ redefine_singleton_method ( name ) { val }
108
107
109
108
if singleton_class?
110
109
class_eval do
@@ -137,10 +136,6 @@ def class_attribute(*attrs)
137
136
instance_variable_set ivar , val
138
137
end
139
138
end
140
-
141
- unless default_value . nil?
142
- self . send ( "#{ name } =" , default_value )
143
- end
144
139
end
145
140
end
146
141
end
0 commit comments