Class: CallerWithContext::Location

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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_locationObject (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_contextObject (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_linesObject



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_fileObject



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_sObject



24
25
26
# File 'lib/caller_with_context/location.rb', line 24

def to_s
  caller_location.to_s
end