File tree 6 files changed +70
-7
lines changed
6 files changed +70
-7
lines changed Original file line number Diff line number Diff line change 1
1
## [ Unreleased]
2
2
3
+ ## [ 1.2.0] - 2022-11-21
4
+
5
+ - Support ` class_method `
6
+
3
7
## [ 1.1.1] - 2022-10-08
4
8
5
9
- Change owner method define from ` self.calss.define_method ` to ` self.define_singleton_method `
Original file line number Diff line number Diff line change @@ -108,6 +108,27 @@ Say.hi('John')
108
108
# => "Hi, John"
109
109
```
110
110
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
+
111
132
## Development
112
133
113
134
``` bash
Original file line number Diff line number Diff line change @@ -15,13 +15,21 @@ module ClassMethods
15
15
def active_method ( name , method_class = nil , **options )
16
16
method_class ||= Util . constantize self , Util . camel_case ( name )
17
17
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
21
27
22
- if options [ :module_function ]
23
- module_function name
28
+ if options [ :module_function ]
29
+ module_function name
30
+ end
24
31
end
32
+
25
33
end
26
34
end
27
35
end
Original file line number Diff line number Diff line change @@ -104,7 +104,8 @@ def initialize(*args)
104
104
def __set_owner ( owner )
105
105
@__method_owner = owner
106
106
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 )
108
109
self . define_singleton_method instance_name do
109
110
@__method_owner
110
111
end
Original file line number Diff line number Diff line change 1
1
# frozen_string_literal: true
2
2
3
3
module ActiveMethod
4
- VERSION = "1.1.1 "
4
+ VERSION = "1.2.0 "
5
5
end
Original file line number Diff line number Diff line change @@ -105,4 +105,33 @@ class Dog
105
105
assert_includes error . message , "undefined method `module_function'"
106
106
end
107
107
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
+
108
137
end
You can’t perform that action at this time.
0 commit comments