Sha256: 9498a6c44f173dd05781770ebb6531f61ff26ecd646ee170f13b15d826992ad7

Contents?: true

Size: 1.71 KB

Versions: 4

Compression:

Stored size: 1.71 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)
        Blank.new
      else
        build(raw_value)
      end
    end

    def self.type_for(field_type)
      type_for = TYPES.fetch(field_type) { field_type }
      if type_for < ::Formulario::Form
        FormField[type_for]
      else
        type_for
      end
    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

    private

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

    def initialize(raw_value)
      @raw_value = raw_value
      freeze
    end

    # TODO: Extract field types
    # TODO: Add
    #       Date
    #       Time
    #       DateTime
    #       Boolean
    #       Boolean
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
formulario-0.1.2 lib/formulario/fields/field.rb
formulario-0.1.1b lib/formulario/fields/field.rb
formulario-0.1.1 lib/formulario/fields/field.rb
formulario-0.1.0 lib/formulario/fields/field.rb