Skip to content

Commit 4bac71a

Browse files
authored
Merge pull request #1 from hoppergee/support_module_function
Support module_function
2 parents 79b7435 + 770fd20 commit 4bac71a

File tree

6 files changed

+79
-51
lines changed

6 files changed

+79
-51
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
.matrixeval/docker-compose
1010
.matrixeval/gemfile_locks
1111
.byebug_history
12+
Gemfile.lock

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## [Unreleased]
22

3+
- Support `module_function`
4+
35
## [1.0.0] - 2022-09-16
46

57
- Now you can use `.active_method` through `include ActiveMethod`

Gemfile.lock

Lines changed: 0 additions & 50 deletions
This file was deleted.

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,27 @@ Bar.call(1, 3, c: 4, d: 5)
8787
# => buzz
8888
```
8989

90+
### Work on module
91+
92+
```ruby
93+
class Hi < ActiveMethod::Base
94+
argument :name
95+
96+
def call
97+
"Hi, #{name}"
98+
end
99+
end
100+
101+
module Say
102+
include ActiveMethod
103+
104+
active_method :hi, module_function: true
105+
end
106+
107+
Say.hi('John')
108+
# => "Hi, John"
109+
```
110+
90111
## Development
91112

92113
```bash

lib/active_method.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@ def self.included(base)
1212
end
1313

1414
module ClassMethods
15-
def active_method(name, method_class = nil)
15+
def active_method(name, method_class = nil, **options)
1616
method_class ||= Util.constantize self, Util.camel_case(name)
1717

1818
define_method name do |*args|
1919
method_class[self].call(*args)
2020
end
21+
22+
if options[:module_function]
23+
module_function name
24+
end
2125
end
2226
end
2327
end

test/test_active_method.rb

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,54 @@ def z; 'z'; end
5555
assert_equal ['a', 'b', 'c', 'd', 'x', 'y', 'z'], Example::FooBar.new.baz('a', 'b', c: 'c', d: 'd')
5656
end
5757

58+
################
59+
# .active_method for module
60+
################
61+
62+
class Hi < ActiveMethod::Base
63+
argument :name
64+
65+
def call
66+
"Hi, #{name}"
67+
end
68+
end
69+
70+
class Bye < ActiveMethod::Base
71+
argument :name
72+
73+
def call
74+
"Bye, #{name}"
75+
end
76+
end
77+
78+
module Say
79+
include ActiveMethod
80+
81+
active_method :hi
82+
active_method :bye, module_function: true
83+
end
84+
85+
class Person
86+
include Say
87+
end
88+
89+
class Dog
90+
include ActiveMethod
91+
end
92+
93+
it ".acive_method - works on module" do
94+
assert_equal "Hi, John", Person.new.hi("John")
95+
end
96+
97+
it ".acive_method - works as a module_function" do
98+
assert_equal "Bye, John", Say.bye("John")
99+
end
100+
101+
it ".acive_method - raise error for a class when pass module_functin: true" do
102+
error = assert_raises NoMethodError do
103+
Dog.active_method(:hi, module_function: true)
104+
end
105+
assert_includes error.message, "undefined method `module_function'"
106+
end
107+
58108
end

0 commit comments

Comments
 (0)