class Terrestrial::GraphSerializer

Attributes

mappings[R]
serialization_map[R]

Public Class Methods

new(mappings:) click to toggle source
# File lib/terrestrial/graph_serializer.rb, line 3
def initialize(mappings:)
  @mappings = mappings
  @serialization_map = {}
end

Public Instance Methods

call(mapping_name, object, depth = 0, parent_foreign_keys = {}) click to toggle source
# 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

Private Instance Methods

associated_records(mapping, current_record, association_fields, depth) click to toggle source
# File lib/terrestrial/graph_serializer.rb, line 33
def associated_records(mapping, current_record, association_fields, depth)
  mapping
    .associations
    .map { |name, association|
      dump_association(
        association,
        current_record,
        association_fields.fetch(name),
        depth,
      )
    }
end
delete(mapping_name, object, depth, _foreign_key) click to toggle source
# File lib/terrestrial/graph_serializer.rb, line 71
def delete(mapping_name, object, depth, _foreign_key)
  mapping = mappings.fetch(mapping_name)
  mapping.delete(object, depth)
end
deleted_nodes(association, current_record, collection, depth) click to toggle source
# File lib/terrestrial/graph_serializer.rb, line 64
def deleted_nodes(association, current_record, collection, depth)
  nodes = get_deleted(collection)
  association.delete(current_record, nodes, depth) { |assoc_mapping_name, assoc_object, foreign_key, assoc_depth|
    delete(assoc_mapping_name, assoc_object, assoc_depth, foreign_key)
  }
end
dump_association(association, current_record, collection, depth) click to toggle source
# File lib/terrestrial/graph_serializer.rb, line 46
def dump_association(association, current_record, collection, depth)
  updated_nodes_recursive(association, current_record, collection, depth) + 
    deleted_nodes(association, current_record, collection, depth)
end
get_deleted(collection) click to toggle source
# File lib/terrestrial/graph_serializer.rb, line 90
def get_deleted(collection)
  if collection.respond_to?(:each_deleted)
    collection.each_deleted
  else
    []
  end
end
get_loaded(collection) click to toggle source
# File lib/terrestrial/graph_serializer.rb, line 76
def get_loaded(collection)
  if collection.respond_to?(:each_loaded)
    collection.each_loaded
  elsif collection.is_a?(Struct)
    [collection]
  elsif collection.respond_to?(:each)
    collection.each
  elsif collection.nil?
    [nil]
  else
    [collection]
  end
end
recurse(current_record, association, assoc_mapping_name, assoc_object, assoc_depth, foreign_key) click to toggle source
# File lib/terrestrial/graph_serializer.rb, line 57
def recurse(current_record, association, assoc_mapping_name, assoc_object, assoc_depth, foreign_key)
  (assoc_object && call(assoc_mapping_name, assoc_object, assoc_depth, foreign_key))
    .tap { |associated_record, *_join_records|
      current_record.merge!(association.extract_foreign_key(associated_record))
    }
end
updated_nodes_recursive(association, current_record, collection, depth) click to toggle source
# File lib/terrestrial/graph_serializer.rb, line 51
def updated_nodes_recursive(association, current_record, collection, depth)
  association.dump(current_record, get_loaded(collection), depth) { |assoc_mapping_name, assoc_object, pass_down_foreign_key, assoc_depth|
    recurse(current_record, association, assoc_mapping_name, assoc_object, assoc_depth, pass_down_foreign_key)
  }
end