module PIM::MappingFunctionUtil

Public Instance Methods

conversion_error(value, type, e) click to toggle source
# File pim.rb, line 10597
def conversion_error value, type, e
  PIM.log_error("Could not parse '#{value}' to '#{type}'", e)
  return "[ERROR] '#{e.class}' in converting '#{value}' to '#{type}': #{e.message}"
end
convert_value(value, type, is_array = false) click to toggle source
# File pim.rb, line 10535
def convert_value value, type, is_array = false

  return value if value.nil? or type.nil?

  if is_array
    array_value = [*value].compact
    array_value.map! { |v| v = convert_value(v, type) }
    return array_value
  end

  value = case type.to_s
    when 'String'
      value.to_s
    when 'Integer'
      value.to_i
    when 'Float'
      value.to_f
    when 'Date'
      if value.is_a?(DateTime)
        value.to_date
      elsif value.is_a?(Date)
        value
      elsif value.is_a?(Numeric)
        Time.at(value / 1000).to_date
      else
        begin
          Date.parse(value.to_s)
        rescue Exception => e
          conversion_error value, type, e
        end
      end
    when 'DateTime'
      if value.is_a?(DateTime)
        value
      elsif value.is_a?(Date)
        value.to_datetime
      elsif value.is_a?(Numeric)
        Time.at(value / 1000).to_datetime
      else
        begin
          DateTime.parse(value.to_s)
        rescue Exception => e
          conversion_error value, type, e
        end
      end
    when 'Boolean'
      if value == true
        true
      elsif value == false
        false
      else
        %w(1 true T yes Y on).include?(value.to_s.downcase)
      end
    else
      PIM.log_error("Unknown type '#{type}' to convert value '#{value}' to")
      value
  end

  return value

end