Class: DiffTools::Diff

Inherits:
Object show all
Defined in:
lib/diff_tools.rb

Instance Method Summary collapse

Constructor Details

#initialize(anObject = nil) ⇒ Diff

Returns a new instance of Diff.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/diff_tools.rb', line 10

def initialize ( anObject=nil )
  @chunks = {}
  @path_list = PathList.new
  case anObject
  when String
    anObject.split(/^Index: /m).each do |chunk|
      self << 'Index: ' + chunk unless chunk.empty?
    end
  when Diff
    merge! anObject
  when Array
    anObject.flatten.each { |chunk| self << chunk }
  when NilClass
  else
    raise TypeError, "Unexpected type #{anObject.class}"
  end
end

Instance Method Details

#<<(chunk) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/diff_tools.rb', line 28

def << ( chunk )
  chunk = Chunk.new(chunk) unless chunk.is_a? Chunk
  if @path_list.include? chunk.path
    raise ArgumentError, "Already have the path `#{chunk.path}'"
  end
  @chunks[chunk.path] = chunk
  @path_list << chunk.path
end

#[](*args) ⇒ Object



37
38
39
# File 'lib/diff_tools.rb', line 37

def [] ( *args )
  grep(*args)
end

#exclude(*args) ⇒ Object



58
59
60
# File 'lib/diff_tools.rb', line 58

def exclude ( *args )
  create @path_list.exclude_with_regex_list(RegexList.new(args))
end

#grep(*args) ⇒ Object



50
51
52
# File 'lib/diff_tools.rb', line 50

def grep ( *args )
  create @path_list.grep_with_regex_list(RegexList.new(args))
end

#negative(*args) ⇒ Object



54
55
56
# File 'lib/diff_tools.rb', line 54

def negative ( *args )
  create @path_list.grep_with_negative_regex_list(RegexList.new(args))
end

#to_sObject



62
63
64
# File 'lib/diff_tools.rb', line 62

def to_s
  @chunks.values.join
end