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
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_attributes) ⇒ ConditionEvaluator

Returns a new instance of ConditionEvaluator.



36
37
38
# File 'lib/optimizely/condition.rb', line 36

def initialize(user_attributes)
  @user_attributes = user_attributes
end

Instance Attribute Details

#user_attributesObject (readonly)

Returns the value of attribute user_attributes.



34
35
36
# File 'lib/optimizely/condition.rb', line 34

def user_attributes
  @user_attributes
end

Instance Method Details

#and_evaluator(conditions) ⇒ Object



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

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



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

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



85
86
87
88
89
90
91
92
93
# File 'lib/optimizely/condition.rb', line 85

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



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/optimizely/condition.rb', line 72

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



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

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