class Terrestrial::RelationMapping
Attributes
associations[R]
created_at_field[R]
database_default_fields[R]
database_owned_fields[R]
factory[R]
fields[R]
name[R]
namespace[R]
observers[R]
primary_key[R]
serializer[R]
subsets[R]
updated_at_field[R]
Public Class Methods
new(name:, namespace:, fields:, database_owned_fields:, database_default_fields:, primary_key:, factory:, serializer:, associations:, subsets:, observers:)
click to toggle source
# File lib/terrestrial/relation_mapping.rb, line 7 def initialize(name:, namespace:, fields:, database_owned_fields:, database_default_fields:, primary_key:, factory:, serializer:, associations:, subsets:, observers:) @name = name @namespace = namespace @fields = fields @database_owned_fields = database_owned_fields @database_default_fields = database_default_fields @primary_key = primary_key @factory = factory @serializer = serializer @associations = associations @subsets = subsets @observers = observers @incoming_foreign_keys = [] end
Public Instance Methods
add_association(name, new_association)
click to toggle source
# File lib/terrestrial/relation_mapping.rb, line 26 def add_association(name, new_association) @associations = associations.merge(name => new_association) end
delete(object, depth)
click to toggle source
# File lib/terrestrial/relation_mapping.rb, line 54 def delete(object, depth) object_attributes = serializer.call(object) [deleted_record(object_attributes, depth)] end
load(record)
click to toggle source
# File lib/terrestrial/relation_mapping.rb, line 34 def load(record) factory.call(reject_non_factory_fields(record)) rescue => e raise LoadError.new(namespace, factory, record, e) end
post_save(object, record, new_attributes)
click to toggle source
# File lib/terrestrial/relation_mapping.rb, line 60 def post_save(object, record, new_attributes) new_record = upsertable_record(object, new_attributes, 0, {}) observers.each { |o| o.post_save(self, object, record, new_record) } record.merge!(new_attributes) end
register_foreign_key(fk)
click to toggle source
# File lib/terrestrial/relation_mapping.rb, line 30 def register_foreign_key(fk) @incoming_foreign_keys += fk end
serialize(object, depth, foreign_keys = {})
click to toggle source
# File lib/terrestrial/relation_mapping.rb, line 40 def serialize(object, depth, foreign_keys = {}) object_attributes = serializer.call(object) record = upsertable_record(object, object_attributes, depth, foreign_keys) observers.each { |o| o.post_serialize(self, object, record) } [ record, extract_associations(object_attributes) ] rescue => e raise SerializationError.new(name, serializer, object, e) end
Private Instance Methods
deleted_record(attributes, depth)
click to toggle source
# File lib/terrestrial/relation_mapping.rb, line 79 def deleted_record(attributes, depth) DeletedRecord.new( self, attributes, depth, ) end
extract_associations(attributes)
click to toggle source
# File lib/terrestrial/relation_mapping.rb, line 87 def extract_associations(attributes) Hash[ associations.map { |name, _association| [ name, attributes.fetch(name) ] } ] end
factory_fields()
click to toggle source
# File lib/terrestrial/relation_mapping.rb, line 103 def factory_fields @factory_fields ||= fields - (local_foreign_keys + @incoming_foreign_keys) end
local_foreign_keys()
click to toggle source
# File lib/terrestrial/relation_mapping.rb, line 107 def local_foreign_keys @local_foreign_keys ||= associations.values.flat_map(&:local_foreign_keys) end
reject_non_factory_fields(attributes)
click to toggle source
# File lib/terrestrial/relation_mapping.rb, line 99 def reject_non_factory_fields(attributes) attributes.reject { |name, _value| (@incoming_foreign_keys + local_foreign_keys).include?(name) } end
select_mapped_fields(attributes)
click to toggle source
# File lib/terrestrial/relation_mapping.rb, line 95 def select_mapped_fields(attributes) attributes.select { |name, _value| fields.include?(name) } end
upsertable_record(object, attributes, depth, foreign_keys)
click to toggle source
# File lib/terrestrial/relation_mapping.rb, line 70 def upsertable_record(object, attributes, depth, foreign_keys) UpsertRecord.new( self, object, select_mapped_fields(attributes).merge(foreign_keys), depth, ) end