Class: DataMapper::Type
- Inherits:
-
Object
- Object
- DataMapper::Type
- Defined in:
- lib/dm-core/type.rb
Overview
Types
Provides means of writing custom types for properties. Each type is based on a ruby primitive and handles its own serialization and materialization, and therefore is responsible for providing those methods.
To see complete list of supported types, see documentation for DataMapper::Property::TYPES
Defining new Types
To define a new type, subclass DataMapper::Type, pick ruby primitive, and set the options for this type.
class MyType < DataMapper::Type
primitive String
size 10
end
Following this, you will be able to use MyType as a type for any given property. If special materialization and serialization is required, override the class methods
class MyType < DataMapper::Type
primitive String
size 10
def self.dump(value, property)
<work some magic>
end
def self.load(value)
<work some magic>
end
end
Direct Known Subclasses
DataMapper::Types::Boolean, DataMapper::Types::Discriminator, DataMapper::Types::Object, DataMapper::Types::Serial, DataMapper::Types::Text
Constant Summary collapse
- PROPERTY_OPTIONS =
[ :accessor, :reader, :writer, :lazy, :default, :nullable, :key, :serial, :field, :size, :length, :format, :index, :unique_index, :check, :ordinal, :auto_validation, :validates, :unique, :track, :precision, :scale ]
- PROPERTY_OPTION_ALIASES =
{ :size => [ :length ] }
Class Method Summary collapse
- .bind(property) ⇒ Object
- .configure(primitive_type, options) ⇒ Object
-
.dump(value, property) ⇒ Object
Stub instance method for dumping.
- .inherited(base) ⇒ Object
-
.load(value, property) ⇒ Object
Stub instance method for loading.
-
.options ⇒ Hash
Gives all the options set on this type.
-
.primitive(primitive = nil) ⇒ Class
The Ruby primitive type to use as basis for this type.
Class Method Details
.bind(property) ⇒ Object
148 149 150 151 152 |
# File 'lib/dm-core/type.rb', line 148 def self.bind(property) # This method should not modify the state of this type class, and # should produce no side-effects on the type class. It's just a # hook to allow your custom-type to modify the property it's bound to. end |
.configure(primitive_type, options) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/dm-core/type.rb', line 50 def configure(primitive_type, ) @_primitive_type = primitive_type @_options = def self.inherited(base) base.primitive @_primitive_type @_options.each do |k, v| base.send(k, v) end end self end |
.dump(value, property) ⇒ Object
Stub instance method for dumping
132 133 134 |
# File 'lib/dm-core/type.rb', line 132 def self.dump(value, property) value end |
.inherited(base) ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/dm-core/type.rb', line 54 def self.inherited(base) base.primitive @_primitive_type @_options.each do |k, v| base.send(k, v) end end |
.load(value, property) ⇒ Object
Stub instance method for loading
144 145 146 |
# File 'lib/dm-core/type.rb', line 144 def self.load(value, property) value end |
.options ⇒ Hash
Gives all the options set on this type
114 115 116 117 118 119 120 121 |
# File 'lib/dm-core/type.rb', line 114 def = {} PROPERTY_OPTIONS.each do |method| next if (value = send(method)).nil? [method] = value end end |
.primitive(primitive = nil) ⇒ Class
The Ruby primitive type to use as basis for this type. See DataMapper::Property::TYPES for list of types.
75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/dm-core/type.rb', line 75 def primitive(primitive = nil) return @primitive if primitive.nil? # TODO: change Integer to be used internally once most in-the-wild code # is updated to use Integer for properties instead of Fixnum, or before # DM 1.0, whichever comes first if Fixnum == primitive warn "#{primitive} properties are deprecated. Please use Integer instead" primitive = Integer end @primitive = primitive end |