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



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



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

def binding_index
  @binding_index
end

#bindingsArray<Binding>



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

def bindings
  @bindings
end

#prior_backtraceArray (readonly)



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

def prior_backtrace
  @prior_backtrace
end

#prior_bindingBinding (readonly)



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

def prior_binding
  @prior_binding
end

#userHash (readonly)



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



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/pry-stack_explorer/frame_manager.rb', line 75

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



55
56
57
# File 'lib/pry-stack_explorer/frame_manager.rb', line 55

def current_frame
  bindings[binding_index]
end

#each(&block) ⇒ Object

Iterate over all frames



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

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

#filter_bindings(vapid_frames: false) ⇒ Object



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

def filter_bindings(vapid_frames: false)
  bindings.reject do |binding|
    !vapid_frames and
      binding.local_variable_defined?(:vapid_frame)
  end
end

#refresh_frame(run_whereami = true) ⇒ Object

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



50
51
52
# File 'lib/pry-stack_explorer/frame_manager.rb', line 50

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.



62
63
64
65
66
67
68
69
70
# File 'lib/pry-stack_explorer/frame_manager.rb', line 62

def set_binding_index_safely(index)
  if index > bindings.size - 1
    raise Pry::CommandError, "At top of stack, cannot go further"
  elsif index < 0
    raise Pry::CommandError, "At bottom of stack, cannot go further"
  else
    self.binding_index = index
  end
end