Skip to content

Commit 5aa09a6

Browse files
authored
Merge pull request #12 from hoppergee/owner_name
Customize owner name
2 parents fe7513e + 5ee7fc3 commit 5aa09a6

File tree

6 files changed

+74
-7
lines changed

6 files changed

+74
-7
lines changed

CHANGELOG.md

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

3+
## [1.4.0] - 2024-06-23
4+
5+
- Support customize method owner name
6+
37
## [1.3.0] - 2022-12-23
48

59
- Support &block

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ User.new.foo('aa', 3, c: 'cc', d: 5)
5252
#=> a: aa, b: 3, c: cc, d: 5
5353
```
5454

55-
### Get the owner of the method
55+
### Customize the owner name of the method
5656

5757
```ruby
5858
class User
@@ -63,8 +63,10 @@ class User
6363
end
6464

6565
class Hi < ActiveMethod::Base
66+
onwer :support
67+
6668
def call
67-
puts "Hi, I'm a #{@__method_owner.fromal_name}. Please call me #{user.name}"
69+
puts "Hi, I'm a #{support.fromal_name}. Please call me #{user.name}"
6870
end
6971
end
7072

lib/active_method/base.rb

+24-3
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,14 @@ def keyword_arguments
2929
class_variable_get(:@@keyword_arguments)
3030
end
3131

32+
def owner_name
33+
class_variable_get(:@@owner_name)
34+
end
35+
3236
def inherited(subclass)
3337
subclass.init_arguments
3438
subclass.init_keyword_arguments
39+
subclass.init_owner_name
3540
end
3641

3742
protected
@@ -42,6 +47,8 @@ def method_missing(method_name, *args)
4247
parse_argument(method_name, *args)
4348
when 'keyword_argument'
4449
parse_keyword_argument(*args)
50+
when 'owner'
51+
parse_method_owner(*args)
4552
else
4653
super
4754
end
@@ -67,6 +74,10 @@ def parse_keyword_argument(name, opts = {})
6774
end
6875
end
6976

77+
def parse_method_owner(name)
78+
class_variable_set(:@@owner_name, name)
79+
end
80+
7081
def init_arguments
7182
return if self.class_variable_defined?(:@@arguments)
7283

@@ -78,6 +89,12 @@ def init_keyword_arguments
7889

7990
self.class_variable_set(:@@keyword_arguments, [])
8091
end
92+
93+
def init_owner_name
94+
return if self.class_variable_defined?(:@@owner_name)
95+
96+
self.class_variable_set(:@@owner_name, nil)
97+
end
8198
end
8299

83100
def initialize(*args)
@@ -104,9 +121,13 @@ def initialize(*args)
104121
def __set_owner(owner)
105122
@__method_owner = owner
106123

107-
klass = owner.is_a?(Class) ? owner : owner.class
108-
instance_name = Util.snake_case(klass.name.split("::").last)
109-
self.define_singleton_method instance_name do
124+
@__owner_name = self.class.owner_name
125+
if @__owner_name.nil?
126+
klass = owner.is_a?(Class) ? owner : owner.class
127+
@__owner_name = Util.snake_case(klass.name.split("::").last)
128+
end
129+
130+
self.define_singleton_method @__owner_name do
110131
@__method_owner
111132
end
112133
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.3.0"
4+
VERSION = "1.4.0"
55
end

matrixeval.yml

+7-1
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,15 @@ matrix:
1818
container:
1919
image: ruby:3.0.4
2020
- key: 3.1
21-
default: true
2221
container:
2322
image: ruby:3.1.2
23+
- key: 3.2
24+
container:
25+
image: ruby:3.2.4
26+
- key: 3.3
27+
default: true
28+
container:
29+
image: ruby:3.3.4
2430
# - key: jruby-9.3
2531
# container:
2632
# image: jruby:9.3

test/test_active_method.rb

+34
Original file line numberDiff line numberDiff line change
@@ -227,4 +227,38 @@ def a=(value)
227227
assert_equal 'aaa', ClassConfiguration.a
228228
end
229229

230+
################
231+
# .active_method customize owner name
232+
################
233+
234+
class PowerOff < ActiveMethod::Base
235+
owner :machine
236+
237+
def call
238+
machine.off = true
239+
end
240+
end
241+
242+
class Desktop
243+
include ActiveMethod
244+
active_method :power_off
245+
attr_accessor :off
246+
end
247+
248+
class Laptop
249+
include ActiveMethod
250+
active_method :power_off
251+
attr_accessor :off
252+
end
253+
254+
it ".active_method can customize owenr name for sharing to work like a concern" do
255+
desktop = Desktop.new
256+
desktop.power_off
257+
assert desktop.off
258+
259+
laptop = Laptop.new
260+
laptop.power_off
261+
assert laptop.off
262+
end
263+
230264
end

0 commit comments

Comments
 (0)