Class: Roar::JSON::HAL::Links::Representer

Inherits:
Representable::Decorator
  • Object
show all
Includes:
Representable::JSON::Collection
Defined in:
lib/roar/json/hal.rb

Overview

Represents all links for “_links”: [Hyperlink, [Hyperlink, Hyperlink]]

Instance Method Summary collapse

Instance Method Details

#from_hash(hash, *args) ⇒ Object


152
153
154
155
156
157
158
# File 'lib/roar/json/hal.rb', line 152

def from_hash(hash, *args)
  collection = hash.collect do |rel, value| # "self" => [{"href": "//"}, ] or "self" => {"href": "//"}
    value.is_a?(Array) ? value.collect { |link| link.merge("rel"=>rel) } : value.merge("rel"=>rel)
  end

  super(collection) # [{rel=>self, href=>//}, ..] or {rel=>self, href=>//}
end

#to_hash(options) ⇒ Object


146
147
148
149
150
# File 'lib/roar/json/hal.rb', line 146

def to_hash(options)
  links = {}
  super.each { |hash| links.merge!(hash) } # [{ rel=>{}, rel=>[{}, {}] }]
  links
end