Skip to content

Commit cc6c377

Browse files
committed
Add "grab" method to Field so its easier to extend field methods
1 parent 2e80a6b commit cc6c377

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

lib/superform/field.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,28 @@ def field
4242
self
4343
end
4444

45+
# A helper, borrowed from Phlex, that makes it easy to "grab" values
46+
# passed into a method that are reserved keywords. For example, this
47+
# would throw a syntax error because `class` and `end` are reserved:
48+
#
49+
# def foo(end:, class:)
50+
# puts class
51+
# puts end
52+
# end
53+
#
54+
# So you "grab" them like this:
55+
# def foo(end:, class:)
56+
# puts grab(end:)
57+
# puts grab(class:)
58+
# end
59+
private def grab(**bindings)
60+
if bindings.size > 1
61+
bindings.values
62+
else
63+
bindings.values.first
64+
end
65+
end
66+
4567
# High-performance Kit proxy that wraps field methods with form.render calls.
4668
# Uses Ruby class hooks to define methods at the class level for maximum speed:
4769
# - Methods are defined once per Field class, not per Kit instance

0 commit comments

Comments
 (0)