Class: Synchronisable::Source

Inherits:
Object
  • Object
show all
Defined in:
lib/synchronisable/source.rb

Overview

Synchronization source.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, parent, remote_attrs) ⇒ Source



9
10
11
12
# File 'lib/synchronisable/source.rb', line 9

def initialize(model, parent, remote_attrs)
  @model, @parent, @synchronizer = model, parent, model.synchronizer
  @remote_attrs = remote_attrs.with_indifferent_access
end

Instance Attribute Details

#associationsObject (readonly)

Returns the value of attribute associations.



5
6
7
# File 'lib/synchronisable/source.rb', line 5

def associations
  @associations
end

#import_idsObject (readonly)

Returns the value of attribute import_ids.



5
6
7
# File 'lib/synchronisable/source.rb', line 5

def import_ids
  @import_ids
end

#import_recordObject

Returns the value of attribute import_record.



4
5
6
# File 'lib/synchronisable/source.rb', line 4

def import_record
  @import_record
end

#local_attrsObject (readonly)

Returns the value of attribute local_attrs.



5
6
7
# File 'lib/synchronisable/source.rb', line 5

def local_attrs
  @local_attrs
end

#modelObject (readonly)

Returns the value of attribute model.



5
6
7
# File 'lib/synchronisable/source.rb', line 5

def model
  @model
end

#remote_attrsObject (readonly)

Returns the value of attribute remote_attrs.



5
6
7
# File 'lib/synchronisable/source.rb', line 5

def remote_attrs
  @remote_attrs
end

#remote_idObject (readonly)

Returns the value of attribute remote_id.



5
6
7
# File 'lib/synchronisable/source.rb', line 5

def remote_id
  @remote_id
end

Instance Method Details

#create_record_pairObject



49
50
51
52
53
54
55
56
57
# File 'lib/synchronisable/source.rb', line 49

def create_record_pair
  record = @model.create!(@local_attrs)
  @import_record = Import.create!(
    :synchronisable_id    => record.id,
    :synchronisable_type  => @model.to_s,
    :remote_id            => @remote_id,
    :attrs                => @local_attrs
  )
end

#dump_messageObject



63
64
65
66
67
68
69
# File 'lib/synchronisable/source.rb', line 63

def dump_message
  %Q(
    remote id: '#{remote_id}',
    remote attributes: #{remote_attrs},
    local attributes: #{local_attrs}
  )
end

#local_recordObject



59
60
61
# File 'lib/synchronisable/source.rb', line 59

def local_record
  @import_record.try(:synchronisable)
end

#prepareObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Prepares synchronization source: remote_id, local_attributes, import_record and associations. Sets foreign key if current model is specified as has_one or has_many association of parent model.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/synchronisable/source.rb', line 20

def prepare
  @remote_id = @synchronizer.extract_remote_id(@remote_attrs)
  @local_attrs = @synchronizer.map_attributes(@remote_attrs)
  @associations = @synchronizer.associations_for(@local_attrs)

  # TODO: implement destroy_missed, somehow pass @import_ids,
  # get all import records if nil

  # remove associations keys from local attributes
  @local_attrs.delete_if do |key, _|
    @associations.keys.any? { |a| a.key == key }
  end

  @import_record = Import.find_by(
    :remote_id => @remote_id,
    :synchronisable_type => @model
  )

  set_foreign_key
end

#updatable?Boolean



41
42
43
# File 'lib/synchronisable/source.rb', line 41

def updatable?
  @import_record.present? && local_record.present?
end

#update_recordObject



45
46
47
# File 'lib/synchronisable/source.rb', line 45

def update_record
  local_record.update_attributes!(@local_attrs)
end