Class: Differ::Diff

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

Instance Method Summary collapse

Constructor Details

#initializeDiff

Returns a new instance of Diff.



3
4
5
# File 'lib/differ/diff.rb', line 3

def initialize
  @raw = []
end

Instance Method Details

#==(other) ⇒ Object



66
67
68
# File 'lib/differ/diff.rb', line 66

def ==(other)
  @raw == other.raw_array
end

#delete(*str) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/differ/diff.rb', line 34

def delete(*str)
  return if str.empty?
  if @raw.last.is_a? Change
    change = @raw.pop
    if change.insert? && @raw.last
      @raw.last << sep if change.insert.sub!(/^#{Regexp.quote(sep)}/, '')
    end
    change.delete << sep if change.delete?
  else
    change = Change.new(:delete => @raw.empty? ? '' : sep)
  end

  @raw << change
  @raw.last.delete << str.join(sep)
end

#format_as(f) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/differ/diff.rb', line 74

def format_as(f)
  f = Differ.format_for(f)
  @raw.inject('') do |sum, part|
    part = case part
    when String then part
    when Change then f.format(part)
    end
    sum << part
  end
end

#insert(*str) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/differ/diff.rb', line 50

def insert(*str)
  return if str.empty?
  if @raw.last.is_a? Change
    change = @raw.pop
    if change.delete? && @raw.last
      @raw.last << sep if change.delete.sub!(/^#{Regexp.quote(sep)}/, '')
    end
    change.insert << sep if change.insert?
  else
    change = Change.new(:insert => @raw.empty? ? '' : sep)
  end

  @raw << change
  @raw.last.insert << str.join(sep)
end

#same(*str) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/differ/diff.rb', line 7

def same(*str)
  return if str.empty?
  if @raw.last.is_a? String
    @raw.last << sep
  elsif @raw.last.is_a? Change
    if @raw.last.change?
      @raw << sep
    else
      change = @raw.pop
      if change.insert? && @raw.last
        @raw.last << sep if change.insert.sub!(/^#{Regexp.quote(sep)}/, '')
      end
      if change.delete? && @raw.last
        @raw.last << sep if change.delete.sub!(/^#{Regexp.quote(sep)}/, '')
      end
      @raw << change

      @raw.last.insert << sep if @raw.last.insert?
      @raw.last.delete << sep if @raw.last.delete?
      @raw << ''
    end
  else
    @raw << ''
  end
  @raw.last << str.join(sep)
end

#to_sObject



70
71
72
# File 'lib/differ/diff.rb', line 70

def to_s
  @raw.join()
end