Skip to content

Commit 12e3e86

Browse files
authored
MONGOID-5264 clean up and comment the fields file (#5407)
* MONGOID-5264 clean up and comment the fields filee * MONGOID-5264 fix tests
1 parent 1aca8d4 commit 12e3e86

File tree

1 file changed

+69
-13
lines changed

1 file changed

+69
-13
lines changed

lib/mongoid/fields.rb

Lines changed: 69 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,8 @@ def traverse_association_tree(key, &block)
538538
# Model.add_defaults(field)
539539
#
540540
# @param [ Field ] field The field to add for.
541+
#
542+
# @api private
541543
def add_defaults(field)
542544
default, name = field.default_val, field.name.to_s
543545
remove_defaults(name)
@@ -557,6 +559,8 @@ def add_defaults(field)
557559
#
558560
# @param [ Symbol ] name The name of the field.
559561
# @param [ Hash ] options The hash of options.
562+
#
563+
# @api private
560564
def add_field(name, options = {})
561565
aliased = options[:as]
562566
aliased_fields[aliased.to_s] = name if aliased
@@ -584,6 +588,8 @@ def add_field(name, options = {})
584588
# # => "called"
585589
#
586590
# @param [ Field ] field the field to process
591+
#
592+
# @api private
587593
def process_options(field)
588594
field_options = field.options
589595

@@ -606,6 +612,8 @@ def process_options(field)
606612
# @param [ Symbol ] name The name of the field.
607613
# @param [ Symbol ] meth The name of the accessor.
608614
# @param [ Hash ] options The options.
615+
#
616+
# @api private
609617
def create_accessors(name, meth, options = {})
610618
field = fields[name]
611619

@@ -629,6 +637,8 @@ def create_accessors(name, meth, options = {})
629637
# @param [ String ] name The name of the attribute.
630638
# @param [ String ] meth The name of the method.
631639
# @param [ Field ] field The field.
640+
#
641+
# @api private
632642
def create_field_getter(name, meth, field)
633643
generated_methods.module_eval do
634644
re_define_method(meth) do
@@ -651,6 +661,8 @@ def create_field_getter(name, meth, field)
651661
#
652662
# @param [ String ] name The name of the attribute.
653663
# @param [ String ] meth The name of the method.
664+
#
665+
# @api private
654666
def create_field_getter_before_type_cast(name, meth)
655667
generated_methods.module_eval do
656668
re_define_method("#{meth}_before_type_cast") do
@@ -671,6 +683,8 @@ def create_field_getter_before_type_cast(name, meth)
671683
# @param [ String ] name The name of the attribute.
672684
# @param [ String ] meth The name of the method.
673685
# @param [ Field ] field The field.
686+
#
687+
# @api private
674688
def create_field_setter(name, meth, field)
675689
generated_methods.module_eval do
676690
re_define_method("#{meth}=") do |value|
@@ -690,6 +704,8 @@ def create_field_setter(name, meth, field)
690704
#
691705
# @param [ String ] name The name of the attribute.
692706
# @param [ String ] meth The name of the method.
707+
#
708+
# @api private
693709
def create_field_check(name, meth)
694710
generated_methods.module_eval do
695711
re_define_method("#{meth}?") do
@@ -706,6 +722,8 @@ def create_field_check(name, meth)
706722
#
707723
# @param [ String ] name The name of the attribute.
708724
# @param [ String ] meth The name of the method.
725+
#
726+
# @api private
709727
def create_translations_getter(name, meth)
710728
generated_methods.module_eval do
711729
re_define_method("#{meth}_translations") do
@@ -724,6 +742,8 @@ def create_translations_getter(name, meth)
724742
# @param [ String ] name The name of the attribute.
725743
# @param [ String ] meth The name of the method.
726744
# @param [ Field ] field The field.
745+
#
746+
# @api private
727747
def create_translations_setter(name, meth, field)
728748
generated_methods.module_eval do
729749
re_define_method("#{meth}_translations=") do |value|
@@ -743,6 +763,8 @@ def create_translations_setter(name, meth, field)
743763
# Person.generated_methods
744764
#
745765
# @return [ Module ] The module of generated methods.
766+
#
767+
# @api private
746768
def generated_methods
747769
@generated_methods ||= begin
748770
mod = Module.new
@@ -757,38 +779,72 @@ def generated_methods
757779
# Model.remove_defaults(name)
758780
#
759781
# @param [ String ] name The field name.
782+
#
783+
# @api private
760784
def remove_defaults(name)
761785
pre_processed_defaults.delete_one(name)
762786
post_processed_defaults.delete_one(name)
763787
end
764788

789+
# Create a field for the given name and options.
790+
#
791+
# @param [ Symbol ] name The name of the field.
792+
# @param [ Hash ] options The hash of options.
793+
#
794+
# @return [ Field ] The created field.
795+
#
796+
# @api private
765797
def field_for(name, options)
766798
opts = options.merge(klass: self)
767-
type_mapping = TYPE_MAPPINGS[options[:type]]
768-
opts[:type] = type_mapping || unmapped_type(options)
769-
if !opts[:type].is_a?(Class)
770-
raise Errors::InvalidFieldType.new(self, name, options[:type])
799+
opts[:type] = retrieve_and_validate_type(name, options[:type])
800+
return Fields::Localized.new(name, opts) if options[:localize]
801+
return Fields::ForeignKey.new(name, opts) if options[:identity]
802+
Fields::Standard.new(name, opts)
803+
end
804+
805+
# Get the class for the given type.
806+
#
807+
# @param [ Symbol ] name The name of the field.
808+
# @param [ Symbol | Class ] type The type of the field.
809+
#
810+
# @return [ Class ] The type of the field.
811+
#
812+
# @raises [ Mongoid::Errors::InvalidFieldType ] if given an invalid field
813+
# type.
814+
#
815+
# @api private
816+
def retrieve_and_validate_type(name, type)
817+
type_mapping = TYPE_MAPPINGS[type]
818+
result = type_mapping || unmapped_type(type)
819+
if !result.is_a?(Class)
820+
raise Errors::InvalidFieldType.new(self, name, type)
771821
else
772-
if INVALID_BSON_CLASSES.include?(opts[:type])
773-
warn_message = "Using #{opts[:type]} as the field type is not supported. "
774-
if opts[:type] == BSON::Decimal128
822+
if INVALID_BSON_CLASSES.include?(result)
823+
warn_message = "Using #{result} as the field type is not supported. "
824+
if result == BSON::Decimal128
775825
warn_message += "In BSON <= 4, the BSON::Decimal128 type will work as expected for both storing and querying, but will return a BigDecimal on query in BSON 5+."
776826
else
777827
warn_message += "Saving values of this type to the database will work as expected, however, querying them will return a value of the native Ruby Integer type."
778828
end
779829
Mongoid.logger.warn(warn_message)
780830
end
781831
end
782-
return Fields::Localized.new(name, opts) if options[:localize]
783-
return Fields::ForeignKey.new(name, opts) if options[:identity]
784-
Fields::Standard.new(name, opts)
832+
result
785833
end
786834

787-
def unmapped_type(options)
788-
if "Boolean" == options[:type].to_s
835+
# Returns the type of the field if the type was not in the TYPE_MAPPINGS
836+
# hash.
837+
#
838+
# @param [ Symbol | Class ] type The type of the field.
839+
#
840+
# @return [ Class ] The type of the field.
841+
#
842+
# @api private
843+
def unmapped_type(type)
844+
if "Boolean" == type.to_s
789845
Mongoid::Boolean
790846
else
791-
options[:type] || Object
847+
type || Object
792848
end
793849
end
794850
end

0 commit comments

Comments
 (0)