Skip to content

Support &block #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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