Skip to content

Commit b68ccfd

Browse files
committed
Requested changes
1 parent 6fc3273 commit b68ccfd

File tree

6 files changed

+60
-32
lines changed

6 files changed

+60
-32
lines changed

docs/reference/fields.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -897,14 +897,14 @@ can use in our model class as follows:
897897
field :location, type: Point
898898
end
899899

900-
You may optionally declare a symbol mapping for the new field type in an initializer:
900+
You may optionally declare a mapping for the new field type in an initializer:
901901

902902
.. code-block:: ruby
903903

904904
# in /config/initializers/mongoid_custom_fields.rb
905905

906906
Mongoid::Fields.configure do
907-
type :point, Point
907+
define_type :point, Point
908908
end
909909

910910

docs/release-notes/mongoid-8.0.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ Mongoid 8.0 adds the ability to define custom ``field :type`` Symbol values as f
313313
# in /config/initializers/mongoid_custom_fields.rb
314314

315315
Mongoid::Fields.configure do
316-
type :point, Point
316+
define_type :point, Point
317317
end
318318

319319
Refer to the :ref:`docs <http://docs.mongodb.org/manual/reference/fields/#custom-field-types>` for details.

lib/config/locales/en.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ en:
176176
Refer to:
177177
https://docs.mongodb.com/mongoid/current/reference/fields/#custom-field-options"
178178
invalid_field_type:
179-
message: "Invalid field type %{type_inspection} for field :%{field} on model '%{klass}'."
180-
summary: "Model '%{klass}' defines a field :%{field} with an unknown :type value
179+
message: "Invalid field type %{type_inspection} for field '%{field}' on model '%{klass}'."
180+
summary: "Model '%{klass}' defines a field '%{field}' with an unknown :type value
181181
%{type_inspection}. This value is neither present in Mongoid's default type mapping,
182182
nor defined in a custom field type mapping."
183183
resolution: "Please provide a valid :type value for the field. If you

lib/mongoid/fields.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,11 @@ def configure(&block)
240240
# Mongoid::Fields.configure do
241241
# type :point, Point
242242
# end
243-
def type(symbol, klass)
243+
#
244+
# @param [ Symbol ] symbol the symbol identifier of the defined type
245+
# @param [ Class ] klass the class of the defined type, which must
246+
# include mongoize and related methods.
247+
def define_type(symbol, klass)
244248
Fields::FieldTypes.define(symbol, klass)
245249
end
246250

lib/mongoid/fields/field_types.rb

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
module Mongoid
44
module Fields
55

6-
# Singleton module which contains a cache for field type definitions.
6+
# Singleton module which contains a mapping of field types to field class.
77
# Custom field types can be configured.
88
module FieldTypes
9-
extend self
109

11-
# For fields defined with symbols use the correct class.
10+
# The default mapping of field type symbol to class.
11+
#
12+
# @api private
1213
DEFAULT_MAPPING = {
1314
array: Array,
1415
bigdecimal: BigDecimal,
@@ -35,32 +36,55 @@ module FieldTypes
3536
time_with_zone: ActiveSupport::TimeWithZone
3637
}.with_indifferent_access.freeze
3738

38-
def get(value)
39-
value = value.to_sym if value.is_a?(String)
40-
mapping[value] || handle_unmapped_type(value)
41-
end
39+
class << self
4240

43-
def define(symbol, klass)
44-
mapping[symbol.to_sym] = klass
45-
end
41+
# Gets a field class given its mapping identifier.
42+
#
43+
# @example
44+
# Mongoid::FieldTypes.get(:point)
45+
#
46+
# @param [ Symbol | String ] value the identifier of the defined type
47+
def get(value)
48+
mapping[value] || handle_unmapped_type(value)
49+
end
4650

47-
delegate :delete, to: :mapping
51+
# Defines a field type mapping, for later use in field :type option.
52+
#
53+
# @example
54+
# Mongoid::FieldTypes.define(:point, Point)
55+
#
56+
# @param [ Symbol ] symbol the symbol identifier of the defined type
57+
# @param [ Class ] klass the class of the defined type, which must
58+
# include mongoize and related methods.
59+
def define(symbol, klass)
60+
mapping[symbol.to_sym] = klass
61+
end
4862

49-
private
63+
delegate :delete, to: :mapping
5064

51-
def mapping
52-
@mapping ||= DEFAULT_MAPPING.dup
53-
end
65+
private
5466

55-
def handle_unmapped_type(type)
56-
return Object if type.nil?
57-
58-
if type.is_a?(Module)
59-
return Mongoid::Boolean if type.to_s == 'Boolean'
60-
return type
67+
# The memoized mapping of field type definitions to classes.
68+
#
69+
# @return [ ActiveSupport::HashWithIndifferentAccess<Symbol, Class> ] The memoized field mapping.
70+
def mapping
71+
@mapping ||= DEFAULT_MAPPING.dup
6172
end
6273

63-
nil
74+
# Handles fallback for case where mapping does not contain the
75+
# requested type.
76+
#
77+
# @return [ Class | nil ] The class to use as a fallback, or nil.
78+
def handle_unmapped_type(type)
79+
return Object if type.nil?
80+
81+
if type.is_a?(Module)
82+
return Mongoid::Boolean if type.to_s == 'Boolean'
83+
return type
84+
end
85+
86+
nil
87+
end
6488
end
6589
end
6690
end

spec/mongoid/errors/invalid_field_type_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717

1818
it "contains the problem in the message" do
1919
expect(error.message).to include(
20-
"Invalid field type :stringgy for field :first_name on model 'Person'."
20+
"Invalid field type :stringgy for field 'first_name' on model 'Person'."
2121
)
2222
end
2323

2424
it "contains the summary in the message" do
2525
expect(error.message).to include(
26-
"Model 'Person' defines a field :first_name with an unknown :type value :stringgy."
26+
"Model 'Person' defines a field 'first_name' with an unknown :type value :stringgy."
2727
)
2828
end
2929
end
@@ -35,13 +35,13 @@
3535

3636
it "contains the problem in the message" do
3737
expect(error.message).to include(
38-
%q,Invalid field type "stringgy" for field :first_name on model 'Person'.,
38+
%q,Invalid field type "stringgy" for field 'first_name' on model 'Person'.,
3939
)
4040
end
4141

4242
it "contains the summary in the message" do
4343
expect(error.message).to include(
44-
%q,Model 'Person' defines a field :first_name with an unknown :type value "stringgy".,
44+
%q,Model 'Person' defines a field 'first_name' with an unknown :type value "stringgy".,
4545
)
4646
end
4747
end

0 commit comments

Comments
 (0)