Class: Synchronisable::Source

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

Overview

Synchronization source.

Constant Summary collapse

CHILD_ASSOCIATION_KEYS =
%i(has_one has_many)
PARENT_ASSOCIATION_KEYS =
%i(belongs_to)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, parent, includes) ⇒ Source

Returns a new instance of Source.



16
17
18
19
20
# File 'lib/synchronisable/source.rb', line 16

def initialize(model, parent, includes)
  @model, @parent, @synchronizer = model, parent, model.synchronizer
  @model_name = @model.to_s.demodulize.underscore.to_sym
  @includes = includes
end

Instance Attribute Details

#child_associationsObject (readonly)

Returns the value of attribute child_associations.



12
13
14
# File 'lib/synchronisable/source.rb', line 12

def child_associations
  @child_associations
end

#dataObject (readonly)

Returns the value of attribute data.



13
14
15
# File 'lib/synchronisable/source.rb', line 13

def data
  @data
end

#import_idsObject (readonly)

Returns the value of attribute import_ids.



13
14
15
# File 'lib/synchronisable/source.rb', line 13

def import_ids
  @import_ids
end

#import_recordObject

Returns the value of attribute import_record.



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

def import_record
  @import_record
end

#includesObject (readonly)

Returns the value of attribute includes.



13
14
15
# File 'lib/synchronisable/source.rb', line 13

def includes
  @includes
end

#local_attrsObject (readonly)

Returns the value of attribute local_attrs.



13
14
15
# File 'lib/synchronisable/source.rb', line 13

def local_attrs
  @local_attrs
end

#modelObject (readonly)

Returns the value of attribute model.



13
14
15
# File 'lib/synchronisable/source.rb', line 13

def model
  @model
end

#parentObject (readonly)

Returns the value of attribute parent.



13
14
15
# File 'lib/synchronisable/source.rb', line 13

def parent
  @parent
end

#parent_associationsObject (readonly)

Returns the value of attribute parent_associations.



12
13
14
# File 'lib/synchronisable/source.rb', line 12

def parent_associations
  @parent_associations
end

#remote_attrsObject (readonly)

Returns the value of attribute remote_attrs.



13
14
15
# File 'lib/synchronisable/source.rb', line 13

def remote_attrs
  @remote_attrs
end

#remote_idObject (readonly)

Returns the value of attribute remote_id.



13
14
15
# File 'lib/synchronisable/source.rb', line 13

def remote_id
  @remote_id
end

#unique_idObject (readonly)

Returns the value of attribute unique_id.



13
14
15
# File 'lib/synchronisable/source.rb', line 13

def unique_id
  @unique_id
end

Instance Method Details

#dump_messageObject



74
75
76
77
78
79
80
81
# File 'lib/synchronisable/source.rb', line 74

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

#find_importObject



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

def find_import
  (@unique_id.present? && find_import_by_unique_id) ||
    find_import_by_remote_id
end

#find_import_by_remote_idObject



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

def find_import_by_remote_id
  Import.find_by(
    remote_id: @remote_id.to_s,
    synchronisable_type: @model
  )
end

#find_import_by_unique_idObject



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

def find_import_by_unique_id
  Import.find_by(
    unique_id: @unique_id.to_s,
    synchronisable_type: @model
  )
end

#local_recordObject



70
71
72
# File 'lib/synchronisable/source.rb', line 70

def local_record
  @import_record.try(:synchronisable)
end

#prepare(data, remote_attrs) ⇒ Object

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`.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/synchronisable/source.rb', line 26

def prepare(data, remote_attrs)
  @data = @parent
    .try(:source).try(:data)
    .try(:merge, data) || data

  @remote_attrs = remote_attrs.with_indifferent_access

  @remote_id    = @synchronizer.extract_remote_id(@remote_attrs)
  @local_attrs  = @synchronizer.map_attributes(@remote_attrs)
  @unique_id    = @synchronizer.uid(@local_attrs)
  @associations = @synchronizer.associations_for(@local_attrs)

  @parent_associations = filter_associations(PARENT_ASSOCIATION_KEYS)
  @child_associations  = filter_associations(CHILD_ASSOCIATION_KEYS)

  @import_record = find_import

  remove_association_keys_from_local_attrs
end

#updatable?Boolean

Returns:

  • (Boolean)


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

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