class Kameleoon::Targeting::CustomDatum

Attributes

index[RW]
operator[RW]
value[RW]

Public Class Methods

new(json_condition) click to toggle source
# File lib/kameleoon/targeting/conditions/custom_datum.rb, line 12
def initialize(json_condition)
  if json_condition['customDataIndex'].nil?
    raise Exception::NotFoundError.new('customDataIndex')
  end
  @index = json_condition['customDataIndex']

  if json_condition['valueMatchType'].nil?
    raise Exception::NotFoundError.new('valueMatchType')
  end
  @operator = json_condition['valueMatchType']

  if json_condition['value'].nil?
    raise Exception::NotFoundError.new('value')
  end
  @value = json_condition['value']

  @type = ConditionType::CUSTOM_DATUM

  if json_condition['include'].nil?
    raise Exception::NotFoundError.new('include')
  end
  @include = json_condition['include']
end

Public Instance Methods

check(datas) click to toggle source
# File lib/kameleoon/targeting/conditions/custom_datum.rb, line 36
def check(datas)
  is_targeted = false
  custom_data = datas.select { |data| data.instance == DataType::CUSTOM && data.id == @index }.first
  if custom_data.nil?
    is_targeted = (@operator == Operator::UNDEFINED.to_s)
  else
    case @operator
    when Operator::MATCH.to_s
      if Regexp.new(@value.to_s).match(custom_data.value.to_s)
        is_targeted = true
      end
    when Operator::CONTAINS.to_s
      if custom_data.value.to_s.include? @value
        is_targeted = true
      end
    when Operator::EXACT.to_s
      if custom_data.value.to_s == @value.to_s
        is_targeted = true
      end
    when Operator::EQUAL.to_s
      if custom_data.value.to_f == @value.to_f
        is_targeted = true
      end
    when Operator::GREATER.to_s
      if custom_data.value.to_f > @value.to_f
        is_targeted = true
      end
    when Operator::LOWER.to_s
      if custom_data.value.to_f < @value.to_f
        is_targeted = true
      end
    when Operator::IS_TRUE.to_s
      if custom_data.value == true
        is_targeted = true
      end
    when Operator::IS_FALSE.to_s
      if custom_data.value == false
        is_targeted = true
      end
    else
      raise KameleoonError.new("Undefined operator " + @operator.to_s)
    end
  end
  is_targeted
end