Sha256: 283235d85939f91b44a79abc1d01d1fab7d686b455a194039834350319fdb932

Contents?: true

Size: 1.09 KB

Versions: 1

Compression:

Stored size: 1.09 KB

Contents

# -*- coding: utf-8 -*-

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
        raw[:href]  = raw.delete(:action)
        raw[:fields] = form.fields.map do |field|
          field.to_h.each_with_object({}) do |(attr,value), hsh|
            if attr == :options
              if !value.empty?
                hsh[:options] = value.map(&:to_h)
              end
            elsif HTML5Forms::FIELD_OPTIONS[attr] != value
              hsh[attr] = value
            end
          end
        end
        raw
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
yaks-0.7.7 lib/yaks/format/halo.rb