Class: PatchObj

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

Overview

Class representing one patch operation.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePatchObj

Returns a new instance of PatchObj.



9
10
11
12
13
14
15
16
# File 'lib/patch_obj.rb', line 9

def initialize
  # Initializes with an empty list of diffs.
  @start1 = nil
  @start2 = nil
  @length1 = 0
  @length2 = 0
  @diffs = []
end

Instance Attribute Details

#diffsObject

Returns the value of attribute diffs.



7
8
9
# File 'lib/patch_obj.rb', line 7

def diffs
  @diffs
end

#length1Object

Returns the value of attribute length1.



6
7
8
# File 'lib/patch_obj.rb', line 6

def length1
  @length1
end

#length2Object

Returns the value of attribute length2.



6
7
8
# File 'lib/patch_obj.rb', line 6

def length2
  @length2
end

#start1Object

Returns the value of attribute start1.



5
6
7
# File 'lib/patch_obj.rb', line 5

def start1
  @start1
end

#start2Object

Returns the value of attribute start2.



5
6
7
# File 'lib/patch_obj.rb', line 5

def start2
  @start2
end

Instance Method Details

#to_sObject

Emulate GNU diff’s format Header: @@ -382,8 +481,9 @@ Indices are printed as 1-based, not 0-based.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/patch_obj.rb', line 21

def to_s
  if length1 == 0
    coords1 = start1.to_s + ",0"
  elsif length1 == 1
    coords1 = (start1 + 1).to_s
  else
    coords1 = (start1 + 1).to_s + "," + length1.to_s
  end

  if length2 == 0
    coords2 = start2.to_s + ",0"
  elsif length2 == 1
    coords2 = (start2 + 1).to_s
  else
    coords2 = (start2 + 1).to_s + "," + length2.to_s
  end
  
  text = '@@ -' + coords1 + ' +' + coords2 + " @@\n"

  # Encode the body of the patch with %xx notation.
  text += diffs.map do |op, data|
    op = case op
          when :insert; '+'
          when :delete; '-'
          when :equal ; ' '
         end
    op + URI.encode(data, /[^0-9A-Za-z_.;!~*'(),\/?:@&=+$\#-]/) + "\n"
  end.join.gsub('%20', ' ')
  
  return text
end