class HttpApiTools::Sideloading::RelationSideloader
Attributes
has_manys[R]
has_ones[R]
identity_map[R]
relation_includes[R]
result[R]
serializable[R]
type_key_resolver[R]
Public Class Methods
new(opts = {})
click to toggle source
# File lib/http_api_tools/sideloading/relation_sideloader.rb, line 5 def initialize(opts = {}) @serializable = opts[:serializable] @has_ones = opts[:has_ones] @has_manys = opts[:has_manys] @relation_includes = opts[:relation_includes] @result = opts[:result] @identity_map = opts[:identity_map] @type_key_resolver = opts[:type_key_resolver] end
Public Instance Methods
as_json()
click to toggle source
# File lib/http_api_tools/sideloading/relation_sideloader.rb, line 20 def as_json identity_map.to_hash.inject({}) do |sideload_data, (key, type_map)| sideload_data[key] = type_map.values sideload_data end end
sideload_relations()
click to toggle source
# File lib/http_api_tools/sideloading/relation_sideloader.rb, line 15 def sideload_relations sideload_has_ones sideload_has_manys end
Private Instance Methods
relation_for(attr_name)
click to toggle source
# File lib/http_api_tools/sideloading/relation_sideloader.rb, line 56 def relation_for(attr_name) serializable.send(attr_name) if relation_includes.includes_relation?(attr_name) end
serializer_class_for(serializable)
click to toggle source
# File lib/http_api_tools/sideloading/relation_sideloader.rb, line 72 def serializer_class_for(serializable) serializer_class = HttpApiTools::SerializerRegistry.instance.get(:sideloading, serializable.class) serializer_class || raise("No Serializer found for #{serializable.class}") end
sideload_has_manys()
click to toggle source
# File lib/http_api_tools/sideloading/relation_sideloader.rb, line 41 def sideload_has_manys has_manys.each do |attr_name| if related_items = relation_for(attr_name) type_key = nil related_items.each do |related_item| type_key ||= type_key_for(related_item) sideload_item(related_item, attr_name, type_key) unless identity_map.get(type_key, related_item.id) end end end end
sideload_has_ones()
click to toggle source
# File lib/http_api_tools/sideloading/relation_sideloader.rb, line 31 def sideload_has_ones has_ones.each do |attr_name| if related_item = relation_for(attr_name) type_key = type_key_for(related_item) sideload_item(related_item, attr_name, type_key) unless identity_map.get(type_key, related_item.id) end end end
sideload_item(related, attr_name, type_key)
click to toggle source
# File lib/http_api_tools/sideloading/relation_sideloader.rb, line 60 def sideload_item(related, attr_name, type_key) serializer_class = serializer_class_for(related) includes = relation_includes.nested_includes_for(attr_name) || [] sideloaded_hash = serializer_class.new(related, { result: result, identity_map: identity_map, type_key_resolver: type_key_resolver }).includes(*includes).as_sideloaded_hash identity_map.put(type_key, related.id, sideloaded_hash) end
type_key_for(related)
click to toggle source
# File lib/http_api_tools/sideloading/relation_sideloader.rb, line 68 def type_key_for(related) type_key_resolver.for_class(related.class) end