Sha256: 8506cb1398a3b2f7eac9e02a55fd1ea558a6a6f76802f5af1a69f033255fecd7

Contents?: true

Size: 1.68 KB

Versions: 1

Compression:

Stored size: 1.68 KB

Contents

module Formulario
  class Field
    TYPES = {
      String      => Formulario::Field::String,
      :string     => Formulario::Field::String,
      Integer     => Formulario::Field::Integer,
      :integer    => Formulario::Field::Integer,
      Boolean     => Formulario::Field::Boolean,
      :boolean    => Formulario::Field::Boolean,
      Time        => Formulario::Field::Time,
      :time       => Formulario::Field::Time,
      Date        => Formulario::Field::Date,
      :date       => Formulario::Field::Date,
      Collection  => Formulario::Field::Collection,
      :collection => Formulario::Field::Collection,
      :array      => Formulario::Field::Collection,
    }

    attr_reader :raw_value

    def self.for(raw_value)
      if raw_value.is_a?(Field)
        raw_value
      elsif ::Formulario::Utils.empty?(raw_value)
        default
      else
        build(raw_value)
      end
    end

    def self.type_for(field_type)
      type = TYPES.fetch(field_type) { field_type }
      if type < ::Formulario::Form
        FormField[type]
      else
        type
      end
    end

    def self.default
      Blank.new
    end

    def exceptional?
      false
    end

    def blank?
      false
    end

    def value
      raw_value
    end

    def to_s
      value.to_s
    end
    alias :to_str :to_s

    def inspect
      "#{self.class}[#{to_s}]"
    end

    def ==(other)
      new_other = Field.for(other)
      value == new_other.value
    end

    def exceptional_class
      ::Formulario::Field::ExceptionalValue
    end

    private

    def self.build(raw_value)
      new(raw_value)
    end

    def initialize(raw_value)
      @raw_value = raw_value
      freeze
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
formulario-0.1.8 lib/formulario/fields/field.rb