module VWO::Utils::Feature

Public Instance Methods

get_type_casted_feature_value(value, variable_type) click to toggle source

Returns type casted value to given value type if possible. @param :value Value to type cast @param :variable_type Type to which value needs to be casted @return Type casted value if value can be type-casted

# File lib/vwo/utils/feature.rb, line 30
def get_type_casted_feature_value(value, variable_type)
  # Check if type(value) is already equal to required variable_type
  return value if RUBY_VARIABLE_TYPES[variable_type].include?(value.class)

  return value.to_s if variable_type == VariableTypes::STRING

  return value.to_i if variable_type == VariableTypes::INTEGER

  return value.to_f if variable_type == VariableTypes::DOUBLE

  return !value || value == 0 ? false : true if variable_type == VariableTypes.BOOLEAN

  return value if variable_type == VariableTypes::JSON
rescue StandardError => _e
  VWO::Logger.get_instance.log(
    LogLevelEnum::ERROR,
    format(
      LogMessageEnum::ErrorMessages::UNABLE_TO_TYPE_CAST,
      file: FileNameEnum::FeatureUtil,
      value: value,
      variable_type: variable_type,
      of_type: value.class.name
    )
  )
  nil
end