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.



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

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



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

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



39
40
41
# File 'lib/diff_tools.rb', line 39

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

#exclude(*args) ⇒ Object



60
61
62
# File 'lib/diff_tools.rb', line 60

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

#grep(*args) ⇒ Object



52
53
54
# File 'lib/diff_tools.rb', line 52

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

#negative(*args) ⇒ Object



56
57
58
# File 'lib/diff_tools.rb', line 56

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

#to_sObject



64
65
66
# File 'lib/diff_tools.rb', line 64

def to_s
  @chunks.values.join
end