Sha256: 15f9f7a73674989bc352f38dc58ef9e7d92e0efad47941da09522f617f516f66

Contents?: true

Size: 1.05 KB

Versions: 4

Compression:

Stored size: 1.05 KB

Contents

module Yaks
  class Mapper
    class Form
      extend Forwardable, Util

      def_delegators :config, :name, :action, :title, :method,
                              :media_type, :fields, :dynamic_blocks

      def self.create(*args, &block)
        args, options = extract_options(args)
        options[:name] = args.first if args.first
        new(config: Config.build(options, &block))
      end

      ############################################################
      # instance

      include Attribs.new(:config)

      def add_to_resource(resource, mapper, _context)
        return resource if config.if && !mapper.expand_value(config.if)
        resource.add_form(to_resource_form(mapper))
      end

      def to_resource_form(mapper)
        attrs = {
          fields: config.to_resource_fields(mapper),
          action: mapper.expand_uri(action)
        }

        [:name, :title, :method, :media_type].each do |attr|
          attrs[attr] = mapper.expand_value(public_send(attr))
        end

        Resource::Form.new(attrs)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

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