Class: Toggly::Evaluators::ContextualTargeting

Inherits:
Base
  • Object
show all
Defined in:
lib/toggly/evaluators/contextual_targeting.rb

Overview

Evaluator for contextual targeting based on traits.

Supports various comparison operators on trait values.

Constant Summary collapse

OPERATORS =

Supported operators

%w[
  eq ne gt gte lt lte
  contains not_contains
  starts_with ends_with
  in not_in
  matches
  exists not_exists
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#handles?

Class Method Details

.typeObject



19
20
21
# File 'lib/toggly/evaluators/contextual_targeting.rb', line 19

def self.type
  "contextual"
end

Instance Method Details

#evaluate(rule, context, feature_key: nil) ⇒ Boolean?

Evaluate contextual targeting rule

Parameters:

  • rule (Hash)

    Rule with "conditions" array

  • context (Context)

    Evaluation context with traits

  • feature_key (String) (defaults to: nil)

    The feature key

Returns:

  • (Boolean, nil)

    True if all conditions match, nil if no conditions



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/toggly/evaluators/contextual_targeting.rb', line 29

def evaluate(rule, context, feature_key: nil)
  conditions = Array(rule_value(rule, "conditions"))
  return nil if conditions.empty?
  return nil unless context

  match_type = rule_value(rule, "matchType") || rule_value(rule, "match_type") || "all"

  if match_type == "any"
    conditions.any? { |condition| evaluate_condition(condition, context) }
  else
    conditions.all? { |condition| evaluate_condition(condition, context) }
  end
end