Class: Diff
- Inherits:
-
Object
- Object
- Diff
- Defined in:
- lib/diff.rb
Instance Attribute Summary collapse
-
#delete_count ⇒ Object
Returns the value of attribute delete_count.
-
#delete_start ⇒ Object
Returns the value of attribute delete_start.
-
#deletions ⇒ Object
Returns the value of attribute deletions.
-
#insert_count ⇒ Object
Returns the value of attribute insert_count.
-
#insert_start ⇒ Object
Returns the value of attribute insert_start.
-
#insertions ⇒ Object
Returns the value of attribute insertions.
Class Method Summary collapse
- .fold_lines_standard(acc, diff_line) ⇒ Object
- .from_git(diff_lines) ⇒ Object
- .parse_diff(diff_lines) ⇒ Object
Instance Method Summary collapse
-
#initialize(attrs = {}) ⇒ Diff
constructor
A new instance of Diff.
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_count ⇒ Object
Returns the value of attribute delete_count.
8 9 10 |
# File 'lib/diff.rb', line 8 def delete_count @delete_count end |
#delete_start ⇒ Object
Returns the value of attribute delete_start.
7 8 9 |
# File 'lib/diff.rb', line 7 def delete_start @delete_start end |
#deletions ⇒ Object
Returns the value of attribute deletions.
6 7 8 |
# File 'lib/diff.rb', line 6 def deletions @deletions end |
#insert_count ⇒ Object
Returns the value of attribute insert_count.
10 11 12 |
# File 'lib/diff.rb', line 10 def insert_count @insert_count end |
#insert_start ⇒ Object
Returns the value of attribute insert_start.
9 10 11 |
# File 'lib/diff.rb', line 9 def insert_start @insert_start end |
#insertions ⇒ Object
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 |