Sha256: e4e4db3221a11c3c8ca4735e6ea7c5a402c04f9b1cd232724e18518ebff4262a

Contents?: true

Size: 1.12 KB

Versions: 9

Compression:

Stored size: 1.12 KB

Contents

module Formulario
  class Field
    class FormField < Field
      attr_reader :form

      def self.[](type)
        Class.new(self) do |typed_class|
          typed_class.type = type
        end
      end

      def self.type
        @type
      end

      def self.default
        new(type.default)
      end

      def value
        form.params
      end

      def exceptional?
        ! form.valid?
      end

      def fields
        form.fields
      end

      def [](value)
        fields[value]
      end

      def initialize(raw_value)
        @form = if raw_value.is_a?(::Formulario::Form)
                  raw_value
                else
                  self.class.type.new(**raw_value)
                end

        super
      end

      private

      def self.build(raw_value)
        raise UndefinedFormFieldType.new unless @type

        new(raw_value)
      end

      def self.type=(new_type)
        @type = new_type
      end

      def method_missing(name, *args, &block)
        return fields[name] if fields.has_key?(name)

        super
      end

      UndefinedFormFieldType = Class.new(RuntimeError)
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
formulario-0.1.12 lib/formulario/fields/form_field.rb
formulario-0.1.11 lib/formulario/fields/form_field.rb
formulario-0.1.10 lib/formulario/fields/form_field.rb
formulario-0.1.9 lib/formulario/fields/form_field.rb
formulario-0.1.8 lib/formulario/fields/form_field.rb
formulario-0.1.6 lib/formulario/fields/form_field.rb
formulario-0.1.5 lib/formulario/fields/form_field.rb
formulario-0.1.4 lib/formulario/fields/form_field.rb
formulario-0.1.3 lib/formulario/fields/form_field.rb