class UnifiedDiff::Chunk
Attributes
Public Class Methods
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
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
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
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
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
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 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 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 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 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
# 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