Class: Peeek::Call

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

Defined Under Namespace

Classes: Exception, Result, ReturnValue

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hook, backtrace, receiver, arguments, result) ⇒ Call

Initialize the call.

Parameters:

  • hook (Peeek::Hook)

    hook the call occurred

  • backtrace (Array<String>)

    backtrace the call occurred

  • receiver (Module, Class, Object)

    object that received the call

  • arguments (Array)

    arguments at the call

  • result (Peeek::Call::Result)

    result of the call

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
19
# File 'lib/peeek/call.rb', line 11

def initialize(hook, backtrace, receiver, arguments, 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
  @result = result
end

Instance Attribute Details

#argumentsArray (readonly)

Returns arguments at the call.

Returns:

  • (Array)

    arguments at the call



43
44
45
# File 'lib/peeek/call.rb', line 43

def arguments
  @arguments
end

#backtraceArray<String> (readonly)

Returns backtrace the call occurred.

Returns:

  • (Array<String>)

    backtrace the call occurred



27
28
29
# File 'lib/peeek/call.rb', line 27

def backtrace
  @backtrace
end

#exceptionStandardError (readonly)

Returns exception that raised from the call.

Returns:

  • (StandardError)

    exception that raised from the call

Raises:

  • (TypeError)


58
59
60
61
# File 'lib/peeek/call.rb', line 58

def exception
  raise TypeError, "the call didn't raised an exception" unless raised?
  @result.value
end

#fileString (readonly)

Returns name of file the call occurred.

Returns:

  • (String)

    name of file the call occurred



31
32
33
# File 'lib/peeek/call.rb', line 31

def file
  @file
end

#hookPeeek::Hook (readonly)

Returns hook the call occurred.

Returns:



23
24
25
# File 'lib/peeek/call.rb', line 23

def hook
  @hook
end

#lineInteger (readonly)

Returns line number the call occurred.

Returns:

  • (Integer)

    line number the call occurred



35
36
37
# File 'lib/peeek/call.rb', line 35

def line
  @line
end

#receiverModule, ... (readonly)

Returns object that received the call.

Returns:

  • (Module, Class, Object)

    object that received the call



39
40
41
# File 'lib/peeek/call.rb', line 39

def receiver
  @receiver
end

#resultPeeek::Call::Result (readonly)

Returns result of the call.

Returns:



47
48
49
# File 'lib/peeek/call.rb', line 47

def result
  @result
end

#return_valueObject (readonly)

Returns value that the call returned.

Returns:

  • (Object)

    value that the call returned

Raises:

  • (TypeError)


51
52
53
54
# File 'lib/peeek/call.rb', line 51

def return_value
  raise TypeError, "the call didn't return a value" unless returned?
  @result.value
end

Instance Method Details

#raised?Boolean

Determine if the result is an exception.

Returns:

  • (Boolean)

    whether the result is an exception



73
74
75
# File 'lib/peeek/call.rb', line 73

def raised?
  @result.is_a?(Exception)
end

#returned?Boolean

Determine if the result is a return value.

Returns:

  • (Boolean)

    whether the result is a return value



66
67
68
# File 'lib/peeek/call.rb', line 66

def returned?
  @result.is_a?(ReturnValue)
end

#to_sObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/peeek/call.rb', line 77

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 returned?
    parts << "returned #{return_value.inspect}"
  elsif raised?
    parts << "raised #{exception.inspect}"
  end

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