Class: Toggly::Evaluators::Targeting

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

Overview

Evaluator for user/group targeting rules.

Supports targeting by:

  • Specific user identities
  • Group membership

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#handles?

Class Method Details

.typeObject



11
12
13
# File 'lib/toggly/evaluators/targeting.rb', line 11

def self.type
  "targeting"
end

Instance Method Details

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

Evaluate targeting rule

Parameters:

  • rule (Hash)

    Rule with "users" and/or "groups" arrays

  • context (Context)

    Evaluation context

  • feature_key (String) (defaults to: nil)

    The feature key

Returns:

  • (Boolean, nil)

    True if matched, false if excluded, nil to continue



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/toggly/evaluators/targeting.rb', line 21

def evaluate(rule, context, feature_key: nil)
  return nil unless context

  # Check user targeting
  users = Array(rule_value(rule, "users"))
  excluded_users = Array(rule_value(rule, "excludedUsers") || rule_value(rule, "excluded_users"))

  if context.identity?
    # Check exclusion first
    return false if excluded_users.any? { |u| u.to_s == context.identity }

    # Check inclusion
    return true if users.any? { |u| u.to_s == context.identity }
  end

  # Check group targeting
  groups = Array(rule_value(rule, "groups"))
  excluded_groups = Array(rule_value(rule, "excludedGroups") || rule_value(rule, "excluded_groups"))

  # Check group exclusion
  return false if excluded_groups.any? { |g| context.in_group?(g) }

  # Check group inclusion
  return true if groups.any? { |g| context.in_group?(g) }

  # No match, continue evaluation
  nil
end