Class: Card::Content::Diff::LCS::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/card/content/diff/processor.rb

Overview

Compares two lists of chunks and generates a diff

Instance Method Summary collapse

Constructor Details

#initialize(old_words, new_words, old_excludees, new_excludees) ⇒ Processor

Returns a new instance of Processor.



7
8
9
10
11
12
13
# File 'lib/card/content/diff/processor.rb', line 7

def initialize old_words, new_words, old_excludees, new_excludees
  @adds = []
  @dels = []
  @words = { old: old_words, new: new_words }
  @excludees =
    ExcludeeIterator.old_and_new old_excludees, new_excludees
end

Instance Method Details

#add_new_excludeesObject



88
89
90
91
92
93
# File 'lib/card/content/diff/processor.rb', line 88

def add_new_excludees
  @excludees[:new].scan_and_record(@adds) do |element|
    write_adds
    @result.complete << element
  end
end

#del_old_excludeesObject



81
82
83
84
85
86
# File 'lib/card/content/diff/processor.rb', line 81

def del_old_excludees
  @excludees[:old].scan_and_record(@dels) do |element|
    write_dels
    @result.write_excluded_chunk element
  end
end

#handle_action(action) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/card/content/diff/processor.rb', line 41

def handle_action action
  case action
  when "-" then del_old_excludees
  when "+" then add_new_excludees
  when "!" then
    del_old_excludees
    add_new_excludees
  else
    write_excludees
  end
end

#interpret_action(prev_action, word) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/card/content/diff/processor.rb', line 31

def interpret_action prev_action, word
  if (prev_action == word.action) ||
     (prev_action == "-" && word.action == "!") ||
     (prev_action == "!" && word.action == "+")
    handle_action word.action
  else
    write_all
  end
end

#minus(old_element) ⇒ Object



113
114
115
116
# File 'lib/card/content/diff/processor.rb', line 113

def minus old_element
  @dels << old_element
  @excludees[:old].word_step
end

#plus(new_element) ⇒ Object



108
109
110
111
# File 'lib/card/content/diff/processor.rb', line 108

def plus new_element
  @adds << new_element
  @excludees[:new].word_step
end

#process_element(old_element, new_element, action) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/card/content/diff/processor.rb', line 95

def process_element old_element, new_element, action
  case action
  when "-" then minus old_element
  when "+" then plus new_element
  when "!"
    minus old_element
    plus new_element
  else
    write_unchanged new_element
    @excludees[:new].word_step
  end
end

#run(result) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/card/content/diff/processor.rb', line 15

def run result
  @result = result
  prev_action = nil
  ::Diff::LCS.traverse_balanced(@words[:old], @words[:new]) do |word|
    if prev_action
      interpret_action prev_action, word
    else
      write_excludees
    end
    process_element word.old_element, word.new_element, word.action
    prev_action = word.action
  end
  write_all
  @result
end

#write_addsObject



69
70
71
72
73
# File 'lib/card/content/diff/processor.rb', line 69

def write_adds
  return if @adds.empty?
  @result.write_added_chunk @adds.join
  @adds = []
end

#write_allObject



53
54
55
56
57
# File 'lib/card/content/diff/processor.rb', line 53

def write_all
  write_dels
  write_adds
  write_excludees
end

#write_delsObject



63
64
65
66
67
# File 'lib/card/content/diff/processor.rb', line 63

def write_dels
  return if @dels.empty?
  @result.write_deleted_chunk @dels.join
  @dels = []
end

#write_excludeesObject



75
76
77
78
79
# File 'lib/card/content/diff/processor.rb', line 75

def write_excludees
  while (ex = @excludees[:new].next)
    @result.write_excluded_chunk ex[:element]
  end
end

#write_unchanged(text) ⇒ Object



59
60
61
# File 'lib/card/content/diff/processor.rb', line 59

def write_unchanged text
  @result.write_unchanged_chunk text
end