Class: DataMapper::Type

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

Constants

PROPERTY_OPTION_ALIASES
{ :size => [ :length ] }
PROPERTY_OPTIONS
[ :public, :protected, :private, :accessor, :reader, :writer, :lazy, :default, :nullable, :key, :serial, :field, :size, :length, :format, :index, :unique_index, :check, :ordinal, :auto_validation, :validates, :unique, :lock, :track, :scale, :precision ]

Public Visibility

Public Class Method Summary

bind(property)
configure(primitive_type, options)
dump(value, property)

Stub instance method for dumping.

Returns:

inherited(base)
load(value, property)

Stub instance method for loading.

Returns:

options

Gives all the options set on this type.

Returns:

primitive(primitive = nil)

The Ruby primitive type to use as basis for this type.

Returns:

Public Instance Methods Inherited from Object

validatable?

Public Class Method Details

bind

public bind(property)
[View source]


150
151
152
153
154
# File 'dm-core/lib/dm-core/type.rb', line 150

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

public configure(primitive_type, options)
[View source]


50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'dm-core/lib/dm-core/type.rb', line 50

def configure(primitive_type, options)
  @_primitive_type = primitive_type
  @_options = options

  def self.inherited(base)
    base.primitive @_primitive_type

    @_options.each do |k, v|
      base.send(k, v)
    end
  end

  self
end

dump

public dump(value, property)

Stub instance method for dumping

Meta Tags

Parameters:

value<Object,

nil> the value to dump

property<Property,

nil> the property the type is being used by

Returns:

<Object> Dumped object

[View source]


134
135
136
# File 'dm-core/lib/dm-core/type.rb', line 134

def self.dump(value, property)
    value
end

inherited

public inherited(base)
[View source]


54
55
56
57
58
59
60
# File 'dm-core/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

public load(value, property)

Stub instance method for loading

Meta Tags

Parameters:

value<Object,

nil> the value to serialize

property<Property,

nil> the property the type is being used by

Returns:

<Object> Serialized object. Must be the same type as the Ruby primitive

[View source]


146
147
148
# File 'dm-core/lib/dm-core/type.rb', line 146

def self.load(value, property)
  value
end

options

public options

Gives all the options set on this type

Meta Tags

Returns:

<Hash> with all options and their values set on this type

[View source]


116
117
118
119
120
121
122
123
124
125
126
# File 'dm-core/lib/dm-core/type.rb', line 116

def options



  options = {}
  PROPERTY_OPTIONS.each do |method|
    next if (value = send(method)).nil?
    options[method] = value
  end
  options
end

primitive

public primitive(primitive = nil)

The Ruby primitive type to use as basis for this type. See DataMapper::Property::TYPES for list of types.

Meta Tags

Parameters:

primitive<Class,

nil> The class for the primitive. If nil is passed in, it returns the current primitive

Returns:

<Class> if the <primitive> param is nil, return the current primitive.

[View source]


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'dm-core/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