Class: RR::Syncers::OneWaySyncer

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyrep/syncers/syncers.rb

Overview

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

* +:direction+: Sync direction. Possible values:
  * +:left+
  * +:right+
* +:delete+: Default: false. If true, deletes in the target database all
             records _not_ existing in the source database.
* +:update+: If true (default), update records in the target database
             if different.
* +:insert+: If true (default), copy over records not existing in the
             target database.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sync_helper) ⇒ OneWaySyncer

Initializes the syncer

* sync_helper: The SyncHelper object provided information and utility
               functions.


84
85
86
87
88
89
# File 'lib/rubyrep/syncers/syncers.rb', line 84

def initialize(sync_helper)
  self.sync_helper = sync_helper
  self.source = sync_helper.sync_options[:direction] == :left ? :right : :left
  self.target = sync_helper.sync_options[:direction] == :left ? :left : :right
  self.source_record_index = sync_helper.sync_options[:direction] == :left ? 1 : 0
end

Instance Attribute Details

#sourceObject

ID of source database (either :left or :right)



63
64
65
# File 'lib/rubyrep/syncers/syncers.rb', line 63

def source
  @source
end

#source_record_indexObject

Array index to source row in case #sync_difference type is :conflict. (As in that case the row parameter is an array of left and right records.)



70
71
72
# File 'lib/rubyrep/syncers/syncers.rb', line 70

def source_record_index
  @source_record_index
end

#sync_helperObject

The current SyncHelper object



60
61
62
# File 'lib/rubyrep/syncers/syncers.rb', line 60

def sync_helper
  @sync_helper
end

#targetObject

ID of target database (either :left or :right)



66
67
68
# File 'lib/rubyrep/syncers/syncers.rb', line 66

def target
  @target
end

Class Method Details

.default_optionsObject

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



74
75
76
77
78
79
# File 'lib/rubyrep/syncers/syncers.rb', line 74

def self.default_options
  {
    :direction => :right,
    :delete => false, :update => true, :insert => true
  }
end

Instance Method Details

#sync_difference(type, row) ⇒ Object

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



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/rubyrep/syncers/syncers.rb', line 93

def sync_difference(type, row)
  case type
  when source
    if sync_helper.sync_options[:insert]
      sync_helper.insert_record target, sync_helper.tables[target], row
    end
  when target
    if sync_helper.sync_options[:delete]
      sync_helper.delete_record target, sync_helper.tables[target], row
    end
  when :conflict
    if sync_helper.sync_options[:update]
      sync_helper.update_record target, sync_helper.tables[target], row[source_record_index]
    end
  end
end