Class: LaunchDarkly::Impl::Evaluator Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Encapsulates the feature flag evaluation logic. The Evaluator has no knowledge of the rest of the SDK environment; if it needs to retrieve flags or segments that are referenced by a flag, it does so through a simple function that is provided in the constructor. It also produces feature requests as appropriate for any referenced prerequisite flags, but does not send them.

Since:

  • 5.5.0

Defined Under Namespace

Classes: EvalResult

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(get_flag, get_segment, get_big_segments_membership, logger) ⇒ Evaluator

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

A single Evaluator is instantiated for each client instance.

Parameters:

  • get_flag (Function) —

    called if the Evaluator needs to query a different flag from the one that it is currently evaluating (i.e. a prerequisite flag); takes a single parameter, the flag key, and returns the flag data - or nil if the flag is unknown or deleted

  • get_segment (Function) —

    similar to get_flag, but is used to query a context segment.

  • logger (Logger) —

    the client's logger

Since:

  • 5.5.0



121
122
123
124
125
126
# File 'lib/ldclient-rb/impl/evaluator.rb', line 121

def initialize(get_flag, get_segment, get_big_segments_membership, logger)
  @get_flag = get_flag
  @get_segment = get_segment
  @get_big_segments_membership = get_big_segments_membership
  @logger = logger
end

Class Method Details

.error_result(errorKind, value = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Helper function used internally to construct an EvaluationDetail for an error result.

Since:

  • 5.5.0



146
147
148
# File 'lib/ldclient-rb/impl/evaluator.rb', line 146

def self.error_result(errorKind, value = nil)
  EvaluationDetail.new(value, nil, EvaluationReason.error(errorKind))
end

.make_big_segment_ref(segment) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

Since:

  • 5.5.0



187
188
189
190
191
192
# File 'lib/ldclient-rb/impl/evaluator.rb', line 187

def self.make_big_segment_ref(segment)  # method is visible for testing
  # The format of Big Segment references is independent of what store implementation is being
  # used; the store implementation receives only this string and does not know the details of
  # the data model. The Relay Proxy will use the same format when writing to the store.
  "#{segment.key}.g#{segment.generation}"
end

Instance Method Details

#evaluate(flag, context) ⇒ Array<EvalResult, EvaluatorState>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The client's entry point for evaluating a flag. The returned EvalResult contains the evaluation result and any events that were generated for prerequisite flags; its value will be nil if the flag returns the default value. Error conditions produce a result with a nil value and an error reason, not an exception.

inspecting the evaluation process

Parameters:

Returns:

Since:

  • 5.5.0



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/ldclient-rb/impl/evaluator.rb', line 158

def evaluate(flag, context)
  state = EvaluatorState.new(flag)

  result = EvalResult.new
  begin
    detail = eval_internal(flag, context, result, state)

    unless result.big_segments_status.nil?
      # If big_segments_status is non-nil at the end of the evaluation, it means a query was done at
      # some point and we will want to include the status in the evaluation reason.
      detail = EvaluationDetail.new(detail.value, detail.variation_index,
        detail.reason.with_big_segments_status(result.big_segments_status))
    end
  rescue EvaluationException => exn
    Impl::Util.log_exception(@logger, "Unexpected error when evaluating flag #{flag.key}", exn)
    detail = EvaluationDetail.new(nil, nil, EvaluationReason::error(exn.error_kind))
  rescue => exn
    Impl::Util.log_exception(@logger, "Unexpected error when evaluating flag #{flag.key}", exn)
    detail = EvaluationDetail.new(nil, nil, EvaluationReason::error(EvaluationReason::ERROR_EXCEPTION))
  end

  # Error results are marked too. A malformed override definition yields an error reason, and an
  # override still affected that result.
  result.override_affected = state.override_affected
  result.detail = mark_override_affected(detail, state.override_affected)
  [result, state]
end