Class: PryStackExplorer::FrameManager

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/pry-stack_explorer/frame_manager.rb

Overview

This class represents a call-stack. It stores the frames that make up the stack and is responsible for updating the associated Pry instance to reflect the active frame. It is fully Enumerable.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bindings, _pry_) ⇒ FrameManager

Returns a new instance of FrameManager.



27
28
29
30
31
32
33
34
# File 'lib/pry-stack_explorer/frame_manager.rb', line 27

def initialize(bindings, _pry_)
  self.bindings      = bindings
  self.binding_index = 0
  @pry               = _pry_
  @user              = {}
  @prior_binding     = _pry_.binding_stack.last
  @prior_backtrace   = _pry_.backtrace
end

Instance Attribute Details

#binding_indexFixnum

Returns The index of the active frame (binding) in the call-stack.

Returns:

  • (Fixnum)

    The index of the active frame (binding) in the call-stack.



14
15
16
# File 'lib/pry-stack_explorer/frame_manager.rb', line 14

def binding_index
  @binding_index
end

#bindingsArray<Binding>

Returns The array of bindings that constitute the call-stack.

Returns:

  • (Array<Binding>)

    The array of bindings that constitute the call-stack.



11
12
13
# File 'lib/pry-stack_explorer/frame_manager.rb', line 11

def bindings
  @bindings
end

#prior_backtraceArray (readonly)

Returns The backtrace of the Pry instance before the FrameManager took over.

Returns:

  • (Array)

    The backtrace of the Pry instance before the FrameManager took over.



25
26
27
# File 'lib/pry-stack_explorer/frame_manager.rb', line 25

def prior_backtrace
  @prior_backtrace
end

#prior_bindingBinding (readonly)

Returns The binding of the Pry instance before the FrameManager took over.

Returns:

  • (Binding)

    The binding of the Pry instance before the FrameManager took over.



21
22
23
# File 'lib/pry-stack_explorer/frame_manager.rb', line 21

def prior_binding
  @prior_binding
end

#userHash (readonly)

Returns A hash for user defined data.

Returns:

  • (Hash)

    A hash for user defined data



17
18
19
# File 'lib/pry-stack_explorer/frame_manager.rb', line 17

def user
  @user
end

Instance Method Details

#change_frame_to(index, run_whereami = true) ⇒ Object

Change active frame to the one indexed by ‘index`. Note that indexing base is `0`

Parameters:

  • index (Fixnum)

    The index of the frame.



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/pry-stack_explorer/frame_manager.rb', line 71

def change_frame_to(index, run_whereami=true)

  set_binding_index_safely(index)

  if @pry.binding_stack.empty?
    @pry.binding_stack.replace [bindings[binding_index]]
  else
    @pry.binding_stack[-1] = bindings[binding_index]
  end

  @pry.run_command "whereami" if run_whereami
end

#current_frameBinding

Returns The currently active frame.

Returns:

  • (Binding)

    The currently active frame



48
49
50
# File 'lib/pry-stack_explorer/frame_manager.rb', line 48

def current_frame
  bindings[binding_index]
end

#each(&block) ⇒ Object

Iterate over all frames



37
38
39
# File 'lib/pry-stack_explorer/frame_manager.rb', line 37

def each(&block)
  bindings.each(&block)
end

#refresh_frame(run_whereami = true) ⇒ Object

Ensure the Pry instance’s active binding is the frame manager’s active binding.



43
44
45
# File 'lib/pry-stack_explorer/frame_manager.rb', line 43

def refresh_frame(run_whereami=true)
  change_frame_to binding_index, run_whereami
end

#set_binding_index_safely(index) ⇒ Object

Set the binding index (aka frame index), but raising an Exception when invalid index received. Also converts negative indices to their positive counterparts.

Parameters:

  • index (Fixnum)

    The index.



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/pry-stack_explorer/frame_manager.rb', line 55

def set_binding_index_safely(index)
  if index > bindings.size - 1
    raise Pry::CommandError, "At top of stack, cannot go further!"
  elsif index < -bindings.size
    raise Pry::CommandError, "At bottom of stack, cannot go further!"
  else
    # wrap around negative indices
    index = (bindings.size - 1) + index + 1 if index < 0

    self.binding_index = index
  end
end