Class Amalgalite::TypeMaps::DefaultMap
In: lib/amalgalite/type_maps/default_map.rb
Parent: Object

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

Methods

bind_type_of   blob   boolean   date   datetime   float   integer   new   result_value_of   string   time  

Public Class methods

[Source]

    # File lib/amalgalite/type_maps/default_map.rb, line 59
59:     def initialize
60:     end

Public Instance methods

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

[Source]

    # File lib/amalgalite/type_maps/default_map.rb, line 66
66:     def bind_type_of( obj )
67:       case obj
68:       when Float
69:         ::Amalgalite::SQLite3::Constants::DataType::FLOAT
70:       when Fixnum
71:         ::Amalgalite::SQLite3::Constants::DataType::INTEGER
72:       when NilClass
73:         ::Amalgalite::SQLite3::Constants::DataType::NULL
74:       when ::Amalgalite::Blob
75:         ::Amalgalite::SQLite3::Constants::DataType::BLOB
76:       else
77:         ::Amalgalite::SQLite3::Constants::DataType::TEXT
78:       end
79:     end

convert a string to a blob

[Source]

     # File lib/amalgalite/type_maps/default_map.rb, line 163
163:     def blob( str )
164:       ::Amalgalite::Blob.new( :string => str )
165:     end

convert a string to true of false

[Source]

     # File lib/amalgalite/type_maps/default_map.rb, line 156
156:     def boolean( str )
157:       ::Amalgalite::Boolean.to_bool( str )
158:     end

convert a string to a date

[Source]

     # File lib/amalgalite/type_maps/default_map.rb, line 114
114:     def date( str )
115:       Date.parse( str )
116:     end

convert a string to a datetime

[Source]

     # File lib/amalgalite/type_maps/default_map.rb, line 121
121:     def datetime( str )
122:       DateTime.parse( str )
123:     end

convert a string to a Float

[Source]

     # File lib/amalgalite/type_maps/default_map.rb, line 135
135:     def float( str )
136:       Float( str )
137:     end

convert an string to an Integer

[Source]

     # File lib/amalgalite/type_maps/default_map.rb, line 142
142:     def integer( str )
143:       Float( str ).to_i
144:     end

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

[Source]

     # File lib/amalgalite/type_maps/default_map.rb, line 86
 86:     def result_value_of( declared_type, value )
 87:       case value
 88:       when Numeric
 89:         return value
 90:       when NilClass
 91:         return value
 92:       when Amalgalite::Blob
 93:         return value
 94:       when String
 95:         if declared_type then
 96:           conversion_method = DefaultMap.sql_to_method( declared_type.downcase )
 97:           if conversion_method then
 98:             return send(conversion_method, value)  
 99:           else
100:             raise ::Amalgalite::Error, "Unable to convert SQL type of #{declared_type} to a Ruby class"
101:           end
102:         else
103:           # unable to do any other conversion, just return what we have.
104:           return value
105:         end
106:       else 
107:         raise ::Amalgalite::Error, "Unable to convert a class #{value.class.name} with value #{value.inspect}"
108:       end
109:     end

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

[Source]

     # File lib/amalgalite/type_maps/default_map.rb, line 149
149:     def string( str )
150:       str
151:     end

convert a string to a Time

[Source]

     # File lib/amalgalite/type_maps/default_map.rb, line 128
128:     def time( str )
129:       Time.parse( str )
130:     end

[Validate]