11# frozen_string_literal: true
22
3+ require 'json'
4+ require 'jsonpath'
35require_relative 'constants'
46require_relative 'models'
57require_relative '../utils/hash_func'
@@ -31,60 +33,7 @@ def get_identity_segments(context)
3133 matching_segments
3234 end
3335
34- # Evaluates whether a given identity is in the provided segment.
35- #
36- # :param identity: identity model object to evaluate
37- # :param segment: segment model object to evaluate
38- # :param override_traits: pass in a list of traits to use instead of those on the
39- # identity model itself
40- # :return: True if the identity is in the segment, False otherwise
41- def evaluate_identity_in_segment ( identity , segment , override_traits = nil )
42- segment . rules &.length &.positive? &&
43- segment . rules . all? do |rule |
44- traits_match_segment_rule (
45- override_traits || identity . identity_traits ,
46- rule ,
47- segment . id ,
48- identity . django_id || identity . composite_key
49- )
50- end
51- end
52-
53- # rubocop:disable Metrics/MethodLength
54- def traits_match_segment_rule ( identity_traits , rule , segment_id , identity_id )
55- matching_block = lambda { |condition |
56- traits_match_segment_condition ( identity_traits , condition , segment_id , identity_id )
57- }
58-
59- matches_conditions =
60- if rule . conditions &.length &.positive?
61- rule . conditions . send ( rule . matching_function , &matching_block )
62- else
63- true
64- end
65-
66- matches_conditions &&
67- rule . rules . all? { |r | traits_match_segment_rule ( identity_traits , r , segment_id , identity_id ) }
68- end
69- # rubocop:enable Metrics/MethodLength
70-
71- def traits_match_segment_condition ( identity_traits , condition , segment_id , identity_id )
72- if condition . operator == PERCENTAGE_SPLIT
73- return hashed_percentage_for_object_ids ( [ segment_id ,
74- identity_id ] ) <= condition . value . to_f
75- end
76-
77- trait = identity_traits . find { |t | t . key . to_s == condition . property }
78-
79- return handle_trait_existence_conditions ( trait , condition . operator ) if [ IS_SET ,
80- IS_NOT_SET ] . include? ( condition . operator )
81-
82- return condition . match_trait_value? ( trait . trait_value ) if trait
83-
84- false
85- end
86-
87- # Context-based helper functions (new approach)
36+ # Context-based helper functions
8837
8938 # Evaluates whether a segment rule matches using context
9039 #
@@ -143,9 +92,7 @@ def traits_match_segment_condition_from_context(condition, segment_key, context)
14392 end
14493
14594 return false if condition [ :property ] . nil?
146-
14795 trait_value = get_trait_value ( condition [ :property ] , context )
148-
14996 return trait_value != nil if condition [ :operator ] == IS_SET
15097 return trait_value . nil? if condition [ :operator ] == IS_NOT_SET
15198
@@ -197,25 +144,15 @@ def get_trait_value(property, context)
197144 traits [ property ] || traits [ property . to_sym ]
198145 end
199146
200- # Get value from context using JSONPath-like syntax
147+ # Get value from context using JSONPath syntax
201148 #
202149 # @param json_path [String] JSONPath expression (e.g., '$.identity.identifier')
203150 # @param context [Hash] The evaluation context
204151 # @return [Object, nil] The value at the path or nil
205152 def get_context_value ( json_path , context )
206153 return nil unless context && json_path &.start_with? ( '$.' )
207-
208- # Simple JSONPath implementation - handle basic cases
209- path_parts = json_path . sub ( '$.' , '' ) . split ( '.' )
210- current = context
211-
212- path_parts . each do |part |
213- return nil unless current . is_a? ( Hash )
214-
215- current = current [ part . to_sym ]
216- end
217-
218- current
154+ results = JsonPath . new ( json_path , use_symbols : true ) . on ( context )
155+ results . first
219156 rescue StandardError
220157 nil
221158 end
@@ -240,14 +177,6 @@ def non_primitive?(value)
240177
241178 value . is_a? ( Hash ) || value . is_a? ( Array )
242179 end
243-
244- private
245-
246- def handle_trait_existence_conditions ( matching_trait , operator )
247- return operator == IS_NOT_SET if matching_trait . nil?
248-
249- operator == IS_SET
250- end
251180 end
252181 end
253182 end
0 commit comments