Class: Formalist::Element

Inherits:
Object
  • Object
show all
Extended by:
ClassInterface
Defined in:
lib/formalist/element.rb,
lib/formalist/element/attributes.rb,
lib/formalist/element/definition.rb,
lib/formalist/element/class_interface.rb,
lib/formalist/element/permitted_children.rb

Defined Under Namespace

Modules: ClassInterface Classes: Attributes, Definition, PermittedChildren

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ClassInterface

attribute, attributes_schema, permitted_children, type

Constructor Details

#initialize(*args, attributes, children, input, errors) ⇒ Element

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Element.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/formalist/element.rb', line 13

def initialize(*args, attributes, children, input, errors)
  # Set supplied attributes or their defaults
  full_attributes = self.class.attributes_schema.each_with_object({}) { |(name, defn), memo|
    value = attributes[name] || defn[:default]
    memo[name] = value unless value.nil?
  }

  # Then run them through the schema
  @attributes = Types::Hash.schema(self.class.attributes_schema.map { |name, defn| [name, defn[:type]] }.to_h).(full_attributes)

  @children = []
  @input = input
  @errors = errors
end

Instance Attribute Details

#attributesObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



10
11
12
# File 'lib/formalist/element.rb', line 10

def attributes
  @attributes
end

#childrenObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



10
11
12
# File 'lib/formalist/element.rb', line 10

def children
  @children
end

#errorsObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



10
11
12
# File 'lib/formalist/element.rb', line 10

def errors
  @errors
end

#inputObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



10
11
12
# File 'lib/formalist/element.rb', line 10

def input
  @input
end

Instance Method Details

#to_astObject

This method is abstract.

Raises:

  • (NotImplementedError)


47
48
49
# File 'lib/formalist/element.rb', line 47

def to_ast
  raise NotImplementedError
end

#typeSymbol

Returns the element’s type, which is a symbolized, camlized representation of the element’s class name.

This is a critical hook for customising form rendering when using custom form elements, since the type in this case will be based on the name of form element’s sublass.

Examples:

Basic element

field.type # => :field

Custom element

my_field.type # => :my_field

Returns:

  • (Symbol)

    the element type.



42
43
44
# File 'lib/formalist/element.rb', line 42

def type
  self.class.type
end