Class: Formulario::Field::UnvalidatedField
- Inherits:
-
Object
- Object
- Formulario::Field::UnvalidatedField
- Defined in:
- lib/formulario/fields/unvalidated_field.rb
Instance Attribute Summary collapse
-
#default ⇒ Object
Returns the value of attribute default.
-
#field_name ⇒ Object
Returns the value of attribute field_name.
-
#field_type ⇒ Object
Returns the value of attribute field_type.
-
#form ⇒ Object
Returns the value of attribute form.
-
#validators ⇒ Object
Returns the value of attribute validators.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
-
#initialize(field_name:, value: nil, field_type: Field, default: Field.type_for(field_type).default, validators: []) ⇒ UnvalidatedField
constructor
A new instance of UnvalidatedField.
- #validate(validator = Undefined, message: Undefined, &validation_block) ⇒ Object
- #validate!(form) ⇒ Object
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
#default ⇒ Object
Returns the value of attribute default.
8 9 10 |
# File 'lib/formulario/fields/unvalidated_field.rb', line 8 def default @default end |
#field_name ⇒ Object
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_type ⇒ Object
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 |
#form ⇒ Object
Returns the value of attribute form.
11 12 13 |
# File 'lib/formulario/fields/unvalidated_field.rb', line 11 def form @form end |
#validators ⇒ Object
Returns the value of attribute validators.
9 10 11 |
# File 'lib/formulario/fields/unvalidated_field.rb', line 9 def validators @validators end |
#value ⇒ Object
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: , } 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 = if validator_hash[:validator].is_a?(::Formulario::Validator) validator_hash[:validator]. end value.exceptional_class .new(current_value, reasons: || validator_hash[:message]) end end end |