Class: CallerWithContext::Location
- Inherits:
-
Object
- Object
- CallerWithContext::Location
- Defined in:
- lib/caller_with_context/location.rb
Instance Attribute Summary collapse
-
#caller_location ⇒ Object
readonly
Returns the value of attribute caller_location.
-
#lines_of_context ⇒ Object
readonly
Returns the value of attribute lines_of_context.
Instance Method Summary collapse
- #context_lines ⇒ Object
-
#initialize(caller_location, lines_of_context = 1) ⇒ Location
constructor
A new instance of Location.
- #line_indexes(lineno = caller_location.lineno-lines_of_context) ⇒ Object
- #lines_in_file ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(caller_location, lines_of_context = 1) ⇒ Location
Returns a new instance of Location.
5 6 7 8 |
# File 'lib/caller_with_context/location.rb', line 5 def initialize(caller_location, lines_of_context=1) @caller_location = caller_location @lines_of_context = lines_of_context end |
Instance Attribute Details
#caller_location ⇒ Object (readonly)
Returns the value of attribute caller_location.
3 4 5 |
# File 'lib/caller_with_context/location.rb', line 3 def caller_location @caller_location end |
#lines_of_context ⇒ Object (readonly)
Returns the value of attribute lines_of_context.
3 4 5 |
# File 'lib/caller_with_context/location.rb', line 3 def lines_of_context @lines_of_context end |
Instance Method Details
#context_lines ⇒ Object
10 11 12 |
# File 'lib/caller_with_context/location.rb', line 10 def context_lines line_indexes.each.map {|line_number| lines_in_file[line_number]&.chomp } end |
#line_indexes(lineno = caller_location.lineno-lines_of_context) ⇒ Object
14 15 16 17 18 |
# File 'lib/caller_with_context/location.rb', line 14 def line_indexes(lineno=caller_location.lineno-lines_of_context) min = lineno < lines_of_context ? 0 : lineno - lines_of_context max = lineno >= lines_in_file.count ? lines_in_file.count : lineno + lines_of_context min..max end |
#lines_in_file ⇒ Object
20 21 22 |
# File 'lib/caller_with_context/location.rb', line 20 def lines_in_file @lines_in_file ||= File.readlines caller_location.absolute_path end |
#to_s ⇒ Object
24 25 26 |
# File 'lib/caller_with_context/location.rb', line 24 def to_s caller_location.to_s end |