Class: Delfos::CallStack::Stack

Inherits:
Object
  • Object
show all
Defined in:
lib/delfos/call_stack/stack.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(on_empty: nil) ⇒ Stack

Returns a new instance of Stack.



5
6
7
# File 'lib/delfos/call_stack/stack.rb', line 5

def initialize(on_empty: nil)
  @on_empty = on_empty
end

Instance Attribute Details

#call_sitesObject



39
40
41
# File 'lib/delfos/call_stack/stack.rb', line 39

def call_sites
  @call_sites ||= []
end

#execution_countObject



35
36
37
# File 'lib/delfos/call_stack/stack.rb', line 35

def execution_count
  @execution_count ||= 0
end

#stack_depthObject



31
32
33
# File 'lib/delfos/call_stack/stack.rb', line 31

def stack_depth
  @stack_depth ||= 0
end

#step_countObject



43
44
45
# File 'lib/delfos/call_stack/stack.rb', line 43

def step_count
  call_sites.length
end

Instance Method Details

#popObject



16
17
18
19
20
21
22
23
24
25
# File 'lib/delfos/call_stack/stack.rb', line 16

def pop
  popping_empty_stack! if self.stack_depth.zero?

  self.stack_depth -= 1

  if stack_depth.zero? && call_sites.length.positive?
    @on_empty&.call(call_sites, execution_count)
    self.call_sites = []
  end
end

#pop_until_top!Object



27
28
29
# File 'lib/delfos/call_stack/stack.rb', line 27

def pop_until_top!
  pop while self.stack_depth.positive?
end

#push(method_object) ⇒ Object



9
10
11
12
13
14
# File 'lib/delfos/call_stack/stack.rb', line 9

def push(method_object)
  call_sites.push(method_object)
  self.stack_depth += 1

  self.execution_count += self.stack_depth == 1 ? 1 : 0
end