Class: Array

Inherits:
Object show all
Defined in:
lib/strokedb/core_ext/blank.rb,
lib/strokedb/sync/diff/array.rb,
lib/strokedb/util/serialization.rb

Overview

:nodoc:

Constant Summary collapse

SDATPTAGS =
{
  '-' => PATCH_MINUS,
  '+' => PATCH_PLUS,
  '!' => PATCH_DIFF
}.freeze

Instance Method Summary collapse

Instance Method Details

#stroke_diff(to) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/strokedb/sync/diff/array.rb', line 8

def stroke_diff(to)
  return super(to) unless to.is_a?(Array)
  return nil if self == to
  
  # sdiff:  +   -   !   = 
  lcs_sdiff = ::Diff::LCS.sdiff(self, to)
  patchset = lcs_sdiff.inject([]) do |patchset, change|
    a = SDATPTAGS[change.action]
    if a == PATCH_DIFF
      patchset << [a, change.new_position, change.old_element.stroke_diff(change.new_element)]
    elsif a == PATCH_MINUS
      patchset << [a, change.new_position]
    elsif a == PATCH_PLUS
      patchset << [a, change.new_position, change.new_element]
    end
    patchset
  end
  patchset.empty? ? nil : patchset
end

#stroke_merge(patch1, patch2) ⇒ Object



38
39
40
41
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/strokedb/sync/diff/array.rb', line 38

def stroke_merge(patch1, patch2)
  unless patch1 && patch2
    return _stroke_automerged(stroke_patch(patch1 || patch2))
  end
  
  patch1 = patch1.dup
  patch2 = patch2.dup
  
  c1 = patch1.shift
  c2 = patch2.shift
  
  offset1 = 0
  offset2 = 0
  result = self.dup
  result1 = nil
  result2 = nil
  
  while c1 && c2
    while c1 && (p1 = c1[1] + offset1) && (p2 = c2[1] + offset2) && p1 < p2
      offset2 += _stroke_elementary_patch(result, p1, c1)
      c1 = patch1.shift
    end
    
    if p1 == p2
      raise "TODO conflict resolution!"
      
      c1 = patch1.shift
    end
    
    while c1 && c2 && (p1 = c1[1] + offset1) && (p2 = c2[1] + offset2) && p2 < p1
      offset1 += _stroke_elementary_patch(result, p2, c2)
      c2 = patch2.shift
    end
  end

  # Tail (one of two) 
  while c1
    offset2 += _stroke_elementary_patch(result, c1[1] + offset1, c1)
    c1 = patch1.shift
  end
  while c2
    offset1 += _stroke_elementary_patch(result, c2[1] + offset2, c2)
    c2 = patch2.shift
  end
  result ? _stroke_automerged(result) : _stroke_conflicted(result1, result2)
end

#stroke_patch(patch) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/strokedb/sync/diff/array.rb', line 28

def stroke_patch(patch)
  return self unless patch
  return patch[1] if patch[0] == PATCH_REPLACE
  res = self.dup
  patch.each do |change|
    _stroke_elementary_patch(res, change[1], change)
  end
  res
end

#to_rawObject



4
5
6
7
8
# File 'lib/strokedb/util/serialization.rb', line 4

def to_raw
  map do |v|
    v.respond_to?(:to_raw) ? v.to_raw : v
  end
end