Class: Grit::DiffHunk

Inherits:
Object
  • Object
show all
Defined in:
lib/grit/ext/diff_hunk.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(header, diff_hunk) ⇒ DiffHunk

Returns a new instance of DiffHunk.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/grit/ext/diff_hunk.rb', line 5

def initialize(header, diff_hunk)
  @header = header
  @lines = []

  diff_hunk.each_with_index.map do |line, index|
    content = line[1..line.length - 1]
    status = DiffLine.status_from_char(line[0])

    if status
      line_number = header.start + index

      case status
      when :added, :unchanged
        line_number = line_number - removed.count
      when :removed
        line_number = line_number - added.count
      end

      @lines << DiffLine.new(content, status, line_number, index)
    end
  end.compact
end

Instance Attribute Details

#linesObject (readonly)

Returns the value of attribute lines.



3
4
5
# File 'lib/grit/ext/diff_hunk.rb', line 3

def lines
  @lines
end

Instance Method Details

#addedObject



28
29
30
# File 'lib/grit/ext/diff_hunk.rb', line 28

def added
  @lines.find_all { |line| line.added? }
end

#removedObject



32
33
34
# File 'lib/grit/ext/diff_hunk.rb', line 32

def removed
  @lines.find_all { |line| line.removed? }
end

#to_sObject



40
41
42
# File 'lib/grit/ext/diff_hunk.rb', line 40

def to_s
  "#{@header}\n#{lines.join("\n")}"
end

#unchangedObject



36
37
38
# File 'lib/grit/ext/diff_hunk.rb', line 36

def unchanged
  @lines.find_all { |line| line.unchanged? }
end