Class: Amalgalite::TypeMaps::DefaultMap
- Inherits:
-
Object
- Object
- Amalgalite::TypeMaps::DefaultMap
- Defined in:
- lib/amalgalite/type_maps/default_map.rb
Overview
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
Class Method Summary collapse
-
.methods_handling_sql_types ⇒ Object
:nodoc:.
-
.sql_to_method(sql_type) ⇒ Object
say what method to call to convert an sql type to a ruby type.
Instance Method Summary collapse
-
#bind_type_of(obj) ⇒ Object
A straight logical mapping (for me at least) of basic Ruby classes to SQLite types, if nothing can be found then default to TEXT.
-
#blob(str) ⇒ Object
convert a string to a blob.
-
#boolean(str) ⇒ Object
convert a string to true of false.
-
#date(str) ⇒ Object
convert a string to a date.
-
#datetime(str) ⇒ Object
convert a string to a datetime, if no timzone is found in the parsed string, set it to the local offset.
-
#float(str) ⇒ Object
convert a string to a Float.
-
#initialize ⇒ DefaultMap
constructor
A new instance of DefaultMap.
-
#integer(str) ⇒ Object
convert an string to an Integer.
-
#result_value_of(declared_type, value) ⇒ Object
Map the incoming value to an outgoing value.
-
#string(str) ⇒ Object
convert a string to a String, yes redundant I know.
-
#time(str) ⇒ Object
convert a string to a Time.
Constructor Details
#initialize ⇒ DefaultMap
Returns a new instance of DefaultMap.
57 58 |
# File 'lib/amalgalite/type_maps/default_map.rb', line 57 def initialize end |
Class Method Details
.methods_handling_sql_types ⇒ Object
:nodoc:
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/amalgalite/type_maps/default_map.rb', line 19 def methods_handling_sql_types # :nodoc: @methods_handling_sql_types ||= { 'date' => %w[ date ], 'datetime' => %w[ datetime ], 'time' => %w[ timestamp time ], 'float' => %w[ double float real numeric decimal ], 'integer' => %w[ integer tinyint smallint int int2 int4 int8 bigint serial bigserial ], 'string' => %w[ text char string varchar character json ], 'boolean' => %w[ bool boolean ], 'blob' => %w[ binary blob ], } end |
.sql_to_method(sql_type) ⇒ Object
say what method to call to convert an sql type to a ruby type
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/amalgalite/type_maps/default_map.rb', line 34 def sql_to_method( sql_type ) # :nodoc: unless defined? @sql_to_method @sql_to_method = {} methods_handling_sql_types.each_pair do |method, sql_types| sql_types.each { |t| @sql_to_method[t] = method } end end return_method = @sql_to_method[sql_type] # the straight lookup didn't work, try iterating through the types and # see what is found unless return_method @sql_to_method.each_pair do |sql, method| if sql_type.index(sql) then return_method = method break end end end return return_method end |
Instance Method Details
#bind_type_of(obj) ⇒ Object
A straight logical mapping (for me at least) of basic Ruby classes to SQLite types, if nothing can be found then default to TEXT.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# 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 Integer ::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) ⇒ Object
convert a string to a blob
162 163 164 |
# File 'lib/amalgalite/type_maps/default_map.rb', line 162 def blob( str ) ::Amalgalite::Blob.new( :string => str ) end |
#boolean(str) ⇒ Object
convert a string to true of false
155 156 157 |
# File 'lib/amalgalite/type_maps/default_map.rb', line 155 def boolean( str ) ::Amalgalite::Boolean.to_bool( str ) end |
#date(str) ⇒ Object
convert a string to a date
112 113 114 |
# File 'lib/amalgalite/type_maps/default_map.rb', line 112 def date( str ) Date.parse( str ) end |
#datetime(str) ⇒ Object
convert a string to a datetime, if no timzone is found in the parsed string, set it to the local offset.
120 121 122 |
# File 'lib/amalgalite/type_maps/default_map.rb', line 120 def datetime( str ) DateTime.parse( str ) end |
#float(str) ⇒ Object
convert a string to a Float
134 135 136 |
# File 'lib/amalgalite/type_maps/default_map.rb', line 134 def float( str ) Float( str ) end |
#integer(str) ⇒ Object
convert an string to an Integer
141 142 143 |
# File 'lib/amalgalite/type_maps/default_map.rb', line 141 def integer( str ) Float( str ).to_i end |
#result_value_of(declared_type, value) ⇒ Object
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
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# 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) ⇒ Object
convert a string to a String, yes redundant I know.
148 149 150 |
# File 'lib/amalgalite/type_maps/default_map.rb', line 148 def string( str ) str end |
#time(str) ⇒ Object
convert a string to a Time
127 128 129 |
# File 'lib/amalgalite/type_maps/default_map.rb', line 127 def time( str ) Time.parse( str ) end |