Class: Kameleoon::Targeting::CustomDatum Private

Inherits:
Condition
  • Object
show all
Includes:
Exception
Defined in:
lib/kameleoon/targeting/conditions/custom_datum.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

CustomDatum represents an instance of Custom Data condition from back office

Constant Summary collapse

@@op =

This classvariable is part of a private API. You should avoid using this classvariable if possible, as it may be removed or be changed in the future.

{
  Operator::UNDEFINED => method(:op_undefined),
  Operator::REGULAR_EXPRESSION => method(:op_match),
  Operator::CONTAINS => method(:op_contains),
  Operator::EXACT => method(:op_exact),
  Operator::EQUAL => method(:op_equal),
  Operator::GREATER => method(:op_greater),
  Operator::LOWER => method(:op_lower),
  Operator::IS_TRUE => method(:op_is_true),
  Operator::IS_FALSE => method(:op_is_false),
  Operator::AMONG_VALUES => method(:op_among_values)
}

Instance Attribute Summary

Attributes inherited from Condition

#id, #include, #type

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json_condition) ⇒ CustomDatum

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of CustomDatum.



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/kameleoon/targeting/conditions/custom_datum.rb', line 72

def initialize(json_condition)
  super(json_condition)

  if json_condition['valueMatchType'].nil?
    raise Exception::NotFound.new('valueMatchType'), 'valueMatchType missed'
  end

  @type = ConditionType::CUSTOM_DATUM
  @index = json_condition['customDataIndex'].to_i
  @operator = json_condition['valueMatchType']
  @operation = @@op[@operator]
  @value = json_condition['value']
end

Class Method Details

.op_among_values(values, value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



53
54
55
56
# File 'lib/kameleoon/targeting/conditions/custom_datum.rb', line 53

def op_among_values(values, value)
  all_matches = JSON.parse(value.to_s).map(&:to_s).to_set
  values.any? { |v| all_matches.include?(v) }
end

.op_contains(values, value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



24
25
26
# File 'lib/kameleoon/targeting/conditions/custom_datum.rb', line 24

def op_contains(values, value)
  values.any? { |v| v.to_s.include? value.to_s }
end

.op_equal(values, value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



32
33
34
35
# File 'lib/kameleoon/targeting/conditions/custom_datum.rb', line 32

def op_equal(values, value)
  epsilon = 1e-9
  values.any? { |v| (v.to_f - value.to_f).abs < epsilon }
end

.op_exact(values, value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



28
29
30
# File 'lib/kameleoon/targeting/conditions/custom_datum.rb', line 28

def op_exact(values, value)
  values.include? value.to_s
end

.op_greater(values, value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



37
38
39
# File 'lib/kameleoon/targeting/conditions/custom_datum.rb', line 37

def op_greater(values, value)
  values.any? { |v| v.to_f > value.to_f }
end

.op_is_false(values, _value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



49
50
51
# File 'lib/kameleoon/targeting/conditions/custom_datum.rb', line 49

def op_is_false(values, _value)
  values.any? { |v| v.to_s == 'false' }
end

.op_is_true(values, _value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



45
46
47
# File 'lib/kameleoon/targeting/conditions/custom_datum.rb', line 45

def op_is_true(values, _value)
  values.any? { |v| v.to_s == 'true' }
end

.op_lower(values, value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



41
42
43
# File 'lib/kameleoon/targeting/conditions/custom_datum.rb', line 41

def op_lower(values, value)
  values.any? { |v| v.to_f < value.to_f }
end

.op_match(values, value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



19
20
21
22
# File 'lib/kameleoon/targeting/conditions/custom_datum.rb', line 19

def op_match(values, value)
  re = Regexp.new(value.to_s)
  values.any? { |v| re.match(v) }
end

.op_undefined(values, value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



15
16
17
# File 'lib/kameleoon/targeting/conditions/custom_datum.rb', line 15

def op_undefined(values, value)
  false
end

Instance Method Details

#check(custom_data_storage) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/kameleoon/targeting/conditions/custom_datum.rb', line 86

def check(custom_data_storage)
  is_targeted = nil
  if custom_data_storage.is_a?(Kameleoon::DataManager::DataMapStorage)
    custom_data = custom_data_storage.get(@index)
    if custom_data.is_a?(Kameleoon::CustomData)
      unless @operation
        raise KameleoonError.new("Undefined operator #{@operator}"), "Undefined operator #{@operator}"
      end
      is_targeted = @operation.call(custom_data.values, @value)
    end
  end
  is_targeted = (@operator == Operator::UNDEFINED.to_s) if is_targeted.nil?
  is_targeted
end