Module: Roar::JSON::HAL::Resources

Defined in:
lib/roar/json/hal.rb

Instance Method Summary collapse

Instance Method Details

#from_hash(hash) ⇒ Object


71
72
73
74
# File 'lib/roar/json/hal.rb', line 71

def from_hash(hash, *)
  hash.fetch("_embedded", []).each { |name, fragment| hash[name] = fragment }
  super
end

#to_hashObject


57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/roar/json/hal.rb', line 57

def to_hash(*)
  super.tap do |hash|
    embedded = {}
    representable_attrs.find_all do |dfn|
      name = dfn[:as] ? dfn[:as].(nil) : dfn.name # DISCUSS: should we simplify that in Representable?
      next unless dfn[:embedded] and fragment = hash.delete(name)
      embedded[name] = fragment
    end

    hash["_embedded"] = embedded if embedded.any?
    hash["_links"]    = hash.delete("_links") if hash["_links"] # always render _links after _embedded.
  end
end