|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | 3 | require_relative 'constants' |
| 4 | +require_relative 'models' |
4 | 5 | require_relative '../utils/hash_func' |
5 | 6 |
|
6 | 7 | module Flagsmith |
7 | 8 | module Engine |
8 | 9 | module Segments |
9 | 10 | # Evaluator methods |
10 | 11 | module Evaluator |
| 12 | + extend self |
11 | 13 | include Flagsmith::Engine::Segments::Constants |
12 | 14 | include Flagsmith::Engine::Utils::HashFunc |
13 | 15 |
|
| 16 | + # Context-based segment evaluation (new approach) |
| 17 | + # Returns all segments that the identity belongs to based on segment rules evaluation |
| 18 | + # |
| 19 | + # @param context [Hash] Evaluation context containing identity and segment definitions |
| 20 | + # @return [Array<Hash>] Array of segments that the identity matches |
| 21 | + def get_identity_segments_from_context(context) |
| 22 | + return [] unless context[:identity] && context[:segments] |
| 23 | + |
| 24 | + context[:segments].values.select do |segment| |
| 25 | + next false if segment[:rules].nil? || segment[:rules].empty? |
| 26 | + |
| 27 | + segment[:rules].all? { |rule| traits_match_segment_rule_from_context(rule, segment[:key], context) } |
| 28 | + end |
| 29 | + end |
| 30 | + |
| 31 | + # Model-based segment evaluation (existing approach) |
14 | 32 | def get_identity_segments(environment, identity, override_traits = nil) |
15 | 33 | environment.project.segments.select do |s| |
16 | 34 | evaluate_identity_in_segment(identity, s, override_traits) |
@@ -70,6 +88,163 @@ def traits_match_segment_condition(identity_traits, condition, segment_id, ident |
70 | 88 | false |
71 | 89 | end |
72 | 90 |
|
| 91 | + # Context-based helper functions (new approach) |
| 92 | + |
| 93 | + # Evaluates whether a segment rule matches using context |
| 94 | + # |
| 95 | + # @param rule [Hash] The rule to evaluate |
| 96 | + # @param segment_key [String] The segment key (used for percentage split) |
| 97 | + # @param context [Hash] The evaluation context |
| 98 | + # @return [Boolean] True if the rule matches |
| 99 | + def traits_match_segment_rule_from_context(rule, segment_key, context) |
| 100 | + matches_conditions = evaluate_conditions_from_context(rule, segment_key, context) |
| 101 | + matches_sub_rules = evaluate_sub_rules_from_context(rule, segment_key, context) |
| 102 | + |
| 103 | + matches_conditions && matches_sub_rules |
| 104 | + end |
| 105 | + |
| 106 | + # Evaluates rule conditions based on rule type (ALL/ANY/NONE) |
| 107 | + # |
| 108 | + # @param rule [Hash] The rule containing conditions and type |
| 109 | + # @param segment_key [String] The segment key |
| 110 | + # @param context [Hash] The evaluation context |
| 111 | + # @return [Boolean] True if conditions match according to rule type |
| 112 | + def evaluate_conditions_from_context(rule, segment_key, context) |
| 113 | + return true if rule[:conditions].nil? || rule[:conditions].empty? |
| 114 | + |
| 115 | + condition_results = rule[:conditions].map do |condition| |
| 116 | + traits_match_segment_condition_from_context(condition, segment_key, context) |
| 117 | + end |
| 118 | + |
| 119 | + evaluate_rule_conditions(rule[:type], condition_results) |
| 120 | + end |
| 121 | + |
| 122 | + # Evaluates nested sub-rules |
| 123 | + # |
| 124 | + # @param rule [Hash] The rule containing nested rules |
| 125 | + # @param segment_key [String] The segment key |
| 126 | + # @param context [Hash] The evaluation context |
| 127 | + # @return [Boolean] True if all sub-rules match |
| 128 | + def evaluate_sub_rules_from_context(rule, segment_key, context) |
| 129 | + return true if rule[:rules].nil? || rule[:rules].empty? |
| 130 | + |
| 131 | + rule[:rules].all? do |sub_rule| |
| 132 | + traits_match_segment_rule_from_context(sub_rule, segment_key, context) |
| 133 | + end |
| 134 | + end |
| 135 | + |
| 136 | + # Evaluates a single segment condition using context |
| 137 | + # |
| 138 | + # @param condition [Hash] The condition to evaluate |
| 139 | + # @param segment_key [String] The segment key (used for percentage split hashing) |
| 140 | + # @param context [Hash] The evaluation context |
| 141 | + # @return [Boolean] True if the condition matches |
| 142 | + def traits_match_segment_condition_from_context(condition, segment_key, context) |
| 143 | + if condition[:operator] == PERCENTAGE_SPLIT |
| 144 | + context_value_key = get_context_value(condition[:property], context) || get_identity_key_from_context(context) |
| 145 | + hashed_percentage = hashed_percentage_for_object_ids([segment_key, context_value_key]) |
| 146 | + return hashed_percentage <= condition[:value].to_f |
| 147 | + end |
| 148 | + |
| 149 | + return false if condition[:property].nil? |
| 150 | + |
| 151 | + trait_value = get_trait_value(condition[:property], context) |
| 152 | + |
| 153 | + return trait_value != nil if condition[:operator] == IS_SET |
| 154 | + return trait_value.nil? if condition[:operator] == IS_NOT_SET |
| 155 | + |
| 156 | + if !trait_value.nil? |
| 157 | + # Reuse existing Condition class logic |
| 158 | + condition_obj = Flagsmith::Engine::Segments::Condition.new( |
| 159 | + operator: condition[:operator], |
| 160 | + value: condition[:value], |
| 161 | + property: condition[:property] |
| 162 | + ) |
| 163 | + return condition_obj.match_trait_value?(trait_value) |
| 164 | + end |
| 165 | + |
| 166 | + false |
| 167 | + end |
| 168 | + |
| 169 | + # Evaluate rule conditions based on type (ALL/ANY/NONE) |
| 170 | + # |
| 171 | + # @param rule_type [String] The rule type |
| 172 | + # @param condition_results [Array<Boolean>] Array of condition evaluation results |
| 173 | + # @return [Boolean] True if conditions match according to rule type |
| 174 | + def evaluate_rule_conditions(rule_type, condition_results) |
| 175 | + case rule_type |
| 176 | + when 'ALL' |
| 177 | + condition_results.empty? || condition_results.all? |
| 178 | + when 'ANY' |
| 179 | + !condition_results.empty? && condition_results.any? |
| 180 | + when 'NONE' |
| 181 | + condition_results.empty? || condition_results.none? |
| 182 | + else |
| 183 | + false |
| 184 | + end |
| 185 | + end |
| 186 | + |
| 187 | + # Get trait value from context, supporting JSONPath expressions |
| 188 | + # |
| 189 | + # @param property [String] The property name or JSONPath |
| 190 | + # @param context [Hash] The evaluation context |
| 191 | + # @return [Object, nil] The trait value or nil |
| 192 | + def get_trait_value(property, context) |
| 193 | + # Check if it's a JSONPath expression |
| 194 | + if property.start_with?('$.') |
| 195 | + context_value = get_context_value(property, context) |
| 196 | + return context_value unless non_primitive?(context_value) |
| 197 | + end |
| 198 | + |
| 199 | + # Otherwise look in traits |
| 200 | + traits = context.dig(:identity, :traits) || {} |
| 201 | + traits[property] |
| 202 | + end |
| 203 | + |
| 204 | + # Get value from context using JSONPath-like syntax |
| 205 | + # |
| 206 | + # @param json_path [String] JSONPath expression (e.g., '$.identity.identifier') |
| 207 | + # @param context [Hash] The evaluation context |
| 208 | + # @return [Object, nil] The value at the path or nil |
| 209 | + def get_context_value(json_path, context) |
| 210 | + return nil unless context && json_path&.start_with?('$.') |
| 211 | + |
| 212 | + # Simple JSONPath implementation - handle basic cases |
| 213 | + path_parts = json_path.sub('$.', '').split('.') |
| 214 | + current = context |
| 215 | + |
| 216 | + path_parts.each do |part| |
| 217 | + return nil unless current.is_a?(Hash) |
| 218 | + |
| 219 | + current = current[part.to_sym] |
| 220 | + end |
| 221 | + |
| 222 | + current |
| 223 | + rescue StandardError |
| 224 | + nil |
| 225 | + end |
| 226 | + |
| 227 | + # Get identity key from context |
| 228 | + # |
| 229 | + # @param context [Hash] The evaluation context |
| 230 | + # @return [String, nil] The identity key or generated composite key |
| 231 | + def get_identity_key_from_context(context) |
| 232 | + return nil unless context[:identity] |
| 233 | + |
| 234 | + context[:identity][:key] || |
| 235 | + "#{context[:environment][:key]}_#{context[:identity][:identifier]}" |
| 236 | + end |
| 237 | + |
| 238 | + # Check if value is non-primitive (object or array) |
| 239 | + # |
| 240 | + # @param value [Object] The value to check |
| 241 | + # @return [Boolean] True if value is an object or array |
| 242 | + def non_primitive?(value) |
| 243 | + return false if value.nil? |
| 244 | + |
| 245 | + value.is_a?(Hash) || value.is_a?(Array) |
| 246 | + end |
| 247 | + |
73 | 248 | private |
74 | 249 |
|
75 | 250 | def handle_trait_existence_conditions(matching_trait, operator) |
|
0 commit comments