Class: DataMapper::Type

Inherits:
Object show all
Defined in:
lib/dm-core/type.rb

Constant Summary collapse

PROPERTY_OPTIONS =

Until cooperation of Property and Type does not change, each must have a separate list of options, because plugins (ex.: dm-validations) may want to extend one or the other, and expects no side effects

[
  :accessor, :reader, :writer,
  :lazy, :default, :key, :serial, :field, :size, :length,
  :format, :index, :unique_index, :auto_validation,
  :validates, :unique, :precision, :scale, :min, :max,
  :allow_nil, :allow_blank, :required
]

Class Method Summary collapse

Class Method Details

.bind(property) ⇒ Object

A hook to allow types to extend or modify property it’s bound to. Implementations are not supposed to modify the state of the type class, and should produce no side-effects on the type class.



204
205
206
# File 'lib/dm-core/type.rb', line 204

def self.bind(property)
  # no op
end

.configure(primitive_type, options) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/dm-core/type.rb', line 104

def configure(primitive_type, options)
  warn "DataMapper.Type.configure is deprecated, specify the primitive and options explicitly (#{caller[0]})"

  @_primitive_type = primitive_type
  @_options = options

  def self.inherited(base)
    base.primitive @_primitive_type
    @_options.each { |key, value| base.send(key, value) }
  end

  self
end

.dump(value, property) ⇒ Object

Stub instance method for dumping

Parameters:

  • value (Object, nil)

    value to dump

  • property (Property, nil)

    property the type is being used by

Returns:



185
186
187
# File 'lib/dm-core/type.rb', line 185

def self.dump(value, property)
  value
end

.inherited(base) ⇒ Object



110
111
112
113
# File 'lib/dm-core/type.rb', line 110

def self.inherited(base)
  base.primitive @_primitive_type
  @_options.each { |key, value| base.send(key, value) }
end

.load(value, property) ⇒ Object

Stub instance method for loading

Parameters:

  • value (Object, nil)

    value to serialize

  • property (Property, nil)

    property the type is being used by

Returns:

  • (Object)

    Serialized object. Must be the same type as the Ruby primitive



197
198
199
# File 'lib/dm-core/type.rb', line 197

def self.load(value, property)
  value
end

.nullable(value) ⇒ Object



156
157
158
159
160
# File 'lib/dm-core/type.rb', line 156

def nullable(value)
  # :required is preferable to :allow_nil, but :nullable maps precisely to :allow_nil
  warn "#nullable is deprecated, use #required instead (#{caller[0]})"
  allow_nil(value)
end

.optionsHash

Gives all the options set on this type

Returns:

  • (Hash)

    with all options and their values set on this type



167
168
169
170
171
172
173
174
# File 'lib/dm-core/type.rb', line 167

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

.primitive(primitive = nil) ⇒ Class

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

Parameters:

  • primitive (Class, nil) (defaults to: 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.



128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/dm-core/type.rb', line 128

def primitive(primitive = nil)
  return @primitive if primitive.nil?
  @primitive = primitive

  return unless @primitive.respond_to?(:options)
  options = @primitive.options

  return unless options.respond_to?(:each)

  # inherit the options from the primitive if any
  options.each do |key, value|
    send(key, value) unless send(key)
  end
end