Skip to content

Commit

Permalink
Only splat class/symbol matcher block return value if it returns array
Browse files Browse the repository at this point in the history
This fixes issues when having the blocks return objects that
respond to to_a.
  • Loading branch information
jeremyevans committed Sep 17, 2024
1 parent 4067794 commit 885617a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/roda/plugins/_symbol_class_matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ def _merge_matcher_blocks(type, obj, block, matcher_meth)

proc do |*a|
if captures = send(matcher_meth, *a)
send(convert_meth, *captures)
if captures.is_a?(Array)
send(convert_meth, *captures)
else
send(convert_meth, captures)
end
end
end
else
Expand Down
11 changes: 11 additions & 0 deletions spec/plugin/class_matchers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,17 @@ def klass9.to_s; "klass9" end
body("/1000000000000000000001").must_equal ""
end

it "yields hash instances as single arguments" do
app(:class_matchers) do |r|
r.is('a', Array){|h| h.to_a.join(',')}
r.is('h', Hash){|h| h.to_a.join('-')}
end
app.class_matcher(Hash, /(\w)(\w)/){|k,v| {k=>v}}
app.class_matcher(Array, Hash){|h| h['c'] = 'd'; h }
body('/h/ab').must_equal 'a-b'
body('/a/ab').must_equal 'a,b,c,d'
end

it "freezes :class_matchers option when freezing app" do
app(:class_matchers){|r| }
app.freeze
Expand Down

0 comments on commit 885617a

Please sign in to comment.