Module: Flagsmith::Engine::Segments::Evaluator

Includes:
Constants, Utils::HashFunc
Included in:
Engine
Defined in:
lib/flagsmith/engine/segments/evaluator.rb

Overview

Evaluator methods

Constant Summary

Constants included from Constants

Constants::ALL_RULE, Constants::ANY_RULE, Constants::CONDITION_OPERATORS, Constants::CONTAINS, Constants::EQUAL, Constants::GREATER_THAN, Constants::GREATER_THAN_INCLUSIVE, Constants::IN, Constants::IS_NOT_SET, Constants::IS_SET, Constants::LESS_THAN, Constants::LESS_THAN_INCLUSIVE, Constants::MODULO, Constants::NONE_RULE, Constants::NOT_CONTAINS, Constants::NOT_EQUAL, Constants::PERCENTAGE_SPLIT, Constants::REGEX, Constants::RULE_TYPES

Instance Method Summary collapse

Methods included from Utils::HashFunc

#hashed_percentage_for_object_ids

Instance Method Details

#evaluate_identity_in_segment(identity, segment, override_traits = nil) ⇒ Object

Evaluates whether a given identity is in the provided segment.

:param identity: identity model object to evaluate :param segment: segment model object to evaluate :param override_traits: pass in a list of traits to use instead of those on the

identity model itself

:return: True if the identity is in the segment, False otherwise



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/flagsmith/engine/segments/evaluator.rb', line 27

def evaluate_identity_in_segment(identity, segment, override_traits = nil)
  segment.rules&.length&.positive? &&
    segment.rules.all? do |rule|
      traits_match_segment_rule(
        override_traits || identity.identity_traits,
        rule,
        segment.id,
        identity.django_id || identity.composite_key
      )
    end
end

#get_identity_segments(environment, identity, override_traits = nil) ⇒ Object



14
15
16
17
18
# File 'lib/flagsmith/engine/segments/evaluator.rb', line 14

def get_identity_segments(environment, identity, override_traits = nil)
  environment.project.segments.select do |s|
    evaluate_identity_in_segment(identity, s, override_traits)
  end
end

#traits_match_segment_condition(identity_traits, condition, segment_id, identity_id) ⇒ Object

rubocop:enable Metrics/MethodLength



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/flagsmith/engine/segments/evaluator.rb', line 57

def traits_match_segment_condition(identity_traits, condition, segment_id, identity_id)
  if condition.operator == PERCENTAGE_SPLIT
    return hashed_percentage_for_object_ids([segment_id,
                                             identity_id]) <= condition.value.to_f
  end

  trait = identity_traits.find { |t| t.key.to_s == condition.property }

  return handle_trait_existence_conditions(trait, condition.operator) if [IS_SET,
                                                                          IS_NOT_SET].include?(condition.operator)

  return condition.match_trait_value?(trait.trait_value) if trait

  false
end

#traits_match_segment_rule(identity_traits, rule, segment_id, identity_id) ⇒ Object

rubocop:disable Metrics/MethodLength



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/flagsmith/engine/segments/evaluator.rb', line 40

def traits_match_segment_rule(identity_traits, rule, segment_id, identity_id)
  matching_block = lambda { |condition|
    traits_match_segment_condition(identity_traits, condition, segment_id, identity_id)
  }

  matches_conditions =
    if rule.conditions&.length&.positive?
      rule.conditions.send(rule.matching_function, &matching_block)
    else
      true
    end

  matches_conditions &&
    rule.rules.all? { |r| traits_match_segment_rule(identity_traits, r, segment_id, identity_id) }
end