44require "mongoid/fields/foreign_key"
55require "mongoid/fields/localized"
66require "mongoid/fields/validators"
7+ require "mongoid/fields/field_types"
78
89module Mongoid
910
@@ -14,27 +15,6 @@ module Fields
1415 StringifiedSymbol = Mongoid ::StringifiedSymbol
1516 Boolean = Mongoid ::Boolean
1617
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-
3818 # Constant for all names of the _id field in a document.
3919 #
4020 # This does not include aliases of _id field.
@@ -243,6 +223,27 @@ def using_object_ids?
243223
244224 class << self
245225
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+
246247 # Stores the provided block to be run when the option name specified is
247248 # defined on a field.
248249 #
@@ -251,8 +252,10 @@ class << self
251252 # provided in the field definition -- even if it is false or nil.
252253 #
253254 # @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
256259 # end
257260 #
258261 # @param [ Symbol ] option_name the option name to match against
@@ -729,22 +732,16 @@ def remove_defaults(name)
729732
730733 def field_for ( name , options )
731734 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 ] )
737736 return Fields ::Localized . new ( name , opts ) if options [ :localize ]
738737 return Fields ::ForeignKey . new ( name , opts ) if options [ :identity ]
739738 Fields ::Standard . new ( name , opts )
740739 end
741740
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 )
748745 end
749746 end
750747 end
0 commit comments