class Synchronisable::Source

Synchronization source.

Constants

CHILD_ASSOCIATION_KEYS
PARENT_ASSOCIATION_KEYS

Attributes

child_associations[R]
data[R]
import_ids[R]
import_record[RW]
includes[R]
local_attrs[R]
model[R]
parent[R]
parent_associations[R]
remote_attrs[R]
remote_id[R]
unique_id[R]

Public Class Methods

new(model, parent, includes) click to toggle source
# 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

Public Instance Methods

dump_message() click to toggle source
# 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_import() click to toggle source
# 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_id() click to toggle source
# 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_id() click to toggle source
# 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_record() click to toggle source
# File lib/synchronisable/source.rb, line 68
def local_record
  @import_record.try(:synchronisable)
end
prepare(data, remote_attrs) click to toggle source

Prepares synchronization source: `remote_id`, `local_attributes`, `import_record` and `associations`.

@api private

# 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?() click to toggle source
# File lib/synchronisable/source.rb, line 63
def updatable?
  import_record.present? &&
  local_record.present?
end

Private Instance Methods

filter_associations(kinds) click to toggle source
# File lib/synchronisable/source.rb, line 89
def filter_associations(kinds)
  @associations.select { |a| kinds.include? a.kind }
end
remove_association_keys_from_local_attrs() click to toggle source
# File lib/synchronisable/source.rb, line 83
def remove_association_keys_from_local_attrs
  @local_attrs.delete_if do |key, _|
    @associations.keys.any? { |a| a.key == key }
  end
end