Class: Diff

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Diff

Returns a new instance of Diff.



12
13
14
15
16
# File 'lib/diff.rb', line 12

def initialize(attrs={})
  attrs.each do |key, val|
    instance_variable_set("@#{key}".to_sym, val)
  end
end

Instance Attribute Details

#delete_countObject

Returns the value of attribute delete_count.



8
9
10
# File 'lib/diff.rb', line 8

def delete_count
  @delete_count
end

#delete_startObject

Returns the value of attribute delete_start.



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

def delete_start
  @delete_start
end

#deletionsObject

Returns the value of attribute deletions.



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

def deletions
  @deletions
end

#insert_countObject

Returns the value of attribute insert_count.



10
11
12
# File 'lib/diff.rb', line 10

def insert_count
  @insert_count
end

#insert_startObject

Returns the value of attribute insert_start.



9
10
11
# File 'lib/diff.rb', line 9

def insert_start
  @insert_start
end

#insertionsObject

Returns the value of attribute insertions.



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

def insertions
  @insertions
end

Class Method Details

.fold_lines_standard(acc, diff_line) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/diff.rb', line 52

def fold_lines_standard(acc, diff_line)
  diff = diff_line
  line = diff_line[1..-1]

  if diff.first == "+"
    acc[:insertions] << line
  elsif diff.first == "-"
    acc[:deletions] << line
  end

  acc
end

.from_git(diff_lines) ⇒ Object



21
22
23
# File 'lib/diff.rb', line 21

def from_git(diff_lines)
  Diff.new(parse_diff(diff_lines))
end

.parse_diff(diff_lines) ⇒ Object



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
# File 'lib/diff.rb', line 25

def parse_diff(diff_lines)
  stats = diff_lines
    .first
    .split("@@")[1]
    .split(" ")

  delete = stats.first.split(",")
  add = stats.last.split(",")

  delete_start = delete.first[1..-1].to_i
  delete_count = delete.length > 1 ? delete.last.to_i : 1
  add_start = add.first[1..-1].to_i
  add_count = add.length > 1 ? add.last.to_i : 1

  diffs = diff_lines[1..-1]
  patch = {
    insertions: [],
    deletions: [],
    delete_start: delete_start,
    delete_count: delete_count,
    insert_start: add_start,
    insert_count: add_count
  }

  diffs.reduce(patch, &method(:fold_lines_standard))
end