Module: Garage::HypermediaResponder

Included in:
AppResponder
Defined in:
lib/garage/hypermedia_responder.rb

Defined Under Namespace

Classes: DataRenderer, Representation

Instance Method Summary collapse

Instance Method Details

#display(resource, given_options = {}) ⇒ Object



5
6
7
8
# File 'lib/garage/hypermedia_responder.rb', line 5

def display(resource, given_options={})
  given_options[:content_type] = representation.content_type if representation.dictionary?
  super(render(transform(resource)), given_options)
end

#encode_to_hash(resource, *args) ⇒ Object

Cache resource encoding result if the resource can be identified. Garage try to get resource identifier by acccess the resource ‘#resource_identifier` or `#id`.



25
26
27
28
29
30
31
32
33
34
# File 'lib/garage/hypermedia_responder.rb', line 25

def encode_to_hash(resource, *args)
  if id = get_resource_identifier(resource)
    options = args[0] || {}
    selector = options[:selector] || controller.field_selector
    cache_key = "#{resource.class.name}:#{id}:#{selector.canonical}"
    cache[cache_key] ||= _encode_to_hash(resource, *args)
  else
    _encode_to_hash(resource, *args)
  end
end

#render(data) ⇒ Object



10
11
12
# File 'lib/garage/hypermedia_responder.rb', line 10

def render(data)
  DataRenderer.render(data, dictionary: representation.dictionary?)
end

#transform(resources) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/garage/hypermedia_responder.rb', line 14

def transform(resources)
  if resources.respond_to?(:map) && resources.respond_to?(:to_a)
    resources.map {|resource| encode_to_hash(resource, partial: true) }
  else
    encode_to_hash(resources)
  end
end