Skip to content

Commit fe7513e

Browse files
authored
Merge pull request #6 from hoppergee/support_block
Support block
2 parents 54c2874 + 0b37a09 commit fe7513e

File tree

6 files changed

+106
-9
lines changed

6 files changed

+106
-9
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## [Unreleased]
22

3+
## [1.3.0] - 2022-12-23
4+
5+
- Support &block
6+
37
## [1.2.1] - 2022-11-21
48

59
- Build on main branch

lib/active_method.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ def active_method(name, method_class = nil, **options)
1616
method_class ||= Util.constantize self, Util.camel_case(name)
1717

1818
if options[:class_method]
19-
define_singleton_method name do |*args|
20-
method_class[self].call(*args)
19+
define_singleton_method name do |*args, &block|
20+
method_class[self].call(*args, &block)
2121
end
2222

2323
else
24-
define_method name do |*args|
25-
method_class[self].call(*args)
24+
define_method name do |*args, &block|
25+
method_class[self].call(*args, &block)
2626
end
2727

2828
if options[:module_function]

lib/active_method/base.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ def [](owner)
1717
end
1818
alias_method :on, :[]
1919

20-
def call(*args)
21-
new(*args).call
20+
def call(*args, &block)
21+
new(*args).call(&block)
2222
end
2323

2424
def arguments

lib/active_method/executor.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ def initialize(klass, owner)
88
@owner = owner
99
end
1010

11-
def call(*args)
11+
def call(*args, &block)
1212
method = method_class.new(*args)
1313
method.__set_owner(owner)
14-
method.call
14+
method.call(&block)
1515
end
1616

1717
end

lib/active_method/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module ActiveMethod
4-
VERSION = "1.2.1"
4+
VERSION = "1.3.0"
55
end

test/test_active_method.rb

+93
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,97 @@ class RobotFactory
134134
assert_includes error.message, "undefined method `build_a_robot'"
135135
end
136136

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+
137230
end

0 commit comments

Comments
 (0)