Class: Formulario::Field::FormField

Inherits:
Formulario::Field show all
Defined in:
lib/formulario/fields/form_field.rb

Constant Summary

Constants inherited from Formulario::Field

TYPES

Instance Attribute Summary collapse

Attributes inherited from Formulario::Field

#raw_value

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Formulario::Field

#==, #blank?, #exceptional_class, for, #inspect, #to_s, type_for

Constructor Details

#initialize(raw_value) ⇒ FormField

Returns a new instance of FormField.



36
37
38
39
40
41
42
43
44
# File 'lib/formulario/fields/form_field.rb', line 36

def initialize(raw_value)
  @form = if raw_value.is_a?(::Formulario::Form)
            raw_value
          else
            self.class.type.new(**raw_value)
          end

  super
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)



58
59
60
61
62
# File 'lib/formulario/fields/form_field.rb', line 58

def method_missing(name, *args, &block)
  return fields[name] if fields.has_key?(name)

  super
end

Instance Attribute Details

#formObject (readonly)

Returns the value of attribute form.



4
5
6
# File 'lib/formulario/fields/form_field.rb', line 4

def form
  @form
end

Class Method Details

.[](type) ⇒ Object



6
7
8
9
10
# File 'lib/formulario/fields/form_field.rb', line 6

def self.[](type)
  Class.new(self) do |typed_class|
    typed_class.type = type
  end
end

.defaultObject



16
17
18
# File 'lib/formulario/fields/form_field.rb', line 16

def self.default
  new(type.default)
end

.typeObject



12
13
14
# File 'lib/formulario/fields/form_field.rb', line 12

def self.type
  @type
end

Instance Method Details

#[](value) ⇒ Object



32
33
34
# File 'lib/formulario/fields/form_field.rb', line 32

def [](value)
  fields[value]
end

#exceptional?Boolean

Returns:



24
25
26
# File 'lib/formulario/fields/form_field.rb', line 24

def exceptional?
  ! form.valid?
end

#fieldsObject



28
29
30
# File 'lib/formulario/fields/form_field.rb', line 28

def fields
  form.fields
end

#valueObject



20
21
22
# File 'lib/formulario/fields/form_field.rb', line 20

def value
  form.params
end