class Amalgalite::TypeMaps::DefaultMap

An Amalgalite::TypeMap that does its best to convert between Ruby classes and known SQL data types.

Upon instantiation, DefaultMap generates a conversion map to try to figure out the best way to convert between populate SQL 'types' and ruby classes

Public Class Methods

new() click to toggle source
# File lib/amalgalite/type_maps/default_map.rb, line 57
def initialize
end

Public Instance Methods

bind_type_of( obj ) click to toggle source

A straight logical mapping (for me at least) of basic Ruby classes to SQLite types, if nothing can be found then default to TEXT.

# File lib/amalgalite/type_maps/default_map.rb, line 64
def bind_type_of( obj )
  case obj
  when Float
    ::Amalgalite::SQLite3::Constants::DataType::FLOAT
  when Fixnum
    ::Amalgalite::SQLite3::Constants::DataType::INTEGER
  when NilClass
    ::Amalgalite::SQLite3::Constants::DataType::NULL
  when ::Amalgalite::Blob
    ::Amalgalite::SQLite3::Constants::DataType::BLOB
  else
    ::Amalgalite::SQLite3::Constants::DataType::TEXT
  end
end
blob( str ) click to toggle source

convert a string to a blob

# File lib/amalgalite/type_maps/default_map.rb, line 162
def blob( str )
  ::Amalgalite::Blob.new( :string => str )
end
boolean( str ) click to toggle source

convert a string to true of false

# File lib/amalgalite/type_maps/default_map.rb, line 155
def boolean( str )
  ::Amalgalite::Boolean.to_bool( str )
end
date( str ) click to toggle source

convert a string to a date

# File lib/amalgalite/type_maps/default_map.rb, line 112
def date( str )
  Date.parse( str )
end
datetime( str ) click to toggle source

convert a string to a datetime, if no timzone is found in the parsed string, set it to the local offset.

# File lib/amalgalite/type_maps/default_map.rb, line 120
def datetime( str )
  DateTime.parse( str )
end
float( str ) click to toggle source

convert a string to a Float

# File lib/amalgalite/type_maps/default_map.rb, line 134
def float( str )
  Float( str )
end
integer( str ) click to toggle source

convert an string to an Integer

# File lib/amalgalite/type_maps/default_map.rb, line 141
def integer( str )
  Float( str ).to_i
end
result_value_of( declared_type, value ) click to toggle source

Map the incoming value to an outgoing value. For some incoming values, there will be no change, but for some (i.e. Dates and Times) there is some conversion

# File lib/amalgalite/type_maps/default_map.rb, line 84
def result_value_of( declared_type, value )
  case value
  when Numeric
    return value
  when NilClass
    return value
  when Amalgalite::Blob
    return value
  when String
    if declared_type then
      conversion_method = DefaultMap.sql_to_method( declared_type.downcase )
      if conversion_method then
        return send(conversion_method, value)  
      else
        raise ::Amalgalite::Error, "Unable to convert SQL type of #{declared_type} to a Ruby class"
      end
    else
      # unable to do any other conversion, just return what we have.
      return value
    end
  else 
    raise ::Amalgalite::Error, "Unable to convert a class #{value.class.name} with value #{value.inspect}"
  end
end
string( str ) click to toggle source

convert a string to a String, yes redundant I know.

# File lib/amalgalite/type_maps/default_map.rb, line 148
def string( str )
  str
end
time( str ) click to toggle source

convert a string to a Time

# File lib/amalgalite/type_maps/default_map.rb, line 127
def time( str )
  Time.parse( str )
end