Sha256: 9773f456321f7693f4304815db3585cbe0e5d9f2739378858711edd9da1b92ec

Contents?: true

Size: 1.72 KB

Versions: 1

Compression:

Stored size: 1.72 KB

Contents

module Yaks
  class Mapper
    class Form
      class Field
        extend Configurable
        include Attributes.new(
                  :name,
                  label: nil,
                  options: []
                ).add(HTML5Forms::FIELD_OPTIONS)

        Builder = StatefulBuilder.new(self, attributes.names)

        def self.create(*args)
          attrs = args.last.instance_of?(Hash) ? args.pop : {}
          if name = args.shift
            attrs = attrs.merge(name: name)
          end
          new(attrs)
        end

        # Convert to a Resource::Form::Field, expanding any dynamic
        # values
        def to_resource(mapper)
          Resource::Form::Field.new(
            resource_attributes.each_with_object({}) do |attr, attrs|
              attrs[attr] = mapper.expand_value(public_send(attr))
            end.merge(options: options.map(&:to_resource))
          )
        end

        # All attributes that can be converted 1-to-1 to
        # Resource::Form::Field
        def resource_attributes
          self.class.attributes.names - [:options]
        end

        # <option>, as used in a <select>
        class Option
          include Attributes.new(:value, :label, selected: false)

          def self.create(value, opts = {})
            new(opts.merge(value: value))
          end

          def to_resource
            to_h #placeholder
          end
        end

        config_method :option, create: Option, append_to: :options
      end #Field

      config_method :field, create: Field::Builder, append_to: :fields

      HTML5Forms::INPUT_TYPES.each do |type|
        config_method type, create: Field::Builder, append_to: :fields, defaults: { type: type }
      end

    end # Form
  end # Mapper
end # Yaks

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
yaks-0.7.7 lib/yaks/mapper/form/field.rb