diff --git a/CHANGELOG b/CHANGELOG index f4c05d35..22ed0a18 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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) diff --git a/lib/roda.rb b/lib/roda.rb index e2a76594..95cfcdd8 100644 --- a/lib/roda.rb +++ b/lib/roda.rb @@ -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) @@ -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 diff --git a/spec/define_roda_method_spec.rb b/spec/define_roda_method_spec.rb index f634ead5..5aea3de7 100644 --- a/spec/define_roda_method_spec.rb +++ b/spec/define_roda_method_spec.rb @@ -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 @@ -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|