class Yaks::Format::Halo

Extension of Hal loosely based on the example by Mike Kelly given at gist.github.com/mikekelly/893552

Public Instance Methods

serialize_form(form) click to toggle source
# File lib/yaks/format/halo.rb, line 22
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
serialize_form_field(field) click to toggle source
# File lib/yaks/format/halo.rb, line 29
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
serialize_forms(forms) click to toggle source
# File lib/yaks/format/halo.rb, line 16
def serialize_forms(forms)
  forms.each_with_object({}) do |form, result|
    result[form.name] = serialize_form(form)
  end
end
serialize_resource(resource) click to toggle source
Calls superclass method Yaks::Format::Hal#serialize_resource
# File lib/yaks/format/halo.rb, line 8
def serialize_resource(resource)
  if resource.forms.any?
    super.merge(_controls: serialize_forms(resource.forms))
  else
    super
  end
end