Class: Db::Sync::Diff
- Inherits:
-
Object
- Object
- Db::Sync::Diff
- Defined in:
- lib/db/sync/diff.rb
Instance Attribute Summary collapse
-
#deletes ⇒ Object
readonly
Returns the value of attribute deletes.
-
#inserts ⇒ Object
readonly
Returns the value of attribute inserts.
-
#original ⇒ Object
Returns the value of attribute original.
-
#pkey ⇒ Object
Returns the value of attribute pkey.
-
#replace_with ⇒ Object
Returns the value of attribute replace_with.
-
#updates ⇒ Object
readonly
Returns the value of attribute updates.
Class Method Summary collapse
- .compare(item1, item2) ⇒ Object
-
.compare_count ⇒ Object
TODO: remove, checks for number of comparisons.
Instance Method Summary collapse
- #check_for_update(original_item, replace_item) ⇒ Object
- #check_items ⇒ Object
- #compare(item1, item2) ⇒ Object
-
#initialize(original, replace_with, pkey) ⇒ Diff
constructor
A new instance of Diff.
- #next_original_item ⇒ Object
- #next_replace_item ⇒ Object
Constructor Details
#initialize(original, replace_with, pkey) ⇒ Diff
Returns a new instance of Diff.
5 6 7 8 9 10 |
# File 'lib/db/sync/diff.rb', line 5 def initialize(original, replace_with, pkey) self.original = original.collect self.replace_with = replace_with.collect self.pkey = pkey check_items end |
Instance Attribute Details
#deletes ⇒ Object (readonly)
Returns the value of attribute deletes.
3 4 5 |
# File 'lib/db/sync/diff.rb', line 3 def deletes @deletes end |
#inserts ⇒ Object (readonly)
Returns the value of attribute inserts.
3 4 5 |
# File 'lib/db/sync/diff.rb', line 3 def inserts @inserts end |
#original ⇒ Object
Returns the value of attribute original.
2 3 4 |
# File 'lib/db/sync/diff.rb', line 2 def original @original end |
#pkey ⇒ Object
Returns the value of attribute pkey.
2 3 4 |
# File 'lib/db/sync/diff.rb', line 2 def pkey @pkey end |
#replace_with ⇒ Object
Returns the value of attribute replace_with.
2 3 4 |
# File 'lib/db/sync/diff.rb', line 2 def replace_with @replace_with end |
#updates ⇒ Object (readonly)
Returns the value of attribute updates.
3 4 5 |
# File 'lib/db/sync/diff.rb', line 3 def updates @updates end |
Class Method Details
.compare(item1, item2) ⇒ Object
70 71 72 73 74 |
# File 'lib/db/sync/diff.rb', line 70 def self.compare(item1, item2) result = item1.values <=> item2.values compare_count result end |
.compare_count ⇒ Object
TODO: remove, checks for number of comparisons
77 78 |
# File 'lib/db/sync/diff.rb', line 77 def self.compare_count end |
Instance Method Details
#check_for_update(original_item, replace_item) ⇒ Object
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/db/sync/diff.rb', line 52 def check_for_update(original_item, replace_item) return if original_item.nil? || replace_item.nil? changes = {} replace_item.each do |key, value| next if value == original_item[key] changes[key] = value end return if changes.blank? @updates << { key: original_item.slice(*pkey), changes: changes } end |
#check_items ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/db/sync/diff.rb', line 24 def check_items @inserts = [] @deletes = [] @updates = [] original.rewind replace_with.rewind original_item = next_original_item replace_item = next_replace_item loop do cmp = compare(original_item, replace_item) if cmp == 0 check_for_update(original_item, replace_item) original_item = next_original_item replace_item = next_replace_item elsif cmp > 0 @inserts << replace_item replace_item = next_replace_item else @deletes << original_item.slice(*pkey) original_item = next_original_item end break if original_item.nil? && replace_item.nil? end nil end |
#compare(item1, item2) ⇒ Object
63 64 65 66 67 68 |
# File 'lib/db/sync/diff.rb', line 63 def compare(item1, item2) return 0 if item1.nil? && item2.nil? return -1 if item2.nil? return 1 if item1.nil? self.class.compare(item1.slice(*pkey), item2.slice(*pkey)) end |
#next_original_item ⇒ Object
12 13 14 15 16 |
# File 'lib/db/sync/diff.rb', line 12 def next_original_item original.next rescue StopIteration nil end |
#next_replace_item ⇒ Object
18 19 20 21 22 |
# File 'lib/db/sync/diff.rb', line 18 def next_replace_item replace_with.next rescue StopIteration nil end |