Sha256: 79b242923d7cc5d1f6090f1f06b211419f44322139ee6d45af56d04719db7cbd

Contents?: true

Size: 1.74 KB

Versions: 4

Compression:

Stored size: 1.74 KB

Contents

module Yaks
  class Mapper
    class Form
      class Config
        include Attribs.new(
                  name: nil,
                  action: nil,
                  title: nil,
                  method: nil,
                  media_type: nil,
                  fields: [].freeze,
                  if: nil
                )

        Builder = Yaks::Builder.new(self) do
          def_set :action, :title, :method, :media_type
          def_add :field, create: Field::Builder, append_to: :fields
          def_add :fieldset, create: Fieldset, append_to: :fields
          HTML5Forms::INPUT_TYPES.each do |type|
            def_add(type,
                    create: Field::Builder,
                    append_to: :fields,
                    defaults: {type: type})
          end
          def_add :legend, create: Legend, append_to: :fields
          def_add :dynamic, create: DynamicField, append_to: :fields
          def_forward :condition
        end

        # Builder expects a `create' method. Alias to constructor
        def self.create(options)
          new(options)
        end

        # Build up a configuration based on an initial set of
        # attributes, and a configuration block
        def self.build(options = {}, &block)
          Builder.create(options, &block)
        end

        # Build up a configuration based on a config block. Provide an
        # object to be supplied to the block
        def self.build_with_object(object, &block)
          Builder.build(new, object, &block)
        end

        def condition(prc = nil, &blk)
          with(if: prc || blk)
        end

        def to_resource_fields(mapper)
          fields.flat_map do |field|
            field.to_resource_fields(mapper)
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
yaks-0.13.0 lib/yaks/mapper/form/config.rb
yaks-0.12.0 lib/yaks/mapper/form/config.rb
yaks-0.11.0 lib/yaks/mapper/form/config.rb
yaks-0.10.0 lib/yaks/mapper/form/config.rb