Class: Terrestrial::RelationMapping

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, namespace:, fields:, database_owned_fields:, database_default_fields:, primary_key:, factory:, serializer:, associations:, subsets:, observers:) ⇒ RelationMapping

Returns a new instance of RelationMapping.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 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

Instance Attribute Details

#associationsObject (readonly)

Returns the value of attribute associations.



23
24
25
# File 'lib/terrestrial/relation_mapping.rb', line 23

def associations
  @associations
end

#created_at_fieldObject (readonly)

Returns the value of attribute created_at_field.



23
24
25
# File 'lib/terrestrial/relation_mapping.rb', line 23

def created_at_field
  @created_at_field
end

#database_default_fieldsObject (readonly)

Returns the value of attribute database_default_fields.



23
24
25
# File 'lib/terrestrial/relation_mapping.rb', line 23

def database_default_fields
  @database_default_fields
end

#database_owned_fieldsObject (readonly)

Returns the value of attribute database_owned_fields.



23
24
25
# File 'lib/terrestrial/relation_mapping.rb', line 23

def database_owned_fields
  @database_owned_fields
end

#fieldsObject (readonly)

Returns the value of attribute fields.



23
24
25
# File 'lib/terrestrial/relation_mapping.rb', line 23

def fields
  @fields
end

#nameObject (readonly)

Returns the value of attribute name.



23
24
25
# File 'lib/terrestrial/relation_mapping.rb', line 23

def name
  @name
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



23
24
25
# File 'lib/terrestrial/relation_mapping.rb', line 23

def namespace
  @namespace
end

#primary_keyObject (readonly)

Returns the value of attribute primary_key.



23
24
25
# File 'lib/terrestrial/relation_mapping.rb', line 23

def primary_key
  @primary_key
end

#subsetsObject (readonly)

Returns the value of attribute subsets.



23
24
25
# File 'lib/terrestrial/relation_mapping.rb', line 23

def subsets
  @subsets
end

#updated_at_fieldObject (readonly)

Returns the value of attribute updated_at_field.



23
24
25
# File 'lib/terrestrial/relation_mapping.rb', line 23

def updated_at_field
  @updated_at_field
end

Instance Method Details

#add_association(name, new_association) ⇒ Object



26
27
28
# File 'lib/terrestrial/relation_mapping.rb', line 26

def add_association(name, new_association)
  @associations = associations.merge(name => new_association)
end

#delete(object, depth) ⇒ Object



54
55
56
57
58
# 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) ⇒ Object



34
35
36
37
38
# 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) ⇒ Object



60
61
62
63
64
65
66
# 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) ⇒ Object



30
31
32
# File 'lib/terrestrial/relation_mapping.rb', line 30

def register_foreign_key(fk)
  @incoming_foreign_keys += fk
end

#serialize(object, depth, foreign_keys = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# 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