Class: Terrestrial::GraphSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/terrestrial/graph_serializer.rb

Instance Method Summary collapse

Constructor Details

#initialize(mappings:) ⇒ GraphSerializer

Returns a new instance of GraphSerializer.



3
4
5
6
# File 'lib/terrestrial/graph_serializer.rb', line 3

def initialize(mappings:)
  @mappings = mappings
  @serialization_map = {}
end

Instance Method Details

#call(mapping_name, object, depth = 0, parent_foreign_keys = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/terrestrial/graph_serializer.rb', line 11

def call(mapping_name, object, depth = 0, parent_foreign_keys = {})
  if serialization_map.include?(object)
    return [serialization_map.fetch(object)]
  end

  mapping = mappings.fetch(mapping_name)

  current_record, association_fields = mapping.serialize(
    object,
    depth,
    parent_foreign_keys,
  )

  serialization_map.store(object, current_record)

  (
    [current_record] + associated_records(mapping, current_record, association_fields, depth)
  ).flatten(1)
end