Skip to content

Commit b629fd1

Browse files
committed
Fix an issue when use argument only
1 parent 761c85a commit b629fd1

File tree

2 files changed

+115
-3
lines changed

2 files changed

+115
-3
lines changed

lib/active_method/base.rb

+6-3
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,18 @@ def keyword_arguments
2424
class_variable_get(:@@keyword_arguments)
2525
end
2626

27-
private
27+
def inherited(subclass)
28+
subclass.init_arguments
29+
subclass.init_keyword_arguments
30+
end
31+
32+
protected
2833

2934
def method_missing(method_name, *args)
3035
case method_name.to_s
3136
when /^argument(_\d)*$/
32-
init_arguments
3337
parse_argument(method_name, *args)
3438
when 'keyword_argument'
35-
init_keyword_arguments
3639
parse_keyword_argument(*args)
3740
else
3841
super

test/active_method/test_base.rb

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# frozen_string_literal: true
2+
3+
require "test_helper"
4+
5+
class ActiveMethod::TestBase < ApplicationTest
6+
7+
it ".call" do
8+
klass :ExampleMethod, ActiveMethod::Base do
9+
argument :a
10+
argument :b, default: 2
11+
keyword_argument :c
12+
keyword_argument :d, default: 4
13+
14+
def call
15+
[a, b, c, d]
16+
end
17+
end
18+
19+
assert_equal [1, 2, nil, 4], ExampleMethod.call(1)
20+
assert_equal [1, 3, nil, 4], ExampleMethod.call(1, 3)
21+
assert_equal [1, 3, 6, 4], ExampleMethod.call(1, 3, c: 6)
22+
assert_equal [1, 3, 4, 5], ExampleMethod.call(1, 3, c: 4, d: 5)
23+
24+
remove_klass :ExampleMethod
25+
end
26+
27+
it ".call - with argument_(number)" do
28+
klass :ExampleMethod, ActiveMethod::Base do
29+
argument_2 :b, default: 2
30+
argument_1 :a
31+
keyword_argument :d, default: 4
32+
keyword_argument :c
33+
34+
def call
35+
[a, b, c, d]
36+
end
37+
end
38+
39+
assert_equal [1, 2, nil, 4], ExampleMethod.call(1)
40+
assert_equal [1, 3, nil, 4], ExampleMethod.call(1, 3)
41+
assert_equal [1, 3, 6, 4], ExampleMethod.call(1, 3, c: 6)
42+
assert_equal [1, 3, 4, 5], ExampleMethod.call(1, 3, c: 4, d: 5)
43+
44+
remove_klass :ExampleMethod
45+
end
46+
47+
it ".call - with single argument" do
48+
klass :ExampleMethod, ActiveMethod::Base do
49+
argument :a
50+
51+
def call
52+
a
53+
end
54+
end
55+
56+
assert_equal 1, ExampleMethod.call(1)
57+
assert_equal 2, ExampleMethod.call(2)
58+
59+
remove_klass :ExampleMethod
60+
end
61+
62+
it ".call - with two arguments" do
63+
klass :ExampleMethod, ActiveMethod::Base do
64+
argument :a
65+
argument :b, default: 2
66+
67+
def call
68+
[a, b]
69+
end
70+
end
71+
72+
assert_equal [1, 2], ExampleMethod.call(1)
73+
assert_equal [1, 3], ExampleMethod.call(1, 3)
74+
75+
remove_klass :ExampleMethod
76+
end
77+
78+
it ".call - with one keyword argument" do
79+
klass :ExampleMethod, ActiveMethod::Base do
80+
keyword_argument :a
81+
82+
def call
83+
a
84+
end
85+
end
86+
87+
assert_equal 1, ExampleMethod.call(a: 1)
88+
assert_equal 3, ExampleMethod.call(a: 3)
89+
90+
remove_klass :ExampleMethod
91+
end
92+
93+
it ".call - with two keyword arguments" do
94+
klass :ExampleMethod, ActiveMethod::Base do
95+
keyword_argument :a
96+
keyword_argument :b, default: 2
97+
98+
def call
99+
[a, b]
100+
end
101+
end
102+
103+
assert_equal [1, 2], ExampleMethod.call(a: 1)
104+
assert_equal [1, 3], ExampleMethod.call(a: 1, b: 3)
105+
106+
remove_klass :ExampleMethod
107+
end
108+
109+
end

0 commit comments

Comments
 (0)