class UnifiedDiff::Chunk

Attributes

modified_range[R]
original_range[R]
raw_lines[R]

Public Class Methods

new(ranges) click to toggle source

Create a new chunk with the specified modified and original ranges

@param [Hash] A hash with keys for original and modified line range @return [Array] the lines in the modified version of the chunk

# File lib/unified_diff/chunk.rb, line 9
def initialize(ranges)
  @original_range = ranges[:original]
  @modified_range = ranges[:modified]
  @raw_lines = []
end

Public Instance Methods

added_lines() click to toggle source

Return an array of lines that were added to the original version of the chunk

@return [Array] the lines that were added to the original

# File lib/unified_diff/chunk.rb, line 38
def added_lines
  processed_lines_of_types('+')
end
modified_lines() click to toggle source

Return an array of lines that are present in the modified version of the chunk

@return [Array] the lines in the modified version of the chunk

# File lib/unified_diff/chunk.rb, line 45
def modified_lines
  processed_lines_of_types(' ','+')
end
original_lines() click to toggle source

Return an array of lines that are present in the original version of the chunk

@return [Array] the lines in the original version of the chunk

# File lib/unified_diff/chunk.rb, line 52
def original_lines
  processed_lines_of_types(' ','-')
end
removed_lines() click to toggle source

Return an array of lines that were removed from the original version of the chunk

@return [Array] the lines that were removed from the original

# File lib/unified_diff/chunk.rb, line 31
def removed_lines
  processed_lines_of_types('-')
end
to_s() click to toggle source

Render the chunk as it appeared in the original unified diff

@return [String] the chunk as it appeared in the original unified diff

# File lib/unified_diff/chunk.rb, line 19
def to_s
  "@@ "+
    "-#{@original_range.begin},#{@original_range.count} " +
    "+#{@modified_range.begin},#{@modified_range.count} " +
  "@@\n" +
  @raw_lines.join("\n") +
  "\n"
end

Private Instance Methods

insert_addition(line) click to toggle source

Insert a new addition line into the list of lines for this chunk

@param [String] the line to be inserted (without '+' tag) @return [Array] the new list of raw lines

# File lib/unified_diff/chunk.rb, line 65
def insert_addition(line)
  @raw_lines << "+#{line}"
end
insert_no_newline_at_eof(line) click to toggle source

Insert a no newline at end of file line into the list of lines for this chunk

www.gnu.org/software/diffutils/manual/html_node/Incomplete-Lines.html

@param [String] the line to be inserted (without '' tag) @return [Array] the new list of raw lines

# File lib/unified_diff/chunk.rb, line 92
def insert_no_newline_at_eof(line)
  @raw_lines << "\\#{line}"
end
insert_removal(line) click to toggle source

Insert a new removal line into the list of lines for this chunk

@param [String] the line to be inserted (without '-' tag) @return [Array] the new list of raw lines

# File lib/unified_diff/chunk.rb, line 73
def insert_removal(line)
  @raw_lines << "-#{line}"
end
insert_unchanged(line) click to toggle source

Insert a new unchanged line into the list of lines for this chunk

@param [String] the line to be inserted (without ' ' tag) @return [Array] the new list of raw lines

# File lib/unified_diff/chunk.rb, line 81
def insert_unchanged(line)
  @raw_lines << " #{line}"
end
processed_lines_of_types(*types) click to toggle source
# File lib/unified_diff/chunk.rb, line 57
def processed_lines_of_types(*types)
  @raw_lines.select {|line| types.include?(line[0])}.map {|line| line[1..-1]}
end