Class: AlertMachine::Caller

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

Overview

Figures out how to parse the call stack and pretty print it.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(caller, &block) ⇒ Caller

Returns a new instance of Caller.



115
116
117
118
119
120
# File 'lib/alert_machine.rb', line 115

def initialize(caller, &block)
  @block = block if block_given?
  @caller = caller
  /^(?<fname>[^:]+)\:(?<line>\d+)\:/ =~ caller[0] and
    @file = fname and @line = line
end

Instance Attribute Details

#callerObject (readonly)

Returns the value of attribute caller.



113
114
115
# File 'lib/alert_machine.rb', line 113

def caller
  @caller
end

#fileObject (readonly)

Returns the value of attribute file.



113
114
115
# File 'lib/alert_machine.rb', line 113

def file
  @file
end

#lineObject (readonly)

Returns the value of attribute line.



113
114
115
# File 'lib/alert_machine.rb', line 113

def line
  @line
end

Instance Method Details

#file_lineObject



122
123
124
# File 'lib/alert_machine.rb', line 122

def file_line
  "#{file}:#{line}"
end

#logObject



126
127
128
129
# File 'lib/alert_machine.rb', line 126

def log
  "#{caller[0]}\n" +
    log_source_file.to_s
end

#log_source_fileObject



131
132
133
134
135
136
137
# File 'lib/alert_machine.rb', line 131

def log_source_file
  File.open(file) {|fh|
    fh.readlines[line.to_i - 1..line.to_i + 3].collect {|l|
      ">> #{l}"
    }.join + "\n---\n"
  } if file && File.exists?(file)
end