Module: DataMapper::Adapters::Sql::Coersion
- Defined in:
- lib/data_mapper/adapters/sql/coersion.rb
Overview
Coersion is a mixin that allows for coercing database values to Ruby Types.
DESIGN: Probably should handle the opposite scenario here too. I believe that’s currently in DataMapper::Database, which is obviously not a very good spot for it.
Constant Summary collapse
- TRUE_ALIASES =
['true'.freeze, 'TRUE'.freeze]
- FALSE_ALIASES =
[nil]
Class Method Summary collapse
Instance Method Summary collapse
- #type_cast_boolean(raw_value) ⇒ Object
- #type_cast_class(raw_value) ⇒ Object
- #type_cast_datetime(raw_value) ⇒ Object
- #type_cast_integer(raw_value) ⇒ Object
- #type_cast_string(raw_value) ⇒ Object
- #type_cast_text(raw_value) ⇒ Object
- #type_cast_value(type, raw_value) ⇒ Object
Class Method Details
.included(base) ⇒ Object
14 15 16 17 |
# File 'lib/data_mapper/adapters/sql/coersion.rb', line 14 def self.included(base) base.const_set('TRUE_ALIASES', TRUE_ALIASES.dup) base.const_set('FALSE_ALIASES', FALSE_ALIASES.dup) end |
Instance Method Details
#type_cast_boolean(raw_value) ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/data_mapper/adapters/sql/coersion.rb', line 19 def type_cast_boolean(raw_value) case raw_value when TrueClass, FalseClass then raw_value when *self::class::TRUE_ALIASES then true when *self::class::FALSE_ALIASES then false else "Can't type-cast #{value.inspect} to a boolean" end end |
#type_cast_class(raw_value) ⇒ Object
38 39 40 41 |
# File 'lib/data_mapper/adapters/sql/coersion.rb', line 38 def type_cast_class(raw_value) return nil if raw_value.blank? Kernel::const_get(raw_value) end |
#type_cast_datetime(raw_value) ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/data_mapper/adapters/sql/coersion.rb', line 50 def type_cast_datetime(raw_value) return nil if raw_value.blank? case raw_value when DateTime then raw_value when Date then DateTime.new(raw_value) when String then DateTime::parse(raw_value) else "Can't type-cast #{raw_value.inspect} to a datetime" end end |
#type_cast_integer(raw_value) ⇒ Object
43 44 45 46 47 48 |
# File 'lib/data_mapper/adapters/sql/coersion.rb', line 43 def type_cast_integer(raw_value) return nil if raw_value.blank? raw_value.to_i # Integer(raw_value) would be "safer", but not as fast. rescue ArgumentError nil end |
#type_cast_string(raw_value) ⇒ Object
28 29 30 31 |
# File 'lib/data_mapper/adapters/sql/coersion.rb', line 28 def type_cast_string(raw_value) return nil if raw_value.blank? raw_value end |
#type_cast_text(raw_value) ⇒ Object
33 34 35 36 |
# File 'lib/data_mapper/adapters/sql/coersion.rb', line 33 def type_cast_text(raw_value) return nil if raw_value.blank? raw_value end |
#type_cast_value(type, raw_value) ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/data_mapper/adapters/sql/coersion.rb', line 61 def type_cast_value(type, raw_value) return nil if raw_value.blank? if respond_to?("type_cast_#{type}") send("type_cast_#{type}", raw_value) else raise "Don't know how to type-cast #{{ type => raw_value }.inspect }" end end |