Sha256: 48ed8f7f293124ab13c2fda85d6480f6caa702a0e42ab60cc0973cb58c665692

Contents?: true

Size: 1.37 KB

Versions: 2

Compression:

Stored size: 1.37 KB

Contents

module WordRunDiffLCS
  
  class WordRunContextChange
    attr_reader :action, :old_element, :new_element

    def initialize(change_context)
      @action = change_context.action
      @old_element = change_context.old_element
      @new_element = change_context.new_element
    end

    def merge!(change_context)
      return false unless merge_possible_with?(change_context)
      @old_element << " #{change_context.old_element}" if change_context.old_element
      @new_element << " #{change_context.new_element}" if change_context.new_element
      true
    end

    private

    def merge_possible_with?(other)
      return true if action == other.action
      return true if action == "!" && other.action != "="
      false
    end
  end

  class WordRunContextChangeSequence
    def initialize
      @word_run_context_changes = [ ]
    end

    def <<(diff_lcs_context_change)
      word_run_context_change = WordRunContextChange.new(diff_lcs_context_change)

      if empty?
        @word_run_context_changes << word_run_context_change
      else
        @word_run_context_changes << word_run_context_change unless last.merge!(word_run_context_change)
      end
    end
    def map(&block)
      @word_run_context_changes.map(&block)
    end

    private

    def empty?
      @word_run_context_changes.empty?
    end

    def last
      @word_run_context_changes.last
    end
  end

end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
monkeyhelper-diffrenderer-0.0.0 lib/word_run_diff_lcs.rb
diffrenderer-0.0.3 lib/word_run_diff_lcs.rb