class Synchronisable::Synchronizer

@abstract Subclass to create your model specific synchronizer class to

setup synchronization attributes and behavior.

@see Synchronisable::DSL::Macro @see Synchronisable::SynchronizerDefault

Constants

SYMBOL_ARRAY_CONVERTER

Public Class Methods

extract_remote_id(attrs) click to toggle source

Extracts remote id from given attribute hash.

@param attrs [Hash] remote attributes @return remote id value

@raise [MissedRemoteIdError] raised when data doesn't contain remote id @see ensure_remote_id

@api private

# File lib/synchronisable/synchronizer.rb, line 141
def extract_remote_id(attrs)
  id = attrs.delete(remote_id)
  ensure_remote_id(id)
end
fetch(params = {}) click to toggle source
# File lib/synchronisable/synchronizer.rb, line 118
def fetch(params = {})
  data = fetcher.(params)
  data.present? ? data : gateway_instance.try(:fetch, params)
end
find(params) click to toggle source
# File lib/synchronisable/synchronizer.rb, line 123
def find(params)
  data = finder.(params)
  data.present? ? data : gateway_instance.try(:find, params)
end
map_attributes(attrs) click to toggle source

Maps the remote attributes to local model attributes.

@param attrs [Hash] remote attributes @return [Hash] local mapped attributes

@api private

# File lib/synchronisable/synchronizer.rb, line 152
def map_attributes(attrs)
  AttributeMapper.map(attrs, mappings, {
    :only   => only,
    :except => except,
    :keep   => associations.keys
  })
end
uid(attrs) click to toggle source
# File lib/synchronisable/synchronizer.rb, line 128
def uid(attrs)
  unique_id.is_a?(Proc) ? unique_id.(attrs) : attrs[unique_id]
end

Private Class Methods

ensure_remote_id(id) click to toggle source

Throws the {Synchronisable::MissedRemoteIdError} if given id isn't present.

@param id id to check

@raise [MissedRemoteIdError] raised when data doesn't contain remote id

# File lib/synchronisable/synchronizer.rb, line 182
def ensure_remote_id(id)
  return id.to_s if id.present?
  raise MissedRemoteIdError, I18n.t(
    'errors.missed_remote_id',
    remote_id: remote_id
  )
end
gateway_instance() click to toggle source
# File lib/synchronisable/synchronizer.rb, line 190
def gateway_instance
  @gateway_instance ||= gateway.try(:new)
end
run_callbacks(method, args, block) click to toggle source
# File lib/synchronisable/synchronizer.rb, line 168
def run_callbacks(method, args, block)
  before = send(:"before_#{method}")
  after  = send(:"after_#{method}")

  return unless before.(*args) if before
  block.()
  after.(*args) if after
end