Class: PatchObj

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

Overview

Class representing one patch operation.

Constant Summary collapse

PATCH_PARSER =
URI::RFC2396_Parser.new

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.



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/patch_obj.rb', line 29

def to_s
  coords1 = get_coords(length1, start1)
  coords2 = get_coords(length2, start2)

  text = ["@@ -", coords1, " +", coords2, " @@\n"].join

  # Encode the body of the patch with %xx notation.
  text += diffs.map { |op, data|
    [OPERATOR_TO_CHAR[op], PATCH_PARSER.escape(data, ENCODE_REGEX), "\n"].join
  }.join.gsub("%20", " ")

  text
end