Sha256: d7af37cbfec0984c852f494e3c8f9328eb8aeca097c7dfe88e42018467cd341d

Contents?: true

Size: 1.29 KB

Versions: 2

Compression:

Stored size: 1.29 KB

Contents

module Yaks
  class Format
    # Extension of Hal loosely based on the example by Mike Kelly given at
    # https://gist.github.com/mikekelly/893552
    class Halo < Hal
      register :halo, :json, 'application/halo+json'

      def serialize_resource(resource)
        if resource.forms.any?
          super.merge(_controls: serialize_forms(resource.forms))
        else
          super
        end
      end

      def serialize_forms(forms)
        forms.each_with_object({}) do |form, result|
          result[form.name] = serialize_form(form)
        end
      end

      def serialize_form(form)
        raw = form.to_h_compact
        raw[:href]  = raw.delete(:action) if raw[:action]
        raw[:fields] = form.fields.map(&method(:serialize_form_field))
        raw
      end

      def serialize_form_field(field)
        if field.type == :fieldset
          {
            type: :fieldset,
            fields: field.fields.map(&method(:serialize_form_field))
          }
        else
          field.to_h_compact.each_with_object({}) do |(attr, value), hsh|
            if attr == :options # <option>s of a <select>
              hsh[:options] = value.map(&:to_h_compact) unless value.empty?
            else
              hsh[attr] = value
            end
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
yaks-0.11.0 lib/yaks/format/halo.rb
yaks-0.10.0 lib/yaks/format/halo.rb