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, block, 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

  • block (Proc)

    block at the call

  • result (Peeek::Call::Result)

    result of the call

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
19
20
21
# 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

Instance Attribute Details

#argumentsArray (readonly)

Returns arguments at the call.

Returns:

  • (Array)

    arguments at the call



45
46
47
# File 'lib/peeek/call.rb', line 45

def arguments
  @arguments
end

#backtraceArray<String> (readonly)

Returns backtrace the call occurred.

Returns:

  • (Array<String>)

    backtrace the call occurred



29
30
31
# File 'lib/peeek/call.rb', line 29

def backtrace
  @backtrace
end

#blockProc (readonly)

Returns block at the call.

Returns:

  • (Proc)

    block at the call



49
50
51
# File 'lib/peeek/call.rb', line 49

def block
  @block
end

#exceptionStandardError (readonly)

Returns exception that raised from the call.

Returns:

  • (StandardError)

    exception that raised from the call

Raises:

  • (TypeError)


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

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



33
34
35
# File 'lib/peeek/call.rb', line 33

def file
  @file
end

#hookPeeek::Hook (readonly)

Returns hook the call occurred.

Returns:



25
26
27
# File 'lib/peeek/call.rb', line 25

def hook
  @hook
end

#lineInteger (readonly)

Returns line number the call occurred.

Returns:

  • (Integer)

    line number the call occurred



37
38
39
# File 'lib/peeek/call.rb', line 37

def line
  @line
end

#receiverModule, ... (readonly)

Returns object that received the call.

Returns:

  • (Module, Class, Object)

    object that received the call



41
42
43
# File 'lib/peeek/call.rb', line 41

def receiver
  @receiver
end

#resultPeeek::Call::Result (readonly)

Returns result of the call.

Returns:



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

def result
  @result
end

#return_valueObject (readonly)

Returns value that the call returned.

Returns:

  • (Object)

    value that the call returned

Raises:

  • (TypeError)


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

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



79
80
81
# File 'lib/peeek/call.rb', line 79

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



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

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

#to_sObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# 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