Skip to content

Commit

Permalink
Warn when defining method that expects 1 argument when block requires…
Browse files Browse the repository at this point in the history
… multiple arguments when :check_arity option is set to :warn

This was overlooked in commit 3fdf0f0.
  • Loading branch information
jeremyevans committed Aug 14, 2023
1 parent 3816542 commit f493207
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
= master

* Warn when defining method that expects 1 argument when block requires multiple arguments when :check_arity option is set to :warn (jeremyevans)

* Implement the match_hooks plugin using the match_hook_args plugin (jeremyevans)

= 3.71.0 (2023-08-14)
Expand Down
4 changes: 4 additions & 0 deletions lib/roda.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def define_roda_method(meth, expected_arity, &block)
end
call_meth = meth

# RODA4: Switch to false # :warn in last Roda 3 version
if (check_arity = opts.fetch(:check_arity, true)) && !block.lambda?
required_args, optional_args, rest, keyword = _define_roda_method_arg_numbers(block)

Expand Down Expand Up @@ -117,6 +118,9 @@ def define_roda_method(meth, expected_arity, &block)
alias_method meth, meth
meth = :"#{meth}_arity"
elsif required_args > 1
if check_arity == :warn
RodaPlugins.warn "Arity mismatch in block passed to define_roda_method. Expected Arity 1, but multiple arguments required for #{block.inspect}"
end
b = block
block = lambda{|r| instance_exec(r, &b)} # Fallback
end
Expand Down
6 changes: 6 additions & 0 deletions spec/define_roda_method_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@

m1 = app.define_roda_method("x", 1){2}
proc{@scope.send(m1, 1)}.must_raise ArgumentError

m1 = app.define_roda_method("x", 1){|x, y| [x, y]}
proc{@scope.send(m1, 4)}.must_raise ArgumentError
end

deprecated "should warn if :check_arity :warn app option is used and a block with invalid arity is passed" do
Expand All @@ -80,6 +83,9 @@

m1 = app.define_roda_method("x", 1){2}
@scope.send(m1, 3).must_equal 2

m1 = app.define_roda_method("x", 1){|x, y| [x, y]}
@scope.send(m1, 4).must_equal [4, nil]
end

[false, true].each do |warn_dynamic_arity|
Expand Down

0 comments on commit f493207

Please sign in to comment.