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.

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)



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

def arguments
  @arguments
end

#backtraceArray<String> (readonly)



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

def backtrace
  @backtrace
end

#exceptionStandardError (readonly)

Returns 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)



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

def file
  @file
end

#hookPeeek::Hook (readonly)



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

def hook
  @hook
end

#lineInteger (readonly)



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

def line
  @line
end

#receiverModule, ... (readonly)



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

def receiver
  @receiver
end

#resultPeeek::Call::Result (readonly)



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

def result
  @result
end

#return_valueObject (readonly)

Returns 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.



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.



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