4
4
require "mongoid/fields/foreign_key"
5
5
require "mongoid/fields/localized"
6
6
require "mongoid/fields/validators"
7
+ require "mongoid/fields/field_types"
7
8
8
9
module Mongoid
9
10
@@ -14,27 +15,6 @@ module Fields
14
15
StringifiedSymbol = Mongoid ::StringifiedSymbol
15
16
Boolean = Mongoid ::Boolean
16
17
17
- # For fields defined with symbols use the correct class.
18
- TYPE_MAPPINGS = {
19
- array : Array ,
20
- big_decimal : BigDecimal ,
21
- binary : BSON ::Binary ,
22
- boolean : Mongoid ::Boolean ,
23
- date : Date ,
24
- date_time : DateTime ,
25
- float : Float ,
26
- hash : Hash ,
27
- integer : Integer ,
28
- object_id : BSON ::ObjectId ,
29
- range : Range ,
30
- regexp : Regexp ,
31
- set : Set ,
32
- string : String ,
33
- stringified_symbol : StringifiedSymbol ,
34
- symbol : Symbol ,
35
- time : Time
36
- } . with_indifferent_access
37
-
38
18
# Constant for all names of the _id field in a document.
39
19
#
40
20
# This does not include aliases of _id field.
@@ -243,6 +223,27 @@ def using_object_ids?
243
223
244
224
class << self
245
225
226
+ # DSL method used for configuration readability, typically in
227
+ # an initializer.
228
+ #
229
+ # @example
230
+ # Mongoid::Fields.configure do
231
+ # # do configuration
232
+ # end
233
+ def configure ( &block )
234
+ instance_exec ( &block )
235
+ end
236
+
237
+ # Defines a field type mapping, for later use in field :type option.
238
+ #
239
+ # @example
240
+ # Mongoid::Fields.configure do
241
+ # type :point, Point
242
+ # end
243
+ def type ( symbol , klass )
244
+ Fields ::FieldTypes . define ( symbol , klass )
245
+ end
246
+
246
247
# Stores the provided block to be run when the option name specified is
247
248
# defined on a field.
248
249
#
@@ -251,8 +252,10 @@ class << self
251
252
# provided in the field definition -- even if it is false or nil.
252
253
#
253
254
# @example
254
- # Mongoid::Fields.option :required do |model, field, value|
255
- # model.validates_presence_of field if value
255
+ # Mongoid::Fields.configure do
256
+ # option :required do |model, field, value|
257
+ # model.validates_presence_of field.name if value
258
+ # end
256
259
# end
257
260
#
258
261
# @param [ Symbol ] option_name the option name to match against
@@ -729,22 +732,16 @@ def remove_defaults(name)
729
732
730
733
def field_for ( name , options )
731
734
opts = options . merge ( klass : self )
732
- type_mapping = TYPE_MAPPINGS [ options [ :type ] ]
733
- opts [ :type ] = type_mapping || unmapped_type ( options )
734
- unless opts [ :type ] . is_a? ( Class )
735
- raise Errors ::InvalidFieldType . new ( self , name , options [ :type ] )
736
- end
735
+ opts [ :type ] = field_type_klass_for ( name , options [ :type ] )
737
736
return Fields ::Localized . new ( name , opts ) if options [ :localize ]
738
737
return Fields ::ForeignKey . new ( name , opts ) if options [ :identity ]
739
738
Fields ::Standard . new ( name , opts )
740
739
end
741
740
742
- def unmapped_type ( options )
743
- if "Boolean" == options [ :type ] . to_s
744
- Mongoid ::Boolean
745
- else
746
- options [ :type ] || Object
747
- end
741
+ def field_type_klass_for ( field , type )
742
+ klass = Fields ::FieldTypes . get ( type )
743
+ return klass if klass
744
+ raise Mongoid ::Errors ::InvalidFieldType . new ( self . name , field , type )
748
745
end
749
746
end
750
747
end
0 commit comments