Class: StrokeDB::DefaultSlotDiff

Inherits:
SlotDiffStrategy show all
Defined in:
lib/strokedb/sync/diff.rb

Class Method Summary collapse

Class Method Details

.diff(from, to) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/strokedb/sync/diff.rb', line 17

def self.diff(from, to)
  unless from.class == to.class # if value types are not the same
    to # then return new value
  else
    case to
    when /@##{UUID_RE}/, /@##{UUID_RE}.#{VERSION_RE}/
      to
    when Array, String
      ::Diff::LCS.diff(from, to).map do |d|
        d.map do |change|  
          change.to_a 
        end
      end
    when Hash
      ::Diff::LCS.diff(from.sort_by{|e| e.to_s}, to.sort_by{|e| e.to_s}).map do |d|
        d.map do |change|
          [change.to_a.first, {change.to_a.last.first => change.to_a.last.last}]
        end
      end
    else
      to 
    end
  end
end

.patch(from, patch) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/strokedb/sync/diff.rb', line 42

def self.patch(from, patch)
  case from
  when /@##{UUID_RE}/, /@##{UUID_RE}.#{VERSION_RE}/
    patch
  when String, Array
    lcs_patch = patch.map do |d|
      d.map do |change|
        ::Diff::LCS::Change.from_a(change)
      end
    end
    ::Diff::LCS.patch!(from, lcs_patch)
  when Hash
    lcs_patch = patch.map do |d|
      d.map_with_index do |change, index|
        ::Diff::LCS::Change.from_a([change.first, index, [change.last.keys.first, change.last.values.first]])
      end
    end
    diff = ::Diff::LCS.patch!(from.sort_by{|e| e.to_s}, lcs_patch)
    hash = {}
    diff.each do |v|
      hash[v.first] = v.last
    end
    hash
  else
    patch
  end
end