module DataConversions

Public Instance Methods

convert_to_array(value) click to toggle source
# File lib/data_conversions.rb, line 37
def convert_to_array(value)
  value = parse_json(value)
  value.is_a?(Array) ? value : Array(value)
end
convert_to_boolean(value) click to toggle source
# File lib/data_conversions.rb, line 22
def convert_to_boolean(value)
  return value if value.is_a?(TrueClass) || value.is_a?(FalseClass)
  value = eval(value)
  value.is_a?(TrueClass) || value.is_a?(FalseClass) ? value : nil
end
convert_to_date(value)
Alias for: convert_to_time
convert_to_date_time(value)
Alias for: convert_to_time
convert_to_float(value) click to toggle source
# File lib/data_conversions.rb, line 18
def convert_to_float(value)
  value.to_f
end
convert_to_hash(value) click to toggle source
# File lib/data_conversions.rb, line 32
def convert_to_hash(value)
  value = parse_json(value)
  value.to_h if value.respond_to? :to_h
end
convert_to_integer(value) click to toggle source
# File lib/data_conversions.rb, line 14
def convert_to_integer(value)
  value.to_i
end
convert_to_string(value) click to toggle source
# File lib/data_conversions.rb, line 3
def convert_to_string(value)
  value.to_s
end
convert_to_symbol(value) click to toggle source
# File lib/data_conversions.rb, line 28
def convert_to_symbol(value)
  value.to_sym
end
convert_to_time(value) click to toggle source
# File lib/data_conversions.rb, line 7
def convert_to_time(value)
  Object.const_get(__callee__.to_s.gsub('convert_to_', '').camelcase).parse value
end
parse_json(value) click to toggle source
# File lib/data_conversions.rb, line 42
def parse_json(value)
  value = JSON.parse value
rescue JSON::ParserError
  value
end