class Formulario::Form

Attributes

model[RW]

Public Class Methods

default() click to toggle source
# File lib/formulario/form.rb, line 31
def self.default
  params = __fields.each_with_object({}) { |field, res|
    default_value    = field.last.default
    res[field.first] = default_value.is_a?(Proc) ? nil : default_value
  }

  new(skip_validations: true, **params)
end
for(model, **rest) click to toggle source
# File lib/formulario/form.rb, line 19
def self.for(model, **rest)
  if model
    fields = __fields.keys.each_with_object({}) { |field_name, res|
      res[field_name] = model.send(field_name)
    }

    new(**fields.merge({model: model, **rest}))
  else
    new(**rest)
  end
end
new(skip_validations: false, **params) click to toggle source
# File lib/formulario/form.rb, line 5
def initialize(skip_validations: false, **params)
  self.class.__fields.each do |field_name, **options|
    send("#{field_name}=", params.delete(field_name))
  end

  params.each do |k, v|
    if self.respond_to?("#{k}=")
      send("#{k}=", v)
    end
  end

  __validate! unless skip_validations
end

Private Class Methods

__fields() click to toggle source
# File lib/formulario/form.rb, line 83
def self.__fields
  @field ||= {}
end
field(field_name, field_type=Field, default: Field.type_for(field_type).default) click to toggle source
# File lib/formulario/form.rb, line 87
def self.field(field_name,
               field_type=Field,
               default: Field.type_for(field_type).default)

  __fields[field_name.to_sym] = Field::UnvalidatedField.new(
    field_name: field_name,
    field_type:  Field.type_for(field_type),
    default:    default,
    validators: [],
  )

  define_method("__set_field_#{field_name}") do
    __fields[field_name.to_sym] ||= self.class.__fields[field_name.to_sym].dup
  end
  
  define_method(field_name) do
    send("__set_field_#{field_name}")
    __fields[field_name.to_sym].value
  end

  define_method("set_#{field_name}") do |raw_value|
    return raw_value unless Utils.empty?(raw_value)

    default.is_a?(Proc) ? instance_eval(&default) : default
  end

  define_method("#{field_name}=") do |raw_value|
    send("__set_field_#{field_name}")
    __fields[field_name.to_sym].value =  Field.type_for(field_type).for(
      send("set_#{field_name}", raw_value)
    )
  end
end
validate(field_name, validator=Undefined, message: Undefined, &validation_block) click to toggle source
# File lib/formulario/form.rb, line 121
def self.validate(field_name, validator=Undefined,
                  message: Undefined, &validation_block)
  __fields[field_name]
    .validate(validator, message: message, &validation_block)
end

Public Instance Methods

error_fields() click to toggle source
# File lib/formulario/form.rb, line 59
def error_fields
  @error_fields ||= fields.select { |_, field| field.exceptional? }
end
errors() click to toggle source
# File lib/formulario/form.rb, line 63
def errors
  error_fields.transform_values(&:to_a)
end
fields() click to toggle source
# File lib/formulario/form.rb, line 49
def fields
  @fields ||= __fields.each_with_object({}) { |field, result|
    result[field.first] = field.last.value
  }
end
on_invalid() { || ... } click to toggle source
# File lib/formulario/form.rb, line 72
def on_invalid
  yield unless valid?
  self
end
on_valid() { || ... } click to toggle source
# File lib/formulario/form.rb, line 67
def on_valid
  yield if valid?
  self
end
params() click to toggle source
# File lib/formulario/form.rb, line 40
def params
  __fields.each_with_object({}) { |field_info, res|
    field_name = field_info.first
    options    = field_info.last

    res[field_name] = options.value.value
  }
end
valid?() click to toggle source
# File lib/formulario/form.rb, line 55
def valid?
  __fields.none? { |_, field| field.value.exceptional? }
end

Private Instance Methods

__fields() click to toggle source
# File lib/formulario/form.rb, line 79
def __fields
  @__fields ||= {}
end
__validate!() click to toggle source
# File lib/formulario/form.rb, line 127
def __validate!
  __fields.each do |field_name, unvalidated_field|
    __fields[field_name].value = unvalidated_field.validate!(self)
  end
end