@@ -134,4 +134,97 @@ class RobotFactory
134
134
assert_includes error . message , "undefined method `build_a_robot'"
135
135
end
136
136
137
+ ################
138
+ # .active_method with &block
139
+ ################
140
+
141
+ class SetInstanceConfig < ActiveMethod ::Base
142
+
143
+ def call
144
+ yield instance_configuration
145
+ end
146
+
147
+ end
148
+
149
+ class InstanceConfiguration
150
+ include ActiveMethod
151
+
152
+ attr_accessor :a
153
+ attr_accessor :b
154
+
155
+ active_method :config , SetInstanceConfig
156
+ end
157
+
158
+ it ".active_method work as a instance method with a block" do
159
+ configuration = InstanceConfiguration . new
160
+ configuration . config do |config |
161
+ config . a = 'aaa'
162
+ config . b = 'bbb'
163
+ end
164
+ assert_equal 'aaa' , configuration . a
165
+ assert_equal 'bbb' , configuration . b
166
+ end
167
+
168
+ class SetModuleConfig < ActiveMethod ::Base
169
+
170
+ def call
171
+ yield @__method_owner
172
+ end
173
+
174
+ end
175
+
176
+ module ModuleConfiguration
177
+ include ActiveMethod
178
+
179
+ module_function
180
+
181
+ def a
182
+ @a
183
+ end
184
+
185
+ def a = ( value )
186
+ @a = value
187
+ end
188
+
189
+ active_method :config , SetModuleConfig , module_function : true
190
+ end
191
+
192
+ it ".active_method work as a module method with a block" do
193
+ ModuleConfiguration . config do |config |
194
+ config . a = 'aaa'
195
+ end
196
+ assert_equal 'aaa' , ModuleConfiguration . a
197
+ end
198
+
199
+ class SetClassConfig < ActiveMethod ::Base
200
+
201
+ def call
202
+ yield class_configuration
203
+ end
204
+
205
+ end
206
+
207
+ class ClassConfiguration
208
+ include ActiveMethod
209
+
210
+ class << self
211
+ def a
212
+ @@a
213
+ end
214
+
215
+ def a = ( value )
216
+ @@a = value
217
+ end
218
+ end
219
+
220
+ active_method :config , SetClassConfig , class_method : true
221
+ end
222
+
223
+ it ".active_method work as a instance method with a block" do
224
+ ClassConfiguration . config do |config |
225
+ config . a = 'aaa'
226
+ end
227
+ assert_equal 'aaa' , ClassConfiguration . a
228
+ end
229
+
137
230
end
0 commit comments