Sha256: b40ea9d69bb00f4b5f95018e67ac80c7c89da87859bcc39850ba3a36f7fdc073

Contents?: true

Size: 1022 Bytes

Versions: 1

Compression:

Stored size: 1022 Bytes

Contents

module Yaks
  class Mapper
    class Control
      include Attributes.new(
                name: nil, href: nil, title: nil, method: nil, media_type: nil, fields: []
              ),
              Configurable

      def self.create(name = nil, options = {})
        new({name: name}.merge(options))
      end

      def add_to_resource(resource, _parent_mapper, _context)
        resource.add_control(
          ::Yaks::Resource::Control.new(to_h.merge(fields: fields.map(&:to_resource_control_field)))
        )
      end

      class Field
        include Attributes.new(:name, label: nil, type: "text", value: nil)

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

        def to_resource_control_field
          Yaks::Resource::Control::Field.new(to_h)
        end
      end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
yaks-0.6.0 lib/yaks/mapper/control.rb