Class: Yaks::Format::Halo

Inherits:
Hal show all
Defined in:
lib/yaks/format/halo.rb

Overview

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

Instance Attribute Summary

Attributes inherited from Yaks::Format

#env

Instance Method Summary collapse

Methods inherited from Hal

#inverse, #transitive?

Methods inherited from Yaks::Format

all, by_accept_header, by_media_type, by_name, #call, #initialize, media_types, names, register

Methods included from Util::Deprecated

#deprecated_alias

Methods included from Util

#Resolve, #camelize, #extract_options, #reject_keys, #slice_hash, #symbolize_keys, #underscore

Methods included from Yaks::FP::Callable

#to_proc

Constructor Details

This class inherits a constructor from Yaks::Format

Instance Method Details

#serialize_form(form) ⇒ Object



22
23
24
25
26
27
# 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) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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) ⇒ Object



16
17
18
19
20
# 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) ⇒ Object



8
9
10
11
12
13
14
# 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