Class: Superform::Field
Overview
A Field represents the data associated with a form element. This class provides methods for accessing and modifying the field’s value. HTML concerns are all delegated to the DOM object.
Direct Known Subclasses
Defined Under Namespace
Classes: Kit
Instance Attribute Summary collapse
-
#dom ⇒ Object
readonly
Returns the value of attribute dom.
Attributes inherited from Node
Class Method Summary collapse
Instance Method Summary collapse
- #assign(value) ⇒ Object (also: #value=)
-
#collection ⇒ Object
Wraps a field that’s an array of values with a bunch of fields that are indexed with the array’s index.
-
#field ⇒ Object
Make the name more obvious for extending or writing docs.
-
#initialize(key, parent:, object: nil, value: nil) {|_self| ... } ⇒ Field
constructor
A new instance of Field.
- #kit(form) ⇒ Object
- #value ⇒ Object (also: #serialize)
Constructor Details
#initialize(key, parent:, object: nil, value: nil) {|_self| ... } ⇒ Field
Returns a new instance of Field.
8 9 10 11 12 13 14 |
# File 'lib/superform/field.rb', line 8 def initialize(key, parent:, object: nil, value: nil) super key, parent: parent @object = object @value = value @dom = Superform::DOM.new(field: self) yield self if block_given? end |
Instance Attribute Details
#dom ⇒ Object (readonly)
Returns the value of attribute dom.
6 7 8 |
# File 'lib/superform/field.rb', line 6 def dom @dom end |
Class Method Details
.inherited(subclass) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/superform/field.rb', line 83 def self.inherited(subclass) super # Create a new Kit class for each Field subclass with true isolation # Copy methods from parent Field classes at creation time, not through inheritance subclass.const_set(:Kit, Class.new(Field::Kit)) # Copy all existing methods from the inheritance chain field_class = self while field_class != Field copy_field_methods_to_kit(field_class, subclass::Kit) field_class = field_class.superclass end end |
.method_added(method_name) ⇒ Object
97 98 99 100 101 102 103 104 105 |
# File 'lib/superform/field.rb', line 97 def self.method_added(method_name) super # Skip if this is the base Field class or if we don't have a Kit class yet return if self == Field return unless const_defined?(:Kit, false) # Only add method to THIS class's Kit, not subclasses (isolation) add_method_to_kit(method_name, self::Kit) end |
Instance Method Details
#assign(value) ⇒ Object Also known as: value=
25 26 27 28 29 30 31 |
# File 'lib/superform/field.rb', line 25 def assign(value) if @object and @object.respond_to? "#{@key}=" @object.send "#{@key}=", value else @value = value end end |
#collection ⇒ Object
Wraps a field that’s an array of values with a bunch of fields that are indexed with the array’s index.
36 37 38 |
# File 'lib/superform/field.rb', line 36 def collection(&) @collection ||= FieldCollection.new(field: self, &) end |
#field ⇒ Object
Make the name more obvious for extending or writing docs.
41 42 43 |
# File 'lib/superform/field.rb', line 41 def field self end |
#kit(form) ⇒ Object
107 108 109 |
# File 'lib/superform/field.rb', line 107 def kit(form) self.class::Kit.new(field: self, form: form) end |
#value ⇒ Object Also known as: serialize
16 17 18 19 20 21 22 |
# File 'lib/superform/field.rb', line 16 def value if @object and @object.respond_to? @key @object.send @key else @value end end |