Class: Card::Diff::LCS::ChunkProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/card/diff/lcs.rb

Overview

Compares two lists of chunks and generates a diff

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(old_words, new_words, old_excludees, new_excludees) ⇒ ChunkProcessor

Returns a new instance of ChunkProcessor.



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/card/diff/lcs.rb', line 91

def initialize old_words, new_words, old_excludees, new_excludees
  @adds = []
  @dels = []
  @words = {
    old: old_words,
    new: new_words
  }
  @excludees = {
    old: ExcludeeIterator.new(old_excludees),
    new: ExcludeeIterator.new(new_excludees)
  }
end

Instance Attribute Details

#adds_cntObject (readonly)

Returns the value of attribute adds_cnt.



90
91
92
# File 'lib/card/diff/lcs.rb', line 90

def adds_cnt
  @adds_cnt
end

#dels_cntObject (readonly)

Returns the value of attribute dels_cnt.



90
91
92
# File 'lib/card/diff/lcs.rb', line 90

def dels_cnt
  @dels_cnt
end

#resultObject (readonly)

Returns the value of attribute result.



90
91
92
# File 'lib/card/diff/lcs.rb', line 90

def result
  @result
end

#summaryObject (readonly)

Returns the value of attribute summary.



90
91
92
# File 'lib/card/diff/lcs.rb', line 90

def summary
  @summary
end

Instance Method Details

#run(result) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/card/diff/lcs.rb', line 104

def run result
  @result = result
  prev_action = nil
  ::Diff::LCS.traverse_balanced(@words[:old], @words[:new]) do |word|
    if prev_action
      if prev_action != word.action &&
         !(prev_action == '-' && word.action == '!') &&
         !(prev_action == '!' && word.action == '+')

        # delete and/or add section stops here; write changes to result
        write_dels
        write_adds

        # new neutral section starts
        # we can just write excludees to result
        write_excludees

      else # current word belongs to edit of previous word
        case word.action
        when '-'
          del_old_excludees
        when '+'
          add_new_excludees
        when '!'
          del_old_excludees
          add_new_excludees
        else
          write_excludees
        end
      end
    else
      write_excludees
    end

    process_word word
    prev_action = word.action
  end
  write_dels
  write_adds
  write_excludees

  @result
end