module Optimizely::Helpers::VariableType
Public Instance Methods
cast_value_to_type(value, variable_type, logger)
click to toggle source
# File lib/optimizely/helpers/variable_type.rb, line 25 def cast_value_to_type(value, variable_type, logger) # Attempts to cast the given value to the specified type # # value - The string value to cast # variable_type - String variable type # # Returns the cast value or nil if not able to cast return_value = nil case variable_type when 'boolean' return_value = value == 'true' when 'double' begin return_value = Float(value) rescue => e logger.log(Logger::ERROR, "Unable to cast variable value '#{value}' to type "\ "'#{variable_type}': #{e.message}.") end when 'integer' begin return_value = Integer(value) rescue => e logger.log(Logger::ERROR, "Unable to cast variable value '#{value}' to type "\ "'#{variable_type}': #{e.message}.") end when 'json' begin return_value = JSON.parse(value) rescue => e logger.log(Logger::ERROR, "Unable to cast variable value '#{value}' to type "\ "'#{variable_type}': #{e.message}.") end else # default case is string return_value = value end return_value end