class Card::Content::Diff::LCS::Processor

Compares two lists of chunks and generates a diff

Public Class Methods

new(old_words, new_words, old_excludees, new_excludees) click to toggle source
# 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

Public Instance Methods

add_new_excludees() click to toggle source
# 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_excludees() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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_adds() click to toggle source
# 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_all() click to toggle source
# File lib/card/content/diff/l_c_s/processor.rb, line 55
def write_all
  write_dels
  write_adds
  write_excludees
end
write_dels() click to toggle source
# 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_excludees() click to toggle source
# 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) click to toggle source
# File lib/card/content/diff/l_c_s/processor.rb, line 61
def write_unchanged text
  @result.write_unchanged_chunk text
end