Class: LazyGraph::StackPointer

Inherits:
Struct
  • Object
show all
Defined in:
lib/lazy_graph/stack_pointer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

Handles method calls not explicitly defined in this class by delegating them first to the frame, then to the parent, recursively up the stack.



39
40
41
42
43
44
45
46
47
# File 'lib/lazy_graph/stack_pointer.rb', line 39

def method_missing(name, *args, &block)
  if frame.respond_to?(name)
    frame.send(name, *args, &block)
  elsif parent
    parent.send(name, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#depthObject

Returns the value of attribute depth

Returns:

  • (Object)

    the current value of depth



7
8
9
# File 'lib/lazy_graph/stack_pointer.rb', line 7

def depth
  @depth
end

#frameObject

Returns the value of attribute frame

Returns:

  • (Object)

    the current value of frame



7
8
9
# File 'lib/lazy_graph/stack_pointer.rb', line 7

def frame
  @frame
end

#keyObject

Returns the value of attribute key

Returns:

  • (Object)

    the current value of key



7
8
9
# File 'lib/lazy_graph/stack_pointer.rb', line 7

def key
  @key
end

#parentObject

Returns the value of attribute parent

Returns:

  • (Object)

    the current value of parent



7
8
9
# File 'lib/lazy_graph/stack_pointer.rb', line 7

def parent
  @parent
end

#pointer_cacheObject

Returns the value of attribute pointer_cache.



8
9
10
# File 'lib/lazy_graph/stack_pointer.rb', line 8

def pointer_cache
  @pointer_cache
end

#recursion_depthObject

Returns the value of attribute recursion_depth

Returns:

  • (Object)

    the current value of recursion_depth



7
8
9
# File 'lib/lazy_graph/stack_pointer.rb', line 7

def recursion_depth
  @recursion_depth
end

#rootObject

Returns the value of attribute root

Returns:

  • (Object)

    the current value of root



7
8
9
# File 'lib/lazy_graph/stack_pointer.rb', line 7

def root
  @root
end

Instance Method Details

#indexObject

Returns the key associated with this stack pointer’s frame.



50
51
52
# File 'lib/lazy_graph/stack_pointer.rb', line 50

def index
  key
end

#log_debug(**log_item) ⇒ Object

Logs debugging information related to this stack pointer in the root frame’s DEBUG section.



55
56
57
58
59
# File 'lib/lazy_graph/stack_pointer.rb', line 55

def log_debug(**log_item)
  root.frame[:DEBUG] = [] if !root.frame[:DEBUG] || root.frame[:DEBUG].is_a?(MissingValue)
  root.frame[:DEBUG] << { **log_item, location: to_s }
  nil
end

#ptr_at(index) ⇒ Object

Retrieves the StackPointer at a specific index in the upward chain of parents.



32
33
34
35
# File 'lib/lazy_graph/stack_pointer.rb', line 32

def ptr_at(index)
  @pointer_cache ||= {}.compare_by_identity
  @pointer_cache[index] ||= depth == index ? self : parent&.ptr_at(index)
end

#push(frame, key) ⇒ Object

Pushes a new frame onto the stack, creating or reusing a StackPointer. Frames represent activation contexts; keys are identifiers within those frames.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/lazy_graph/stack_pointer.rb', line 12

def push(frame, key)
  ptr = POINTER_POOL.pop || StackPointer.new
  ptr.parent = self
  ptr.root = root || self
  ptr.frame = frame
  ptr.key = key
  ptr.depth = depth + 1
  ptr.recursion_depth = recursion_depth
  ptr.pointer_cache&.clear
  ptr
end

#recycle!Object

Recycles the current StackPointer by adding it back to the pointer pool. Once recycled, this instance should no longer be used unless reassigned by push.



26
27
28
29
# File 'lib/lazy_graph/stack_pointer.rb', line 26

def recycle!
  POINTER_POOL.push(self)
  nil
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Determines if the stack pointer can respond to a missing method by mimicking the behavior of the frame or any parent stack pointers recursively.

Returns:

  • (Boolean)


63
64
65
# File 'lib/lazy_graph/stack_pointer.rb', line 63

def respond_to_missing?(name, include_private = false)
  frame.respond_to?(name, include_private) || parent.respond_to?(name, include_private)
end

#to_sObject

Returns a string representation of the stacking path of keys up to this pointer.



68
69
70
71
72
73
74
# File 'lib/lazy_graph/stack_pointer.rb', line 68

def to_s
  if parent
    "#{parent}#{key.to_s =~ /\d+/ ? "[#{key}]" : ".#{key}"}"
  else
    key.to_s
  end
end