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/class_interface.rb

Defined Under Namespace

Modules: ClassInterface Classes: Attributes

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ClassInterface

attribute, attributes_schema

Constructor Details

#initialize(name: nil, attributes: {}, children: [], input: nil, 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.



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/formalist/element.rb', line 22

def initialize(name: nil, attributes: {}, children: [], input: nil, errors: [])
  @name = name&.to_sym

  @attributes = self.class.attributes_schema.each_with_object({}) { |(name, defn), hsh|
    value = attributes.fetch(name) { defn[:default] }
    hsh[name] = value unless value.nil?
  }

  @children = 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.



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

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.



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

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.



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

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.



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

def input
  @input
end

#nameObject (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.



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

def name
  @name
end

Class Method Details

.build(**args) ⇒ Object

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.



12
13
14
# File 'lib/formalist/element.rb', line 12

def self.build(**args)
  new(**args)
end

.fill(input: {}, errors: {}, **args) ⇒ Object

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.



17
18
19
# File 'lib/formalist/element.rb', line 17

def self.fill(input: {}, errors: {}, **args)
  new(**args).fill(input: input, errors: errors)
end

Instance Method Details

#==(other) ⇒ Object



53
54
55
# File 'lib/formalist/element.rb', line 53

def ==(other)
  name && type == other.type && name == other.name
end

#fill(input: {}, errors: {}, **args) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/formalist/element.rb', line 35

def fill(input: {}, errors: {}, **args)
  return self if input == @input && errors == @errors

  args = {
    name: @name,
    attributes: @attributes,
    children: @children,
    input: input,
    errors: errors,
  }.merge(args)

  self.class.new(**args)
end

#to_astObject

This method is abstract.

Raises:

  • (NotImplementedError)


58
59
60
# File 'lib/formalist/element.rb', line 58

def to_ast
  raise NotImplementedError
end

#typeObject



49
50
51
# File 'lib/formalist/element.rb', line 49

def type
  self.class.type
end