Class: Reduxco::Context::Callstack

Inherits:
Object
  • Object
show all
Defined in:
lib/reduxco/context/callstack.rb

Overview

Defines and implements a callstack interface.

Callstacks are made of frames, and the top element of the stack is the current frame.

Instance Method Summary collapse

Constructor Details

#initialize(array = []) ⇒ Callstack

Initialize an empty callstack. Optionally takes an array of frames, reading from top of the stack to the bottom.



11
12
13
# File 'lib/reduxco/context/callstack.rb', line 11

def initialize(array=[])
  @stack = array.reverse
end

Instance Method Details

#depthObject

Returns teh callstack depth



44
45
46
# File 'lib/reduxco/context/callstack.rb', line 44

def depth
  @stack.length
end

#dupObject

Returns a copy of this callstack.



54
55
56
# File 'lib/reduxco/context/callstack.rb', line 54

def dup
  self.class.new(@stack.dup.reverse)
end

#include?(frame) ⇒ Boolean

Returns true if the callstack contains the given frame

Returns:

  • (Boolean)


39
40
41
# File 'lib/reduxco/context/callstack.rb', line 39

def include?(frame)
  @stack.include?(frame)
end

#inspectObject

Inspect the Callstack, with the top frame first.



74
75
76
# File 'lib/reduxco/context/callstack.rb', line 74

def inspect
  @stack.reverse.inspect
end

#peek(depth) ⇒ Object

Returns the element at a given depth from the top of the stack.

A depth of zero corresponds to the top of the stack.



34
35
36
# File 'lib/reduxco/context/callstack.rb', line 34

def peek(depth)
  @stack[-depth - 1]
end

#popObject

Pops the top frame from the callstack and returns it.



22
23
24
# File 'lib/reduxco/context/callstack.rb', line 22

def pop
  @stack.pop
end

#push(frame) ⇒ Object

Pushes the given frame onto the callstack



16
17
18
19
# File 'lib/reduxco/context/callstack.rb', line 16

def push(frame)
  @stack.push(frame)
  self
end

#restObject

Returns a Callstack instance for everything below the top of the stack.



49
50
51
# File 'lib/reduxco/context/callstack.rb', line 49

def rest
  self.class.new(@stack[0..-2].reverse)
end

#to_caller(top = nil) ⇒ Object

Returns the callstack in a form that looks like Ruby’s caller method, so that it can be placed in exception backtraces. Typically one wants the top of the caller-style stack to be the trace to where Context#call was invoked in a caller, so this may be provided.



62
63
64
65
66
# File 'lib/reduxco/context/callstack.rb', line 62

def to_caller(top=nil)
  @stack.reverse.map {|frame| "#{self.class.name} frame: #{frame}"}.tap do |cc|
    cc.unshift top.to_s unless top.nil?
  end
end

#to_sObject

Output the Callstack from top to bottom.



69
70
71
# File 'lib/reduxco/context/callstack.rb', line 69

def to_s
  @stack.reverse.to_s
end

#topObject

Returns the element at the top of the stack.



27
28
29
# File 'lib/reduxco/context/callstack.rb', line 27

def top
  @stack.last
end