class RR::Syncers::TwoWaySyncer

This syncer implements a two way sync. Syncer options relevant for this syncer:

Example of using a Proc object:

lambda do |sync_helper, type, row|
  # delete records existing only in the left database.
  sync_helper.delete(type, row) if type == :left
end

Constants

TYPE_DESCRIPTIONS

Sync type descriptions that are written into the event log

Attributes

sync_helper[RW]

The current SyncHelper object

Public Class Methods

default_options() click to toggle source

Provides default option for the syncer. Optional. Returns a hash with key => value pairs.

# File lib/rubyrep/syncers/two_way_syncer.rb, line 54
def self.default_options
  {
    :left_record_handling => :insert,
    :right_record_handling => :insert,
    :sync_conflict_handling => :ignore,
    :logged_sync_events => [:ignored_conflicts]
  }
end
new(sync_helper) click to toggle source

Initializes the syncer

Raises an ArgumentError if any of the option in sync_helper.sync_options is invalid.

# File lib/rubyrep/syncers/two_way_syncer.rb, line 100
def initialize(sync_helper)
  validate_left_right_record_handling_option sync_helper.sync_options[:left_record_handling]
  validate_left_right_record_handling_option sync_helper.sync_options[:right_record_handling]
  validate_conflict_handling_option sync_helper.sync_options[:sync_conflict_handling]
  validate_logging_options sync_helper.sync_options[:logged_sync_events]
  
  self.sync_helper = sync_helper
end

Public Instance Methods

log_sync_outcome(type, action, row) click to toggle source

Logs a sync event into the event log table as per configuration options.

  • type: Refer to DirectTableScan#run for a description

  • action: the sync action that is executed (The :left_record_handling / :right_record_handling or :sync_conflict_handling option)

  • row: Refer to DirectTableScan#run for a description

# File lib/rubyrep/syncers/two_way_syncer.rb, line 128
def log_sync_outcome(type, action, row)
  if type == :conflict
    return unless log_option_values.include?(:all_conflicts) or log_option_values.include?(:ignored_conflicts)
    return if action != :ignore and not log_option_values.include?(:all_conflicts)
    row = row[0] # Extract left row from row array
  else
    return unless log_option_values.include?(:all_changes) or log_option_values.include?(:ignored_changes)
    return if action != :ignore and not log_option_values.include?(:all_changes)
  end

  sync_helper.log_sync_outcome row, TYPE_DESCRIPTIONS[type], action
end
sync_difference(type, row) click to toggle source

Called to sync the provided difference. See DirectTableScan#run for a description of the type and row parameters.

# File lib/rubyrep/syncers/two_way_syncer.rb, line 143
def sync_difference(type, row)
  if type == :left or type == :right
    option_key = type == :left ? :left_record_handling : :right_record_handling
    option = sync_helper.sync_options[option_key]
    log_sync_outcome type, option, row unless option.respond_to?(:call)
    if option == :ignore
      # nothing to do
    elsif option == :delete
      sync_helper.delete_record type, sync_helper.tables[type], row
    elsif option == :insert
      target = (type == :left ? :right : :left)
      sync_helper.insert_record target, sync_helper.tables[target], row
    else #option must be a Proc
      option.call sync_helper, type, row
    end
  else
    option = sync_helper.sync_options[:sync_conflict_handling]
    log_sync_outcome type, option, row unless option.respond_to?(:call)
    if option == :ignore
      # nothing to do
    elsif option == :right_wins
      sync_helper.update_record :left, sync_helper.tables[:left], row[1]
    elsif option == :left_wins
      sync_helper.update_record :right, sync_helper.tables[:right], row[0]
    else #option must be a Proc
      option.call sync_helper, type, row
    end
  end
end
validate_conflict_handling_option(option) click to toggle source

Verifies if the given :sync_conflict_handling option is valid. Raises an ArgumentError if option is invalid

# File lib/rubyrep/syncers/two_way_syncer.rb, line 76
def validate_conflict_handling_option(option)
  unless option.respond_to? :call
    unless [:ignore, :right_wins, :left_wins].include? option
      raise ArgumentError.new("#{option.inspect} not a valid :sync_conflict_handling option")
    end
  end
end
validate_left_right_record_handling_option(option) click to toggle source

Verifies if the given :left_record_handling / :right_record_handling option is valid. Raises an ArgumentError if option is invalid

# File lib/rubyrep/syncers/two_way_syncer.rb, line 66
def validate_left_right_record_handling_option(option)
  unless option.respond_to? :call
    unless [:ignore, :delete, :insert].include? option
      raise ArgumentError.new("#{option.inspect} not a valid :left_record_handling / :right_record_handling option")
    end
  end
end
validate_logging_options(options) click to toggle source

Verifies if the given :replication_logging option /options is / are valid. Raises an ArgumentError if invalid

# File lib/rubyrep/syncers/two_way_syncer.rb, line 86
def validate_logging_options(options)
  values = [options].flatten # ensure that I have an array
  values.each do |value|
    unless [:ignored_changes, :all_changes, :ignored_conflicts, :all_conflicts].include? value
      raise ArgumentError.new("#{value.inspect} not a valid :logged_sync_events option")
    end
  end
end

Private Instance Methods

log_option_values() click to toggle source

Returns the :logged_sync_events option values.

# File lib/rubyrep/syncers/two_way_syncer.rb, line 117
def log_option_values
  @log_option_values ||= [sync_helper.sync_options[:logged_sync_events]].flatten
end