Class: CallerWithContext::Caller

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Caller

Returns a new instance of Caller.



8
9
10
11
12
13
14
# File 'lib/caller_with_context/caller.rb', line 8

def initialize(options={})
  @options = {
    colorize: true,
    only_cwd: false,
    lines_of_context: 1
  }.merge(options)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/caller_with_context/caller.rb', line 6

def options
  @options
end

Instance Method Details

#format_single_location(location) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/caller_with_context/caller.rb', line 39

def format_single_location(location)
  output(location.to_s, :light_blue)
  if options[:lines_of_context] > 0
    location.context_lines.each do |line|
      puts line
    end
    puts
  end
end

#locationsObject



16
17
18
19
20
21
22
23
# File 'lib/caller_with_context/caller.rb', line 16

def locations
  return @locations if @locations
  locations = caller_locations
  if @options[:only_cwd]
    locations.select! { |l| l.to_s.include?(Dir.pwd) }
  end
  @locations ||= locations.map { |location| Location.new(location, options[:lines_of_context]) }
end

#output(string, color) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/caller_with_context/caller.rb', line 31

def output(string, color)
  if options[:colorize]
    puts string.send(color)
  else
    puts string
  end
end

#showObject



25
26
27
28
29
# File 'lib/caller_with_context/caller.rb', line 25

def show
  locations.each do |location|
    format_single_location location
  end
end