@@ -3,7 +3,12 @@ module Superform
33 # methods for accessing and modifying the field's value. HTML concerns are all
44 # delegated to the DOM object.
55 class Field < Node
6- attr_reader :dom
6+ # Helpful for overriding methods in the `input`, `email`, `label`, etc. calls.
7+ include Phlex ::Helpers
8+
9+ # Expose these as a reader so they're accessible from the `input`, `label`,
10+ # etc. methods.
11+ attr_reader :dom , :object
712
813 def initialize ( key , parent :, object : nil , value : nil )
914 super key , parent : parent
@@ -42,28 +47,6 @@ def field
4247 self
4348 end
4449
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-
6750 # High-performance Kit proxy that wraps field methods with form.render calls.
6851 # Uses Ruby class hooks to define methods at the class level for maximum speed:
6952 # - Methods are defined once per Field class, not per Kit instance
@@ -85,7 +68,7 @@ def self.inherited(subclass)
8568 # Create a new Kit class for each Field subclass with true isolation
8669 # Copy methods from parent Field classes at creation time, not through inheritance
8770 subclass . const_set ( :Kit , Class . new ( Field ::Kit ) )
88-
71+
8972 # Copy all existing methods from the inheritance chain
9073 field_class = self
9174 while field_class != Field
@@ -99,7 +82,7 @@ def self.method_added(method_name)
9982 # Skip if this is the base Field class or if we don't have a Kit class yet
10083 return if self == Field
10184 return unless const_defined? ( :Kit , false )
102-
85+
10386 # Only add method to THIS class's Kit, not subclasses (isolation)
10487 add_method_to_kit ( method_name , self ::Kit )
10588 end
@@ -111,14 +94,14 @@ def kit(form)
11194 private
11295
11396 def self . copy_field_methods_to_kit ( field_class , kit_class )
114- base_methods = ( Object . instance_methods + Node . instance_methods +
97+ base_methods = ( Object . instance_methods + Node . instance_methods +
11598 [ :dom , :value , :serialize , :assign , :collection , :field , :kit ] ) . to_set
116-
99+
117100 field_class . instance_methods ( false ) . each do |method_name |
118101 next if method_name . to_s . end_with? ( '=' )
119102 next if base_methods . include? ( method_name )
120103 next if kit_class . method_defined? ( method_name )
121-
104+
122105 kit_class . define_method ( method_name ) do |*args , **kwargs , &block |
123106 result = @field . send ( method_name , *args , **kwargs , &block )
124107 @form . render result
@@ -128,12 +111,12 @@ def self.copy_field_methods_to_kit(field_class, kit_class)
128111
129112 def self . add_method_to_kit ( method_name , kit_class )
130113 return if method_name . to_s . end_with? ( '=' )
131-
132- base_methods = ( Object . instance_methods + Node . instance_methods +
114+
115+ base_methods = ( Object . instance_methods + Node . instance_methods +
133116 [ :dom , :value , :serialize , :assign , :collection , :field , :kit ] ) . to_set
134117 return if base_methods . include? ( method_name )
135118 return if kit_class . method_defined? ( method_name )
136-
119+
137120 kit_class . define_method ( method_name ) do |*args , **kwargs , &block |
138121 result = @field . send ( method_name , *args , **kwargs , &block )
139122 @form . render result
0 commit comments