Module: VWO::Utils::Segment

Includes:
Enums
Included in:
Services::OperandEvaluator, Services::SegmentEvaluator
Defined in:
lib/vwo/utils/segment.rb

Constant Summary collapse

GROUPING_PATTERN =
/^(.+?)\((.*)\)/
WILDCARD_PATTERN =
/(^\*|^)(.+?)(\*$|$)/

Instance Method Summary collapse

Instance Method Details

#convert_to_true_types(operator_value, custom_variables_value) ⇒ [String, String]

Extracts true values represented in the args, and returns stringified value of it

Parameters:

  • :operator_value (String)

    operand/dsl leaf value

  • :custom_variables_value (String|Number)

    custom_variables value

Returns:

  • ([String, String])

    tuple of str value of operator_value, custom_variables_value converted into their true types



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/vwo/utils/segment.rb', line 31

def convert_to_true_types(operator_value, custom_variables_value)
  # This is atomic, either both values will be processed or none
  begin
    true_type_operator_value = Kernel::Float(operator_value)
    true_type_custom_variables_value = Kernel::Float(custom_variables_value)
  rescue StandardError => _e
    return operator_value, custom_variables_value
  end
  # Now both are float, So, convert them independently to int type if they are int rather than floats
  true_type_operator_value = true_type_operator_value.to_i if true_type_operator_value == true_type_operator_value.floor

  true_type_custom_variables_value = true_type_custom_variables_value.to_i if true_type_custom_variables_value == true_type_custom_variables_value.floor

  # Convert them back to string and return
  [true_type_operator_value.to_s, true_type_custom_variables_value.to_s]
end

#process_custom_variables_value(custom_variables_value) ⇒ String

Processes the value from the custom_variables_variables @param :custom_variables_value the custom_variables_value provided inside custom_variables

Returns:

  • (String)

    stringified value of processed custom_variables_value



65
66
67
68
69
70
71
72
# File 'lib/vwo/utils/segment.rb', line 65

def process_custom_variables_value(custom_variables_value)
  return '' if custom_variables_value.nil?

  if custom_variables_value.is_a?(TrueClass) || custom_variables_value.is_a?(FalseClass)
    custom_variables_value = custom_variables_value ? OperandValuesBooleanTypes::TRUE : OperandValuesBooleanTypes::FALSE
  end
  custom_variables_value.to_s
end

#process_operand_value(operand) ⇒ Object

Extracts operand_type and operand_value from the leaf_node/operand @param :operand String value from the leaf_node @return[[String, String]] Tuple of defined operand_types and operand_value



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
# File 'lib/vwo/utils/segment.rb', line 78

def process_operand_value(operand)
  # Separate the operand type and value inside the bracket

  operand_type_name, operand_value = separate_operand(operand)

  # Enum the operand type, here lower, regex, and equals will be identified
  operand_type =
    begin
      VWO::Enums::OperandValueTypesName.const_get(operand_type_name.upcase)
    rescue StandardError => _e
      nil
    end

  # In case of wildcard, the operand type is further divided into contains, startswith and endswith
  if operand_type_name == OperandValueTypesName::WILDCARD
    starting_star, operand_value, ending_star = WILDCARD_PATTERN.match(operand_value)[1..3]
    operand_type =
      if starting_star.to_s.length > 0 && ending_star.to_s.length > 0
        OperandValueTypes::CONTAINS
      elsif starting_star.to_s.length > 0
        OperandValueTypes::STARTS_WITH
      elsif ending_star.to_s.length > 0
        OperandValueTypes::ENDS_WITH
      else
        OperandValueTypes::EQUALS
      end
  elsif operand_type_name == OperandValueTypesName::GREATER_THAN
    operand_type = OperandValueTypes::GREATER_THAN
  elsif operand_type_name == OperandValueTypesName::LESS_THAN
    operand_type = OperandValueTypes::LESS_THAN
  elsif operand_type_name == OperandValueTypesName::GREATER_THAN_EQUAL_TO
    operand_type = OperandValueTypes::GREATER_THAN_EQUAL_TO
  elsif operand_type_name == OperandValueTypesName::LESS_THAN_EQUAL_TO
    operand_type = OperandValueTypes::LESS_THAN_EQUAL_TO
  end

  # In case there is an abnormal patter, it would have passed all the above if cases, which means it
  # should be equals, so set the whole operand as operand value and operand type as equals
  if operand_type.nil?
    operand_type = OperandValueTypes::EQUALS
    operand_value = operand
  end
  [operand_type, operand_value]
end

#separate_operand(operand) ⇒ [String, String]

Extract the operand_type, ie. lower, wildcard, regex or equals

@param :operand string value from leaf_node of dsl

Returns:

  • ([String, String])

    tuple of operand value and operand type



53
54
55
56
57
58
# File 'lib/vwo/utils/segment.rb', line 53

def separate_operand(operand)
  groups = GROUPING_PATTERN.match(operand)
  return groups[1..2] if groups

  [OperandValueTypesName::EQUALS, operand]
end