Class: Protoform::Field

Inherits:
Node
  • Object
show all
Defined in:
lib/protoform/field.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.



7
8
9
10
11
12
# File 'lib/protoform/field.rb', line 7

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

Instance Attribute Details

#domObject (readonly)

Returns the value of attribute dom.



5
6
7
# File 'lib/protoform/field.rb', line 5

def dom
  @dom
end

#objectObject (readonly)

Returns the value of attribute object.



5
6
7
# File 'lib/protoform/field.rb', line 5

def object
  @object
end

Instance Method Details

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



23
24
25
26
27
28
29
# File 'lib/protoform/field.rb', line 23

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

#collection(&block) ⇒ Object

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



34
35
36
# File 'lib/protoform/field.rb', line 34

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

#valueObject Also known as: serialize



14
15
16
17
18
19
20
# File 'lib/protoform/field.rb', line 14

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