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

Inherits:
Object
  • Object
show all
Defined in:
lib/card/content/diff/l_c_s/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/l_c_s/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



90
91
92
93
94
95
# File 'lib/card/content/diff/l_c_s/processor.rb', line 90

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

#del_old_excludeesObject



83
84
85
86
87
88
# File 'lib/card/content/diff/l_c_s/processor.rb', line 83

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/l_c_s/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

#handle_action?(word_action, prev_action) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
# File 'lib/card/content/diff/l_c_s/processor.rb', line 35

def handle_action? word_action, prev_action
  (prev_action == word_action) ||
    (prev_action == "-" && word_action == "!") ||
    (prev_action == "!" && word_action == "+")
end

#interpret_action(prev_actn, word_actn) ⇒ Object



31
32
33
# File 'lib/card/content/diff/l_c_s/processor.rb', line 31

def interpret_action prev_actn, word_actn
  handle_action?(word_actn, prev_actn) ? handle_action(word_actn) : write_all
end

#minus(old_element) ⇒ Object



115
116
117
118
# File 'lib/card/content/diff/l_c_s/processor.rb', line 115

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

#plus(new_element) ⇒ Object



110
111
112
113
# File 'lib/card/content/diff/l_c_s/processor.rb', line 110

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

#process_element(old_element, new_element, action) ⇒ Object



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

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

#process_word(word, prev_action) ⇒ Object



26
27
28
29
# File 'lib/card/content/diff/l_c_s/processor.rb', line 26

def process_word word, prev_action
  prev_action ? interpret_action(prev_action, word.action) : write_excludees
  process_element word.old_element, word.new_element, word.action
end

#run(result) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/card/content/diff/l_c_s/processor.rb', line 15

def run result
  @result = result
  prev_action = nil
  ::Diff::LCS.traverse_balanced(@words[:old], @words[:new]) do |word|
    process_word word, prev_action
    prev_action = word.action
  end
  write_all
  @result
end

#write_addsObject



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

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/l_c_s/processor.rb', line 53

def write_all
  write_dels
  write_adds
  write_excludees
end

#write_delsObject



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

def write_dels
  return if @dels.empty?

  @result.write_deleted_chunk @dels.join
  @dels = []
end

#write_excludeesObject



77
78
79
80
81
# File 'lib/card/content/diff/l_c_s/processor.rb', line 77

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/l_c_s/processor.rb', line 59

def write_unchanged text
  @result.write_unchanged_chunk text
end