class Peeek::Call

Attributes

arguments[R]

@attribute [r] arguments @return [Array] arguments at the call

backtrace[R]

@attribute [r] backtrace @return [Array<String>] backtrace the call occurred

block[R]

@attribute [r] block @return [Proc] block at the call

file[R]

@attribute [r] file @return [String] name of file the call occurred

hook[R]

@attribute [r] hook @return [Peeek::Hook] hook the call occurred

line[R]

@attribute [r] line @return [Integer] line number the call occurred

receiver[R]

@attribute [r] receiver @return [Module, Class, Object] object that received the call

result[R]

@attribute [r] result @return [Peeek::Call::Result] result of the call

Public Class Methods

new(hook, backtrace, receiver, arguments, block, result) click to toggle source

Initialize the call.

@param [Peeek::Hook] hook hook the call occurred @param [Array<String>] backtrace backtrace the call occurred @param [Module, Class, Object] receiver object that received the call @param [Array] arguments arguments at the call @param [Proc] block block at the call @param [Peeek::Call::Result] result result of the call

# File lib/peeek/call.rb, line 12
def initialize(hook, backtrace, receiver, arguments, block, result)
  raise ArgumentError, 'invalid as result' unless result.is_a?(Result)
  @hook = hook
  @backtrace = backtrace
  @file, @line = extract_file_and_line(backtrace.first)
  @receiver = receiver
  @arguments = arguments
  @block = block
  @result = result
end

Public Instance Methods

exception() click to toggle source

@attribute [r] exception @return [StandardError] exception that raised from the call

# File lib/peeek/call.rb, line 64
def exception
  raise TypeError, "the call didn't raised an exception" unless raised?
  @result.value
end
raised?() click to toggle source

Determine if the result is an exception.

@return whether the result is an exception

# File lib/peeek/call.rb, line 79
def raised?
  @result.is_a?(Exception)
end
return_value() click to toggle source

@attribute [r] return_value @return [Object] value that the call returned

# File lib/peeek/call.rb, line 57
def return_value
  raise TypeError, "the call didn't return a value" unless returned?
  @result.value
end
returned?() click to toggle source

Determine if the result is a return value.

@return whether the result is a return value

# File lib/peeek/call.rb, line 72
def returned?
  @result.is_a?(ReturnValue)
end
to_s() click to toggle source
# File lib/peeek/call.rb, line 83
def to_s
  parts = [@hook.to_s]
  parts << "from #{@receiver.inspect}"

  if @arguments.size == 1
    parts << "with #{@arguments.first.inspect}"
  elsif @arguments.size > 1
    parts << "with (#{@arguments.map(&:inspect) * ', '})"
  end

  if @block
    conjunction = @arguments.empty? ? 'with' : 'and'
    parts << "#{conjunction} a block"
  end

  if returned?
    parts << "returned #{return_value.inspect}"
  elsif raised?
    parts << "raised #{exception.inspect}"
  end

  parts << "in #{@file}"
  parts << "at #{@line}"
  parts * ' '
end

Private Instance Methods

extract_file_and_line(string) click to toggle source
# File lib/peeek/call.rb, line 111
def extract_file_and_line(string)
  _, file, line = /^(.+):(\d+)(?::in\s+|$)/.match(string).to_a
  raise ArgumentError, 'invalid as string of backtrace' unless file and line
  [file, line.to_i]
end