Class: Phlexi::Field::Builder

Overview

Builder class is responsible for building fields with various options and components.

Defined Under Namespace

Classes: DOM, FieldCollection

Instance Attribute Summary collapse

Attributes inherited from Structure::Node

#key, #parent

Instance Method Summary collapse

Methods included from Options::Attachments

#attachment_reflection

Methods included from Options::Associations

#association_reflection

Methods included from Options::Hints

#has_hint?, #hint

Methods included from Options::Descriptions

#description, #has_description?

Methods included from Options::Placeholders

#placeholder

Methods included from Options::Labels

#label

Methods included from Options::Multiple

#multiple!, #multiple?

Methods included from Options::InferredTypes

#inferred_field_component, #inferred_field_type, #inferred_string_field_type

Methods inherited from Structure::Node

#inspect

Constructor Details

#initialize(key, parent:, object: nil, value: NIL_VALUE, **options) ⇒ Builder

Initializes a new FieldBuilder instance.

Parameters:

  • key (Symbol, String)

    The key for the field.

  • parent (Structure::Namespace)

    The parent object.

  • object (Object, nil) (defaults to: nil)

    The associated object.

  • value (Object) (defaults to: NIL_VALUE)

    The initial value for the field.

  • options (Hash)

    Additional options for the field.



39
40
41
42
43
44
45
46
# File 'lib/phlexi/field/builder.rb', line 39

def initialize(key, parent:, object: nil, value: NIL_VALUE, **options)
  super(key, parent: parent)

  @object = object
  @value = determine_initial_value(value)
  @options = options
  @dom = self.class::DOM.new(field: self)
end

Instance Attribute Details

#attributesHash (readonly)

Attributes for the field.

Returns:

  • (Hash)

    the current value of attributes



14
15
16
# File 'lib/phlexi/field/builder.rb', line 14

def attributes
  @attributes
end

#domStructure::DOM (readonly)

The DOM structure for the field.

Returns:



14
15
16
# File 'lib/phlexi/field/builder.rb', line 14

def dom
  @dom
end

#objectObject (readonly)

The object associated with the field.

Returns:

  • (Object)

    the current value of object



14
15
16
# File 'lib/phlexi/field/builder.rb', line 14

def object
  @object
end

#optionsHash (readonly)

Options for the field.

Returns:

  • (Hash)

    the current value of options



14
15
16
# File 'lib/phlexi/field/builder.rb', line 14

def options
  @options
end

#valueObject (readonly)

Returns the value of attribute value.



30
31
32
# File 'lib/phlexi/field/builder.rb', line 30

def value
  @value
end

Instance Method Details

#has_value?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/phlexi/field/builder.rb', line 57

def has_value?
  attachment_reflection.present? ? value.attached? : (value.present? || value == false)
end

#repeated(collection = nil) {|block| ... } ⇒ FieldCollection

Creates a repeated field collection.

Parameters:

  • range (#each)

    The collection of items to generate displays for.

Yields:

  • (block)

    The block to be executed for each item in the collection.

Returns:



53
54
55
# File 'lib/phlexi/field/builder.rb', line 53

def repeated(collection = nil, &)
  self.class::FieldCollection.new(field: self, collection:, &)
end