Class: Diff

Inherits:
Object show all
Defined in:
lib/taskjuggler/AlgorithmDiff.rb

Overview

This class is an implementation of the classic UNIX diff functionality. It’s based on an original implementation by Lars Christensen, which based his version on the Perl Algorithm::Diff implementation. This is largely a from-scratch implementation that tries to have a less intrusive and more user-friendly interface. But some code fragments are very similar to the original and are copyright © 2001 Lars Christensen.

Defined Under Namespace

Classes: Hunk

Instance Method Summary collapse

Constructor Details

#initialize(a, b) ⇒ Diff

Create a new Diff between the a list and b list.



96
97
98
99
# File 'lib/taskjuggler/AlgorithmDiff.rb', line 96

def initialize(a, b)
  @hunks = []
  diff(a, b)
end

Instance Method Details

#editScriptObject



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/taskjuggler/AlgorithmDiff.rb', line 115

def editScript
  script = []
  @hunks.each do |hunk|
    if hunk.delete?
      script << "#{hunk.aIdx + 1}d#{hunk.deleteValues.length}"
    end
    if hunk.insert?
      script << "#{hunk.bIdx + 1}i#{hunk.insertValues.join(',')}"
    end
  end

  script
end

#inspectObject



136
137
138
# File 'lib/taskjuggler/AlgorithmDiff.rb', line 136

def inspect
  puts to_s
end

#patch(values) ⇒ Object

Modify the values list according to the stored diff information.



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/taskjuggler/AlgorithmDiff.rb', line 102

def patch(values)
  res = values.dup
  @hunks.each do |hunk|
    if hunk.delete?
      res.slice!(hunk.bIdx, hunk.deleteValues.length)
    end
    if hunk.insert?
      res.insert(hunk.bIdx, *hunk.insertValues)
    end
  end
  res
end

#to_sObject

Return the diff list as standard UNIX diff output.



130
131
132
133
134
# File 'lib/taskjuggler/AlgorithmDiff.rb', line 130

def to_s
  str = ''
  @hunks.each { |hunk| str << hunk.to_s }
  str
end