Class: Optimizely::ConditionEvaluator

Inherits:
Object
  • Object
show all
Defined in:
lib/optimizely/condition.rb

Constant Summary collapse

DEFAULT_OPERATOR_TYPES =
[
  ConditionalOperatorTypes::AND,
  ConditionalOperatorTypes::OR,
  ConditionalOperatorTypes::NOT
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_attributes) ⇒ ConditionEvaluator

Returns a new instance of ConditionEvaluator.



19
20
21
# File 'lib/optimizely/condition.rb', line 19

def initialize(user_attributes)
  @user_attributes = user_attributes
end

Instance Attribute Details

#user_attributesObject (readonly)

Returns the value of attribute user_attributes.



17
18
19
# File 'lib/optimizely/condition.rb', line 17

def user_attributes
  @user_attributes
end

Instance Method Details

#and_evaluator(conditions) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/optimizely/condition.rb', line 23

def and_evaluator(conditions)
  # Evaluates an array of conditions as if the evaluator had been applied
  # to each entry and the results AND-ed together.
  #
  # conditions - Array of conditions ex: [operand_1, operand_2]
  #
  # Returns boolean true if all operands evaluate to true.

  conditions.each do |condition|
    result = evaluate(condition)
    return result if result == false
  end

  true
end

#evaluate(conditions) ⇒ Object



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
# File 'lib/optimizely/condition.rb', line 78

def evaluate(conditions)
  # 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 result of evaluating the conditions evaluated.

  if conditions.is_a? Array
    operator_type = conditions[0]
    return false unless DEFAULT_OPERATOR_TYPES.include?(operator_type)
    case operator_type
    when ConditionalOperatorTypes::AND
      return and_evaluator(conditions[1..-1])
    when ConditionalOperatorTypes::OR
      return or_evaluator(conditions[1..-1])
    when ConditionalOperatorTypes::NOT
      return not_evaluator(conditions[1..-1])
    end
  end

  # Create array of condition key and corresponding value of audience condition.
  condition_array = audience_condition_deserializer(conditions)

  # Compare audience condition against provided user data i.e. attributes.
  evaluator(condition_array)
end

#evaluator(condition_array) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/optimizely/condition.rb', line 68

def evaluator(condition_array)
  # Method to compare single audience condition against provided user data i.e. attributes.
  #
  # condition_array - Array consisting of condition key and corresponding value.
  #
  # Returns boolean indicating the result of comparing the condition value against the user attributes.

  condition_array[1] == @user_attributes[condition_array[0]]
end

#not_evaluator(single_condition) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/optimizely/condition.rb', line 55

def not_evaluator(single_condition)
  # Evaluates an array of conditions as if the evaluator had been applied
  # to a single entry and NOT was applied to the result.
  #
  # single_condition - Array of a single condition ex: [operand_1]
  #
  # Returns boolean true if the operand evaluates to false.

  return false if single_condition.length != 1

  !evaluate(single_condition[0])
end

#or_evaluator(conditions) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/optimizely/condition.rb', line 39

def or_evaluator(conditions)
  # Evaluates an array of conditions as if the evaluator had been applied
  # to each entry and the results AND-ed together.
  #
  # conditions - Array of conditions ex: [operand_1, operand_2]
  #
  # Returns boolean true if any operand evaluates to true.

  conditions.each do |condition|
    result = evaluate(condition)
    return result if result == true
  end

  false
end