Class: Card::Content::Diff::LCS::Processor
- Inherits:
-
Object
- Object
- Card::Content::Diff::LCS::Processor
- Defined in:
- lib/card/content/diff/processor.rb
Overview
Compares two lists of chunks and generates a diff
Instance Method Summary collapse
- #add_new_excludees ⇒ Object
- #del_old_excludees ⇒ Object
- #handle_action(action) ⇒ Object
-
#initialize(old_words, new_words, old_excludees, new_excludees) ⇒ Processor
constructor
A new instance of Processor.
- #interpret_action(prev_action, word) ⇒ Object
- #minus(old_element) ⇒ Object
- #plus(new_element) ⇒ Object
- #process_element(old_element, new_element, action) ⇒ Object
- #run(result) ⇒ Object
- #write_adds ⇒ Object
- #write_all ⇒ Object
- #write_dels ⇒ Object
- #write_excludees ⇒ Object
- #write_unchanged(text) ⇒ Object
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_excludees ⇒ Object
90 91 92 93 94 95 |
# File 'lib/card/content/diff/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_excludees ⇒ Object
83 84 85 86 87 88 |
# File 'lib/card/content/diff/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/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
115 116 117 118 |
# File 'lib/card/content/diff/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/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/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 |
#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_adds ⇒ Object
70 71 72 73 74 75 |
# File 'lib/card/content/diff/processor.rb', line 70 def write_adds return if @adds.empty? @result.write_added_chunk @adds.join @adds = [] end |
#write_all ⇒ Object
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_dels ⇒ Object
63 64 65 66 67 68 |
# 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_excludees ⇒ Object
77 78 79 80 81 |
# File 'lib/card/content/diff/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/processor.rb', line 59 def write_unchanged text @result.write_unchanged_chunk text end |