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.



9
10
11
12
13
14
15
# File 'lib/card/content/diff/l_c_s/processor.rb', line 9

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



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

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

#del_old_excludeesObject



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

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



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

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

#handle_action?(word_action, prev_action) ⇒ Boolean

Returns:

  • (Boolean)


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

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



33
34
35
# File 'lib/card/content/diff/l_c_s/processor.rb', line 33

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

#minus(old_element) ⇒ Object



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

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

#plus(new_element) ⇒ Object



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

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

#process_element(old_element, new_element, action) ⇒ Object



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

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



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

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



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

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



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

def write_adds
  return if @adds.empty?

  @result.write_added_chunk @adds.join
  @adds = []
end

#write_allObject



55
56
57
58
59
# File 'lib/card/content/diff/l_c_s/processor.rb', line 55

def write_all
  write_dels
  write_adds
  write_excludees
end

#write_delsObject



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

def write_dels
  return if @dels.empty?

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

#write_excludeesObject



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

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

#write_unchanged(text) ⇒ Object



61
62
63
# File 'lib/card/content/diff/l_c_s/processor.rb', line 61

def write_unchanged text
  @result.write_unchanged_chunk text
end