Class: Optimizely::CustomAttributeConditionEvaluator
- Inherits:
-
Object
- Object
- Optimizely::CustomAttributeConditionEvaluator
- Defined in:
- lib/optimizely/custom_attribute_condition_evaluator.rb
Constant Summary collapse
- CUSTOM_ATTRIBUTE_CONDITION_TYPE =
'custom_attribute'
- EXACT_MATCH_TYPE =
Conditional match types
'exact'
- EXISTS_MATCH_TYPE =
'exists'
- GREATER_THAN_MATCH_TYPE =
'gt'
- GREATER_EQUAL_MATCH_TYPE =
'ge'
- LESS_THAN_MATCH_TYPE =
'lt'
- LESS_EQUAL_MATCH_TYPE =
'le'
- SUBSTRING_MATCH_TYPE =
'substring'
- SEMVER_EQ =
'semver_eq'
- SEMVER_GE =
'semver_ge'
- SEMVER_GT =
'semver_gt'
- SEMVER_LE =
'semver_le'
- SEMVER_LT =
'semver_lt'
- EVALUATORS_BY_MATCH_TYPE =
{ EXACT_MATCH_TYPE => :exact_evaluator, EXISTS_MATCH_TYPE => :exists_evaluator, GREATER_THAN_MATCH_TYPE => :greater_than_evaluator, GREATER_EQUAL_MATCH_TYPE => :greater_than_or_equal_evaluator, LESS_THAN_MATCH_TYPE => :less_than_evaluator, LESS_EQUAL_MATCH_TYPE => :less_than_or_equal_evaluator, SUBSTRING_MATCH_TYPE => :substring_evaluator, SEMVER_EQ => :semver_equal_evaluator, SEMVER_GE => :semver_greater_than_or_equal_evaluator, SEMVER_GT => :semver_greater_than_evaluator, SEMVER_LE => :semver_less_than_or_equal_evaluator, SEMVER_LT => :semver_less_than_evaluator }.freeze
Instance Attribute Summary collapse
-
#user_attributes ⇒ Object
readonly
Returns the value of attribute user_attributes.
Instance Method Summary collapse
- #evaluate(leaf_condition) ⇒ Object
- #exact_evaluator(condition) ⇒ Object
- #exists_evaluator(condition) ⇒ Object
- #greater_than_evaluator(condition) ⇒ Object
- #greater_than_or_equal_evaluator(condition) ⇒ Object
-
#initialize(user_attributes, logger) ⇒ CustomAttributeConditionEvaluator
constructor
A new instance of CustomAttributeConditionEvaluator.
- #less_than_evaluator(condition) ⇒ Object
- #less_than_or_equal_evaluator(condition) ⇒ Object
- #semver_equal_evaluator(condition) ⇒ Object
- #semver_greater_than_evaluator(condition) ⇒ Object
- #semver_greater_than_or_equal_evaluator(condition) ⇒ Object
- #semver_less_than_evaluator(condition) ⇒ Object
- #semver_less_than_or_equal_evaluator(condition) ⇒ Object
- #substring_evaluator(condition) ⇒ Object
Constructor Details
#initialize(user_attributes, logger) ⇒ CustomAttributeConditionEvaluator
Returns a new instance of CustomAttributeConditionEvaluator.
58 59 60 61 |
# File 'lib/optimizely/custom_attribute_condition_evaluator.rb', line 58 def initialize(user_attributes, logger) @user_attributes = user_attributes @logger = logger end |
Instance Attribute Details
#user_attributes ⇒ Object (readonly)
Returns the value of attribute user_attributes.
56 57 58 |
# File 'lib/optimizely/custom_attribute_condition_evaluator.rb', line 56 def user_attributes @user_attributes end |
Instance Method Details
#evaluate(leaf_condition) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/optimizely/custom_attribute_condition_evaluator.rb', line 63 def evaluate(leaf_condition) # Top level method to evaluate audience conditions. # # conditions - Nested array of and/or conditions. # Example: ['and', operand_1, ['or', operand_2, operand_3]] # # Returns boolean if the given user attributes match/don't match the given conditions, # nil if the given conditions can't be evaluated. unless leaf_condition['type'] == CUSTOM_ATTRIBUTE_CONDITION_TYPE @logger.log( Logger::WARN, format(Helpers::Constants::AUDIENCE_EVALUATION_LOGS['UNKNOWN_CONDITION_TYPE'], leaf_condition) ) return nil end condition_match = leaf_condition['match'] || EXACT_MATCH_TYPE if !@user_attributes.key?(leaf_condition['name']) && condition_match != EXISTS_MATCH_TYPE @logger.log( Logger::DEBUG, format( Helpers::Constants::AUDIENCE_EVALUATION_LOGS['MISSING_ATTRIBUTE_VALUE'], leaf_condition, leaf_condition['name'] ) ) return nil end if @user_attributes[leaf_condition['name']].nil? && condition_match != EXISTS_MATCH_TYPE @logger.log( Logger::DEBUG, format( Helpers::Constants::AUDIENCE_EVALUATION_LOGS['NULL_ATTRIBUTE_VALUE'], leaf_condition, leaf_condition['name'] ) ) return nil end unless EVALUATORS_BY_MATCH_TYPE.include?(condition_match) @logger.log( Logger::WARN, format(Helpers::Constants::AUDIENCE_EVALUATION_LOGS['UNKNOWN_MATCH_TYPE'], leaf_condition) ) return nil end begin send(EVALUATORS_BY_MATCH_TYPE[condition_match], leaf_condition) rescue InvalidAttributeType condition_name = leaf_condition['name'] user_value = @user_attributes[condition_name] @logger.log( Logger::WARN, format( Helpers::Constants::AUDIENCE_EVALUATION_LOGS['UNEXPECTED_TYPE'], leaf_condition, user_value.class, condition_name ) ) return nil rescue InvalidSemanticVersion condition_name = leaf_condition['name'] @logger.log( Logger::WARN, format( Helpers::Constants::AUDIENCE_EVALUATION_LOGS['INVALID_SEMANTIC_VERSION'], leaf_condition, condition_name ) ) return nil end end |
#exact_evaluator(condition) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/optimizely/custom_attribute_condition_evaluator.rb', line 145 def exact_evaluator(condition) # Evaluate the given exact match condition for the given user attributes. # # Returns boolean true if numbers values matched, i.e 2 is equal to 2.0 # true if the user attribute value is equal (===) to the condition value, # false if the user attribute value is not equal (!==) to the condition value, # nil if the condition value or user attribute value has an invalid type, # or if there is a mismatch between the user attribute type and the condition value type. condition_value = condition['value'] user_provided_value = @user_attributes[condition['name']] if !value_type_valid_for_exact_conditions?(condition_value) || (condition_value.is_a?(Numeric) && !Helpers::Validator.finite_number?(condition_value)) @logger.log( Logger::WARN, format(Helpers::Constants::AUDIENCE_EVALUATION_LOGS['UNKNOWN_CONDITION_VALUE'], condition) ) return nil end if !value_type_valid_for_exact_conditions?(user_provided_value) || !Helpers::Validator.same_types?(condition_value, user_provided_value) raise InvalidAttributeType end if user_provided_value.is_a?(Numeric) && !Helpers::Validator.finite_number?(user_provided_value) @logger.log( Logger::WARN, format( Helpers::Constants::AUDIENCE_EVALUATION_LOGS['INFINITE_ATTRIBUTE_VALUE'], condition, condition['name'] ) ) return nil end condition_value == user_provided_value end |
#exists_evaluator(condition) ⇒ Object
187 188 189 190 191 192 193 194 195 |
# File 'lib/optimizely/custom_attribute_condition_evaluator.rb', line 187 def exists_evaluator(condition) # Evaluate the given exists match condition for the given user attributes. # Returns boolean true if both: # 1) the user attributes have a value for the given condition, and # 2) the user attribute value is neither nil nor undefined # Returns false otherwise !@user_attributes[condition['name']].nil? end |
#greater_than_evaluator(condition) ⇒ Object
197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/optimizely/custom_attribute_condition_evaluator.rb', line 197 def greater_than_evaluator(condition) # Evaluate the given greater than match condition for the given user attributes. # Returns boolean true if the user attribute value is greater than the condition value, # false if the user attribute value is less than or equal to the condition value, # nil if the condition value isn't a number or the user attribute value isn't a number. condition_value = condition['value'] user_provided_value = @user_attributes[condition['name']] return nil unless valid_numeric_values?(user_provided_value, condition_value, condition) user_provided_value > condition_value end |
#greater_than_or_equal_evaluator(condition) ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/optimizely/custom_attribute_condition_evaluator.rb', line 211 def greater_than_or_equal_evaluator(condition) # Evaluate the given greater than or equal match condition for the given user attributes. # Returns boolean true if the user attribute value is greater than or equal to the condition value, # false if the user attribute value is less than the condition value, # nil if the condition value isn't a number or the user attribute value isn't a number. condition_value = condition['value'] user_provided_value = @user_attributes[condition['name']] return nil unless valid_numeric_values?(user_provided_value, condition_value, condition) user_provided_value >= condition_value end |
#less_than_evaluator(condition) ⇒ Object
225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/optimizely/custom_attribute_condition_evaluator.rb', line 225 def less_than_evaluator(condition) # Evaluate the given less than match condition for the given user attributes. # Returns boolean true if the user attribute value is less than the condition value, # false if the user attribute value is greater than or equal to the condition value, # nil if the condition value isn't a number or the user attribute value isn't a number. condition_value = condition['value'] user_provided_value = @user_attributes[condition['name']] return nil unless valid_numeric_values?(user_provided_value, condition_value, condition) user_provided_value < condition_value end |
#less_than_or_equal_evaluator(condition) ⇒ Object
239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/optimizely/custom_attribute_condition_evaluator.rb', line 239 def less_than_or_equal_evaluator(condition) # Evaluate the given less than or equal match condition for the given user attributes. # Returns boolean true if the user attribute value is less than or equal to the condition value, # false if the user attribute value is greater than the condition value, # nil if the condition value isn't a number or the user attribute value isn't a number. condition_value = condition['value'] user_provided_value = @user_attributes[condition['name']] return nil unless valid_numeric_values?(user_provided_value, condition_value, condition) user_provided_value <= condition_value end |
#semver_equal_evaluator(condition) ⇒ Object
275 276 277 278 279 280 281 282 283 284 |
# File 'lib/optimizely/custom_attribute_condition_evaluator.rb', line 275 def semver_equal_evaluator(condition) # Evaluate the given semantic version equal match target version for the user version. # Returns boolean true if the user version is equal to the target version, # false if the user version is not equal to the target version target_version = condition['value'] user_version = @user_attributes[condition['name']] SemanticVersion.compare_user_version_with_target_version(target_version, user_version).zero? end |
#semver_greater_than_evaluator(condition) ⇒ Object
286 287 288 289 290 291 292 293 294 295 |
# File 'lib/optimizely/custom_attribute_condition_evaluator.rb', line 286 def semver_greater_than_evaluator(condition) # Evaluate the given semantic version greater than match target version for the user version. # Returns boolean true if the user version is greater than the target version, # false if the user version is less than or equal to the target version target_version = condition['value'] user_version = @user_attributes[condition['name']] SemanticVersion.compare_user_version_with_target_version(target_version, user_version).positive? end |
#semver_greater_than_or_equal_evaluator(condition) ⇒ Object
297 298 299 300 301 302 303 304 305 306 |
# File 'lib/optimizely/custom_attribute_condition_evaluator.rb', line 297 def semver_greater_than_or_equal_evaluator(condition) # Evaluate the given semantic version greater than or equal to match target version for the user version. # Returns boolean true if the user version is greater than or equal to the target version, # false if the user version is less than the target version target_version = condition['value'] user_version = @user_attributes[condition['name']] SemanticVersion.compare_user_version_with_target_version(target_version, user_version) >= 0 end |
#semver_less_than_evaluator(condition) ⇒ Object
308 309 310 311 312 313 314 315 316 317 |
# File 'lib/optimizely/custom_attribute_condition_evaluator.rb', line 308 def semver_less_than_evaluator(condition) # Evaluate the given semantic version less than match target version for the user version. # Returns boolean true if the user version is less than the target version, # false if the user version is greater than or equal to the target version target_version = condition['value'] user_version = @user_attributes[condition['name']] SemanticVersion.compare_user_version_with_target_version(target_version, user_version).negative? end |
#semver_less_than_or_equal_evaluator(condition) ⇒ Object
319 320 321 322 323 324 325 326 327 328 |
# File 'lib/optimizely/custom_attribute_condition_evaluator.rb', line 319 def semver_less_than_or_equal_evaluator(condition) # Evaluate the given semantic version less than or equal to match target version for the user version. # Returns boolean true if the user version is less than or equal to the target version, # false if the user version is greater than the target version target_version = condition['value'] user_version = @user_attributes[condition['name']] SemanticVersion.compare_user_version_with_target_version(target_version, user_version) <= 0 end |
#substring_evaluator(condition) ⇒ Object
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/optimizely/custom_attribute_condition_evaluator.rb', line 253 def substring_evaluator(condition) # Evaluate the given substring match condition for the given user attributes. # Returns boolean true if the condition value is a substring of the user attribute value, # false if the condition value is not a substring of the user attribute value, # nil if the condition value isn't a string or the user attribute value isn't a string. condition_value = condition['value'] user_provided_value = @user_attributes[condition['name']] unless condition_value.is_a?(String) @logger.log( Logger::WARN, format(Helpers::Constants::AUDIENCE_EVALUATION_LOGS['UNKNOWN_CONDITION_VALUE'], condition) ) return nil end raise InvalidAttributeType unless user_provided_value.is_a?(String) user_provided_value.include? condition_value end |