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.



14
15
16
17
18
# File 'lib/synchronisable/source.rb', line 14

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.



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

def child_associations
  @child_associations
end

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

#import_idsObject (readonly)

Returns the value of attribute import_ids.



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

def import_ids
  @import_ids
end

#import_recordObject

Returns the value of attribute import_record.



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

def import_record
  @import_record
end

#includesObject (readonly)

Returns the value of attribute includes.



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

def includes
  @includes
end

#local_attrsObject (readonly)

Returns the value of attribute local_attrs.



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

def local_attrs
  @local_attrs
end

#modelObject (readonly)

Returns the value of attribute model.



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

def model
  @model
end

#parentObject (readonly)

Returns the value of attribute parent.



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

def parent
  @parent
end

#parent_associationsObject (readonly)

Returns the value of attribute parent_associations.



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

def parent_associations
  @parent_associations
end

#remote_attrsObject (readonly)

Returns the value of attribute remote_attrs.



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

def remote_attrs
  @remote_attrs
end

#remote_idObject (readonly)

Returns the value of attribute remote_id.



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

def remote_id
  @remote_id
end

#unique_idObject (readonly)

Returns the value of attribute unique_id.



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

def unique_id
  @unique_id
end

Instance Method Details

#dump_messageObject



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

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

#find_importObject



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

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

#find_import_by_remote_idObject



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

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

#find_import_by_unique_idObject



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

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

#local_recordObject



68
69
70
# File 'lib/synchronisable/source.rb', line 68

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



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

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)


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

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