module Rseed::AttributeConverters

Public Instance Methods

deserialize_boolean(value) click to toggle source
# File lib/rseed/attribute_converters.rb, line 18
def deserialize_boolean value
  /^y|t/.match(value.strip.downcase) ? true : false
end
deserialize_clean_string(value) click to toggle source
# File lib/rseed/attribute_converters.rb, line 11
def deserialize_clean_string(value)
  value = value.to_i if (value.to_i == value.to_f) if /^\s*[\d]+(\.0+){0,1}\s*$/.match(value.to_s)
  value = value.gsub(/[^A-Za-z0-9 \.,\?'""!@#\$%\^&\*\(\)-_=\+;:<>\/\\\|\}\{\[\]`~]/, '').strip if value.is_a?(String)
  return nil if value.to_s.blank? || value.to_s.nil?
  value.to_s
end
deserialize_date(s) click to toggle source
# File lib/rseed/attribute_converters.rb, line 26
def deserialize_date s
  return nil if (s.nil? || s.blank?)
  return Date.strptime(s, "%d/%m/%y") if /^[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{2}$/.match(s)
  return DateTime.new(1899, 12, 30) + s.to_f if s.to_f unless s !~ /^\s*[+-]?((\d+_?)*\d+(\.(\d+_?)*\d+)?|\.(\d+_?)*\d+)(\s*|([eE][+-]?(\d+_?)*\d+)\s*)$/
  begin
    result = Date.parse(s)
  rescue
    Rseed.logger.error "Could not parse date ".red + "'#{s}'"
  end

  return result
end
deserialize_datetime(s) click to toggle source
# File lib/rseed/attribute_converters.rb, line 39
def deserialize_datetime s
  begin
    result = Time.zone.parse(s)
  rescue
    Rseed.logger.error "Could not parse datetime ".red + "'#{s}'"
  end

  return result
end
deserialize_decimal(value) click to toggle source
# File lib/rseed/attribute_converters.rb, line 22
def deserialize_decimal value
  BigDecimal(value)
end
deserialize_string(value) click to toggle source
# File lib/rseed/attribute_converters.rb, line 3
def deserialize_string(value)
  # If the string represents a number with .0 on the end (something that comes from Roo excel, then remove it)
  # this is a big problem with phone numbers.
  value.gsub!(/\.0+$/, '') if (value.to_i == value.to_f) if /^\s*[\d]+(\.0+){0,1}\s*$/.match(value.to_s)
  return nil if value.to_s.blank? || value.to_s.nil?
  value.to_s
end