lib/materialist/materializer/internals/materializer.rb in materialist-3.2.0 vs lib/materialist/materializer/internals/materializer.rb in materialist-3.3.0
- old
+ new
@@ -73,11 +73,11 @@
'url' => root_resource.body._links[key].href,
'type' => 'noop'
})
end
- def mapping
+ def mappings
options.fetch :mapping
end
def before_upsert
options[:before_upsert]
@@ -110,50 +110,18 @@
def source_lookup(url)
@_source_lookup ||= { source_key => url_parser.call(url) }
end
def attributes
- build_attributes root_resource, mapping
+ mappings.map{ |m| m.map(root_resource) }.compact.reduce(&:merge) || {}
end
def root_resource
- @_root_resource ||= resource_at(url)
- end
-
- def build_attributes(resource, mapping)
- return {} unless resource
-
- mapping.inject({}) do |result, m|
- case m
- when FieldMapping
- result.tap { |r| r[m.as] = resource.body[m.key] }
- when LinkHrefMapping
- result.tap do |r|
- if resource.body._links.include?(m.key)
- r[m.as] = resource.body._links[m.key].href
- end
- end
- when LinkMapping
- resource.body._links.include?(m.key) ?
- result.merge(build_attributes(linked_resource(resource, m), m.mapping || [])) :
- result
- else
- result
- end
+ @_root_resource ||= begin
+ api_client.get(url, options: { enable_caching: false })
+ rescue Routemaster::Errors::ResourceNotFound
+ nil
end
- end
-
- def linked_resource(resource, mapping)
- resource_at(resource.send(mapping.key).url, enable_caching: mapping.enable_caching)
- end
-
- def resource_at(url, enable_caching: false)
- api_client.get(url, options: { enable_caching: enable_caching })
- rescue Routemaster::Errors::ResourceNotFound
- # this is due to a race condition between an upsert event
- # and a :delete event
- # when this happens we should silently ignore the case
- nil
end
def api_client
@_api_client ||= Routemaster::APIClient.new(
response_class: Routemaster::Responses::HateoasResponse