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



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

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_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

#buildObject

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.

Extracts the ‘remote_id` from remote attributes, maps remote attirubtes to local attributes and tries to find import record for given model by extracted remote id.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/synchronisable/source.rb', line 18

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

  @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_parent_attribute
end

#dump_messageObject



43
44
45
46
47
48
49
# File 'lib/synchronisable/source.rb', line 43

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

#local_recordObject



39
40
41
# File 'lib/synchronisable/source.rb', line 39

def local_record
  @import_record.try(:synchronisable)
end

#updatable?Boolean



35
36
37
# File 'lib/synchronisable/source.rb', line 35

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