Skip to content

Commit 515a120

Browse files
authored
Merge pull request #18 from nolith/fix_signature
Cannot use an Hash as the last parameter when there are no options and the last param has a default value
2 parents 1c37b3a + edde76f commit 515a120

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

lib/dry/initializer/signature.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ def each
2525
end
2626

2727
def call
28-
(select(&:param?).map(&:call) + %w(**__options__)).compact.join(", ")
28+
options = all?(&:param?) ? %w(__options__={}) : %w(**__options__)
29+
(select(&:param?).map(&:call) + options).compact.join(", ")
2930
end
3031

3132
private

spec/default_values_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,36 @@ def default_mox
4848
subject = Test::Foo.new
4949
expect(subject.mox).to eql :MOX
5050
end
51+
52+
describe "when the last param has a default and there are no options" do
53+
before do
54+
class Test::Bar
55+
extend Dry::Initializer::Mixin
56+
57+
param :foo
58+
param :bar, default: proc { {} }
59+
end
60+
end
61+
62+
it "instantiate arguments" do
63+
subject = Test::Bar.new(1, 2)
64+
65+
expect(subject.foo).to eql 1
66+
expect(subject.bar).to eql 2
67+
end
68+
69+
it "applies default values" do
70+
subject = Test::Bar.new(1)
71+
72+
expect(subject.foo).to eql 1
73+
expect(subject.bar).to eql({})
74+
end
75+
76+
it "instantiate arguments also if the last is an hash" do
77+
subject = Test::Bar.new(1, { baz: 2, qux: 3 })
78+
79+
expect(subject.foo).to eql 1
80+
expect(subject.bar).to eql({ baz: 2, qux: 3 })
81+
end
82+
end
5183
end

0 commit comments

Comments
 (0)