Class: Terrestrial::UpsertRecord

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Record

#==, #deep_clone, #empty?, #identity, #identity_fields, #identity_values, #if_delete, #keys, #merge, #merge!, #namespace, #reject, #subset?, #to_h, #updatable?, #updatable_attributes

Constructor Details

#initialize(mapping, object, attributes, depth) ⇒ UpsertRecord

Returns a new instance of UpsertRecord.



5
6
7
8
9
10
# File 'lib/terrestrial/upsert_record.rb', line 5

def initialize(mapping, object, attributes, depth)
  @mapping = mapping
  @object = object
  @attributes = attributes
  @depth = depth
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



12
13
14
# File 'lib/terrestrial/upsert_record.rb', line 12

def attributes
  @attributes
end

#depthObject (readonly)

Returns the value of attribute depth.



12
13
14
# File 'lib/terrestrial/upsert_record.rb', line 12

def depth
  @depth
end

#mappingObject (readonly)

Returns the value of attribute mapping.



12
13
14
# File 'lib/terrestrial/upsert_record.rb', line 12

def mapping
  @mapping
end

#objectObject (readonly)

Returns the value of attribute object.



12
13
14
# File 'lib/terrestrial/upsert_record.rb', line 12

def object
  @object
end

Instance Method Details

#get(name) ⇒ Object



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

def get(name)
  fetch(name)
end

#id?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/terrestrial/upsert_record.rb', line 14

def id?
  identity_values.reject(&:nil?).any?
end

#if_upsert(&block) ⇒ Object



31
32
33
34
# File 'lib/terrestrial/upsert_record.rb', line 31

def if_upsert(&block)
  block.call(self)
  self
end

#include?(field_name) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/terrestrial/upsert_record.rb', line 44

def include?(field_name)
  keys.include?(field_name)
end

#insertableObject



40
41
42
# File 'lib/terrestrial/upsert_record.rb', line 40

def insertable
  to_h.reject { |k, v| v.nil? && identity_fields.include?(k) }
end

#on_upsert(new_attributes) ⇒ Object



36
37
38
# File 'lib/terrestrial/upsert_record.rb', line 36

def on_upsert(new_attributes)
  mapping.post_save(object, self, new_attributes)
end

#set(name, value) ⇒ Object



27
28
29
# File 'lib/terrestrial/upsert_record.rb', line 27

def set(name, value)
  merge!(name => value)
end

#set_id(id) ⇒ Object



18
19
20
21
# File 'lib/terrestrial/upsert_record.rb', line 18

def set_id(id)
  raise "Cannot use #set_id with composite key" if identity_fields.length > 1
  merge!(identity_fields[0] => id)
end