Skip to content

Commit 11c6466

Browse files
authored
Merge pull request #3 from hoppergee/support_class_method
Support class method
2 parents 2d7f821 + 0549196 commit 11c6466

File tree

6 files changed

+70
-7
lines changed

6 files changed

+70
-7
lines changed

CHANGELOG.md

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

3+
## [1.2.0] - 2022-11-21
4+
5+
- Support `class_method`
6+
37
## [1.1.1] - 2022-10-08
48

59
- Change owner method define from `self.calss.define_method` to `self.define_singleton_method`

README.md

+21
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,27 @@ Say.hi('John')
108108
# => "Hi, John"
109109
```
110110

111+
### Work as a class method
112+
113+
```ruby
114+
class BuildARobot < ActiveMethod::Base
115+
argument :name
116+
117+
def call
118+
"#{robot_factory} build a robot called #{name}"
119+
end
120+
end
121+
122+
class RobotFactory
123+
include ActiveMethod
124+
125+
active_method :build_a_robot, class_method: true
126+
end
127+
128+
RobotFactory.build_a_robot
129+
# => "RobotFactory build a robot called B-2"
130+
```
131+
111132
## Development
112133

113134
```bash

lib/active_method.rb

+13-5
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,21 @@ module ClassMethods
1515
def active_method(name, method_class = nil, **options)
1616
method_class ||= Util.constantize self, Util.camel_case(name)
1717

18-
define_method name do |*args|
19-
method_class[self].call(*args)
20-
end
18+
if options[:class_method]
19+
define_singleton_method name do |*args|
20+
method_class[self].call(*args)
21+
end
22+
23+
else
24+
define_method name do |*args|
25+
method_class[self].call(*args)
26+
end
2127

22-
if options[:module_function]
23-
module_function name
28+
if options[:module_function]
29+
module_function name
30+
end
2431
end
32+
2533
end
2634
end
2735
end

lib/active_method/base.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ def initialize(*args)
104104
def __set_owner(owner)
105105
@__method_owner = owner
106106

107-
instance_name = Util.snake_case(owner.class.name.split("::").last)
107+
klass = owner.is_a?(Class) ? owner : owner.class
108+
instance_name = Util.snake_case(klass.name.split("::").last)
108109
self.define_singleton_method instance_name do
109110
@__method_owner
110111
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.1.1"
4+
VERSION = "1.2.0"
55
end

test/test_active_method.rb

+29
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,33 @@ class Dog
105105
assert_includes error.message, "undefined method `module_function'"
106106
end
107107

108+
################
109+
# .active_method for class method
110+
################
111+
112+
class BuildARobot < ActiveMethod::Base
113+
argument :name
114+
115+
def call
116+
"#{robot_factory} build a robot called #{name}"
117+
end
118+
end
119+
120+
class RobotFactory
121+
include ActiveMethod
122+
123+
active_method :build_a_robot, class_method: true
124+
end
125+
126+
it ".acive_method - works as a class method" do
127+
assert_equal "TestActiveMethod::RobotFactory build a robot called B-2", RobotFactory.build_a_robot("B-2")
128+
end
129+
130+
it ".acive_method - won't works as a instance method" do
131+
error = assert_raises NoMethodError do
132+
RobotFactory.new.build_a_robot("B-2")
133+
end
134+
assert_includes error.message, "undefined method `build_a_robot'"
135+
end
136+
108137
end

0 commit comments

Comments
 (0)