Class: Superform::Field

Inherits:
Node
  • Object
show all
Defined in:
lib/superform.rb

Direct Known Subclasses

Rails::Form::Field

Instance Attribute Summary collapse

Attributes inherited from Node

#key, #parent

Instance Method Summary collapse

Constructor Details

#initialize(key, parent:, object: nil, value: nil) ⇒ Field

Returns a new instance of Field.



199
200
201
202
203
204
# File 'lib/superform.rb', line 199

def initialize(key, parent:, object: nil, value: nil)
  super key, parent: parent
  @object = object
  @value = value
  @dom = DOM.new(field: self)
end

Instance Attribute Details

#domObject (readonly)

Returns the value of attribute dom.



197
198
199
# File 'lib/superform.rb', line 197

def dom
  @dom
end

Instance Method Details

#assign(value) ⇒ Object Also known as: value=



215
216
217
218
219
220
221
# File 'lib/superform.rb', line 215

def assign(value)
  if @object and @object.respond_to? "#{@key}="
    @object.send "#{@key}=", value
  else
    @value = value
  end
end

#collectionObject

Wraps a field that’s an array of values with a bunch of fields that are indexed with the array’s index.



226
227
228
# File 'lib/superform.rb', line 226

def collection(&)
  @collection ||= FieldCollection.new(field: self, &)
end

#valueObject Also known as: serialize



206
207
208
209
210
211
212
# File 'lib/superform.rb', line 206

def value
  if @object and @object.respond_to? @key
    @object.send @key
  else
    @value
  end
end