Class: LaunchDarkly::Impl::EvaluatorStack

Inherits:
Object
  • Object
show all
Defined in:
lib/ldclient-rb/impl/evaluator.rb

Overview

A helper class for managing cycle detection.

Each time a method sees a new flag or segment, they can push that object’s key onto the stack. Once processing for that object has finished, you can call pop to remove it.

Because the most common use case would be a flag or segment without ANY prerequisites, this stack has a small optimization in place– the stack is not created until absolutely necessary.

Since:

  • 5.5.0

Instance Method Summary collapse

Constructor Details

#initialize(original) ⇒ EvaluatorStack

Returns a new instance of EvaluatorStack.

Parameters:

  • original (String, nil)

Since:

  • 5.5.0



54
55
56
57
58
# File 'lib/ldclient-rb/impl/evaluator.rb', line 54

def initialize(original)
  @original = original
  # @type [Array<String>, nil]
  @stack = nil
end

Instance Method Details

#include?(key) ⇒ Boolean

Parameters:

  • key (String)

Returns:

  • (Boolean)

Since:

  • 5.5.0



84
85
86
87
88
89
# File 'lib/ldclient-rb/impl/evaluator.rb', line 84

def include?(key)
  return true if key == @original
  return false if @stack.nil?

  @stack.include? key
end

#popObject

Since:

  • 5.5.0



75
76
77
78
# File 'lib/ldclient-rb/impl/evaluator.rb', line 75

def pop
  return if @stack.nil? || @stack.empty?
  @stack.pop
end

#push(key) ⇒ Object

Parameters:

  • key (String)

Since:

  • 5.5.0



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ldclient-rb/impl/evaluator.rb', line 61

def push(key)
  # No need to store the key if we already have a record in our instance
  # variable.
  return if @original == key

  # The common use case is that flags/segments won't have prereqs, so we
  # don't allocate the stack memory until we absolutely must.
  if @stack.nil?
    @stack = []
  end

  @stack.push(key)
end