Class: PgVerify::Model::SourceLocation
- Inherits:
-
Object
- Object
- PgVerify::Model::SourceLocation
- Defined in:
- lib/pg-verify/model/source_location.rb
Instance Attribute Summary collapse
-
#file ⇒ Object
Returns the value of attribute file.
-
#line_number ⇒ Object
Returns the value of attribute line_number.
Instance Method Summary collapse
-
#initialize(file, line_number) ⇒ SourceLocation
constructor
A new instance of SourceLocation.
- #render_code_block(num_lines = 2) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(file, line_number) ⇒ SourceLocation
Returns a new instance of SourceLocation.
9 10 11 |
# File 'lib/pg-verify/model/source_location.rb', line 9 def initialize(file, line_number) @file, @line_number = file, line_number end |
Instance Attribute Details
#file ⇒ Object
Returns the value of attribute file.
6 7 8 |
# File 'lib/pg-verify/model/source_location.rb', line 6 def file @file end |
#line_number ⇒ Object
Returns the value of attribute line_number.
7 8 9 |
# File 'lib/pg-verify/model/source_location.rb', line 7 def line_number @line_number end |
Instance Method Details
#render_code_block(num_lines = 2) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/pg-verify/model/source_location.rb', line 13 def render_code_block(num_lines = 2) index = @line_number - 1 # Read lines and add line numbers lines = File.read(@file).split("\n").each_with_index.map { |l, i| "#{i.to_s.c_blue}: #{l}" } # Get the line surrounding the error start_index = [0, index - num_lines].max end_index = [lines.length - 1, index + num_lines].min surrounding_lines = lines[start_index..end_index] error_line = lines[index] surrounding_lines = [ "...".c_sidenote ] + surrounding_lines + [ "...".c_sidenote ] surrounding_lines = surrounding_lines.map { |l| if l == error_line l.ljust(surrounding_lines.map(&:length).max) + " < Error".c_error else l end } return surrounding_lines.join("\n") end |
#to_s ⇒ Object
37 38 39 |
# File 'lib/pg-verify/model/source_location.rb', line 37 def to_s() "#{@file}:#{@line_number}" end |