Skip to content

Commit

Permalink
Merge pull request #6 from hoppergee/support_block
Browse files Browse the repository at this point in the history
Support block
  • Loading branch information
hoppergee authored Dec 23, 2022
2 parents 54c2874 + 0b37a09 commit fe7513e
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## [Unreleased]

## [1.3.0] - 2022-12-23

- Support &block

## [1.2.1] - 2022-11-21

- Build on main branch
Expand Down
8 changes: 4 additions & 4 deletions lib/active_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ def active_method(name, method_class = nil, **options)
method_class ||= Util.constantize self, Util.camel_case(name)

if options[:class_method]
define_singleton_method name do |*args|
method_class[self].call(*args)
define_singleton_method name do |*args, &block|
method_class[self].call(*args, &block)
end

else
define_method name do |*args|
method_class[self].call(*args)
define_method name do |*args, &block|
method_class[self].call(*args, &block)
end

if options[:module_function]
Expand Down
4 changes: 2 additions & 2 deletions lib/active_method/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def [](owner)
end
alias_method :on, :[]

def call(*args)
new(*args).call
def call(*args, &block)
new(*args).call(&block)
end

def arguments
Expand Down
4 changes: 2 additions & 2 deletions lib/active_method/executor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ def initialize(klass, owner)
@owner = owner
end

def call(*args)
def call(*args, &block)
method = method_class.new(*args)
method.__set_owner(owner)
method.call
method.call(&block)
end

end
Expand Down
2 changes: 1 addition & 1 deletion lib/active_method/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module ActiveMethod
VERSION = "1.2.1"
VERSION = "1.3.0"
end
93 changes: 93 additions & 0 deletions test/test_active_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,97 @@ class RobotFactory
assert_includes error.message, "undefined method `build_a_robot'"
end

################
# .active_method with &block
################

class SetInstanceConfig < ActiveMethod::Base

def call
yield instance_configuration
end

end

class InstanceConfiguration
include ActiveMethod

attr_accessor :a
attr_accessor :b

active_method :config, SetInstanceConfig
end

it ".active_method work as a instance method with a block" do
configuration = InstanceConfiguration.new
configuration.config do |config|
config.a = 'aaa'
config.b = 'bbb'
end
assert_equal 'aaa', configuration.a
assert_equal 'bbb', configuration.b
end

class SetModuleConfig < ActiveMethod::Base

def call
yield @__method_owner
end

end

module ModuleConfiguration
include ActiveMethod

module_function

def a
@a
end

def a=(value)
@a = value
end

active_method :config, SetModuleConfig, module_function: true
end

it ".active_method work as a module method with a block" do
ModuleConfiguration.config do |config|
config.a = 'aaa'
end
assert_equal 'aaa', ModuleConfiguration.a
end

class SetClassConfig < ActiveMethod::Base

def call
yield class_configuration
end

end

class ClassConfiguration
include ActiveMethod

class << self
def a
@@a
end

def a=(value)
@@a = value
end
end

active_method :config, SetClassConfig, class_method: true
end

it ".active_method work as a instance method with a block" do
ClassConfiguration.config do |config|
config.a = 'aaa'
end
assert_equal 'aaa', ClassConfiguration.a
end

end

0 comments on commit fe7513e

Please sign in to comment.