Class: Patch
- Inherits:
-
Object
- Object
- Patch
- Defined in:
- app/models/patch.rb
Constant Summary collapse
- RANGE_INFORMATION_LINE =
/^@@ .+\+(?<line_number>\d+),/
- MODIFIED_LINE =
/^\+(?!\+|\+)/
- NOT_REMOVED_LINE =
/^[^-]/
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
Class Method Summary collapse
Instance Method Summary collapse
- #changed_lines ⇒ Object
-
#initialize(body) ⇒ Patch
constructor
A new instance of Patch.
- #position_for_line_number(line) ⇒ Object
Constructor Details
#initialize(body) ⇒ Patch
Returns a new instance of Patch.
15 16 17 |
# File 'app/models/patch.rb', line 15 def initialize(body) @body = body || '' end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
6 7 8 |
# File 'app/models/patch.rb', line 6 def body @body end |
Class Method Details
.from_file_body(source) ⇒ Object
8 9 10 11 12 13 |
# File 'app/models/patch.rb', line 8 def self.from_file_body(source) lines = source.lines header = "@@ -0,0 +1,#{lines.count} @@\n" body = header + lines.map { |line| "+#{line}"}.join new(body) end |
Instance Method Details
#changed_lines ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'app/models/patch.rb', line 19 def changed_lines line_number = 0 lines.each_with_index.inject([]) do |lines, (content, patch_position)| case content when RANGE_INFORMATION_LINE line_number = Regexp.last_match[:line_number].to_i when MODIFIED_LINE line = Line.new( content: content, number: line_number, patch_position: patch_position ) lines << line line_number += 1 when NOT_REMOVED_LINE line_number += 1 end lines end end |
#position_for_line_number(line) ⇒ Object
42 43 44 |
# File 'app/models/patch.rb', line 42 def position_for_line_number(line) changed_lines.find { |patch_line| patch_line.number == line }.patch_position end |