Class: Materialist::Materializer::Internals::LinkMapping

Inherits:
Object
  • Object
show all
Defined in:
lib/materialist/materializer/internals/link_mapping.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key:, mapping: [], enable_caching: false) ⇒ LinkMapping

Returns a new instance of LinkMapping.



5
6
7
8
9
# File 'lib/materialist/materializer/internals/link_mapping.rb', line 5

def initialize(key:, mapping: [], enable_caching: false)
  @key = key
  @mapping = mapping
  @enable_caching = enable_caching
end

Instance Attribute Details

#mappingObject (readonly)

Returns the value of attribute mapping.



11
12
13
# File 'lib/materialist/materializer/internals/link_mapping.rb', line 11

def mapping
  @mapping
end

Instance Method Details

#linked_resource(resource) ⇒ Object



18
19
20
21
22
23
# File 'lib/materialist/materializer/internals/link_mapping.rb', line 18

def linked_resource(resource)
  return unless href = resource.dig(:_links, @key, :href)
  resource.client.get(href, options: { enable_caching: @enable_caching, response_class: HateoasResource })
rescue Routemaster::Errors::ResourceNotFound
  nil
end

#map(resource) ⇒ Object



13
14
15
16
# File 'lib/materialist/materializer/internals/link_mapping.rb', line 13

def map(resource)
  return unless linked_resource = linked_resource(resource)
  mapping.map{ |m| m.map(linked_resource) }.compact.reduce(&:merge)
end