Skip to content

Commit b2d82e2

Browse files
authored
Merge pull request #13 from hoppergee/support_inheritance
Support inheritance
2 parents 5aa09a6 + 9f32d5b commit b2d82e2

File tree

4 files changed

+58
-28
lines changed

4 files changed

+58
-28
lines changed

CHANGELOG.md

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

3+
## [1.5.0] - 2024-12-18
4+
5+
- Support inheritance
6+
37
## [1.4.0] - 2024-06-23
48

59
- Support customize method owner name

lib/active_method/base.rb

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,17 @@ def call(*args, &block)
2222
end
2323

2424
def arguments
25-
class_variable_get(:@@arguments)
25+
@arguments ||= {}
2626
end
2727

2828
def keyword_arguments
29-
class_variable_get(:@@keyword_arguments)
29+
@keyword_arguments ||= []
3030
end
3131

3232
def owner_name
33-
class_variable_get(:@@owner_name)
34-
end
35-
36-
def inherited(subclass)
37-
subclass.init_arguments
38-
subclass.init_keyword_arguments
39-
subclass.init_owner_name
33+
return @owner_name unless @owner_name.nil?
34+
return if self == ActiveMethod::Base
35+
superclass.owner_name
4036
end
4137

4238
protected
@@ -75,26 +71,9 @@ def parse_keyword_argument(name, opts = {})
7571
end
7672

7773
def parse_method_owner(name)
78-
class_variable_set(:@@owner_name, name)
79-
end
80-
81-
def init_arguments
82-
return if self.class_variable_defined?(:@@arguments)
83-
84-
self.class_variable_set(:@@arguments, {})
74+
@owner_name = name
8575
end
8676

87-
def init_keyword_arguments
88-
return if self.class_variable_defined?(:@@keyword_arguments)
89-
90-
self.class_variable_set(:@@keyword_arguments, [])
91-
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
9877
end
9978

10079
def initialize(*args)

lib/active_method/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module ActiveMethod
4-
VERSION = "1.4.0"
4+
VERSION = "1.5.0"
55
end

test/test_active_method.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,4 +261,51 @@ class Laptop
261261
assert laptop.off
262262
end
263263

264+
################
265+
# .active_method with inheritance
266+
################
267+
268+
class DemoAPIBase < ActiveMethod::Base
269+
owner :client
270+
end
271+
272+
class GetA < DemoAPIBase
273+
argument :id
274+
275+
def call
276+
puts "Request GET: /a"
277+
end
278+
end
279+
280+
class GetB < DemoAPIBase
281+
keyword_argument :first
282+
keyword_argument :per
283+
284+
def call
285+
puts "Request GET: /b"
286+
end
287+
end
288+
289+
class Client
290+
include ActiveMethod
291+
active_method :get_a
292+
active_method :get_b
293+
end
294+
295+
it ".active_method can orgianize with a Base methd" do
296+
get_a_arguments = GetA.arguments.values
297+
assert_equal 1, get_a_arguments.count
298+
assert_equal :id, get_a_arguments[0].name
299+
get_a_keyword_arguments = GetA.keyword_arguments
300+
assert_equal 0, get_a_keyword_arguments.count
301+
assert_equal :client, GetA.owner_name
302+
303+
get_b_arguments = GetB.arguments.values
304+
assert_equal 0, get_b_arguments.count
305+
get_b_keyword_arguments = GetB.keyword_arguments
306+
assert_equal :first, get_b_keyword_arguments[0].name
307+
assert_equal :per, get_b_keyword_arguments[1].name
308+
assert_equal :client, GetB.owner_name
309+
end
310+
264311
end

0 commit comments

Comments
 (0)