Class: Formulario::Field::UnvalidatedField

Inherits:
Object
  • Object
show all
Defined in:
lib/formulario/fields/unvalidated_field.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field_name:, value: nil, field_type: Field, default: Field.type_for(field_type).default, validators: []) ⇒ UnvalidatedField

Returns a new instance of UnvalidatedField.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/formulario/fields/unvalidated_field.rb', line 13

def initialize(
      field_name: ,

      value:      nil,
      field_type: Field,
      default:    Field.type_for(field_type).default,
      validators: []
    )


  @field_name = field_name

  @field_type = field_type
  @default    = default
  @validators = validators

end

Instance Attribute Details

#defaultObject

Returns the value of attribute default.



8
9
10
# File 'lib/formulario/fields/unvalidated_field.rb', line 8

def default
  @default
end

#field_nameObject

Returns the value of attribute field_name.



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

def field_name
  @field_name
end

#field_typeObject

Returns the value of attribute field_type.



7
8
9
# File 'lib/formulario/fields/unvalidated_field.rb', line 7

def field_type
  @field_type
end

#formObject

Returns the value of attribute form.



11
12
13
# File 'lib/formulario/fields/unvalidated_field.rb', line 11

def form
  @form
end

#validatorsObject

Returns the value of attribute validators.



9
10
11
# File 'lib/formulario/fields/unvalidated_field.rb', line 9

def validators
  @validators
end

#valueObject

Returns the value of attribute value.



6
7
8
# File 'lib/formulario/fields/unvalidated_field.rb', line 6

def value
  @value
end

Instance Method Details

#validate(validator = Undefined, message: Undefined, &validation_block) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/formulario/fields/unvalidated_field.rb', line 31

def validate(validator=Undefined,
             message: Undefined, &validation_block)

  validation_function = case
                        when block_given?
                          validation_block
                        when validator.is_a?(Symbol)
                          validator
                        when validator.is_a?(::Formulario::Validator)
                          validator
                        end

  validators << {
    validator: validation_function,
    message:   message,
  }
end

#validate!(form) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/formulario/fields/unvalidated_field.rb', line 49

def validate!(form)
  self.form = form

  validators.inject(field_type.for(value.value)) do |current_value, validator_hash|
    validator_block = case validator_hash[:validator]
                      when ::Formulario::Validator
                        validator_hash[:validator]
                      when Symbol
                        ::Formulario::Validator.new(&value.method(validator_hash[:validator]))
                      when Proc
                        ::Formulario::Validator.new(&validator_hash[:validator])
                      end

    if instance_exec(value: value.value, object: self, field_name: field_name, &validator_block.to_proc).valid?
      current_value
    else
      message = if validator_hash[:validator].is_a?(::Formulario::Validator)
                  validator_hash[:validator].message
                end

      value.exceptional_class
        .new(current_value,
             reasons: message || validator_hash[:message])
    end
  end

end