@@ -538,6 +538,8 @@ def traverse_association_tree(key, &block)
538
538
# Model.add_defaults(field)
539
539
#
540
540
# @param [ Field ] field The field to add for.
541
+ #
542
+ # @api private
541
543
def add_defaults ( field )
542
544
default , name = field . default_val , field . name . to_s
543
545
remove_defaults ( name )
@@ -557,6 +559,8 @@ def add_defaults(field)
557
559
#
558
560
# @param [ Symbol ] name The name of the field.
559
561
# @param [ Hash ] options The hash of options.
562
+ #
563
+ # @api private
560
564
def add_field ( name , options = { } )
561
565
aliased = options [ :as ]
562
566
aliased_fields [ aliased . to_s ] = name if aliased
@@ -584,6 +588,8 @@ def add_field(name, options = {})
584
588
# # => "called"
585
589
#
586
590
# @param [ Field ] field the field to process
591
+ #
592
+ # @api private
587
593
def process_options ( field )
588
594
field_options = field . options
589
595
@@ -606,6 +612,8 @@ def process_options(field)
606
612
# @param [ Symbol ] name The name of the field.
607
613
# @param [ Symbol ] meth The name of the accessor.
608
614
# @param [ Hash ] options The options.
615
+ #
616
+ # @api private
609
617
def create_accessors ( name , meth , options = { } )
610
618
field = fields [ name ]
611
619
@@ -629,6 +637,8 @@ def create_accessors(name, meth, options = {})
629
637
# @param [ String ] name The name of the attribute.
630
638
# @param [ String ] meth The name of the method.
631
639
# @param [ Field ] field The field.
640
+ #
641
+ # @api private
632
642
def create_field_getter ( name , meth , field )
633
643
generated_methods . module_eval do
634
644
re_define_method ( meth ) do
@@ -651,6 +661,8 @@ def create_field_getter(name, meth, field)
651
661
#
652
662
# @param [ String ] name The name of the attribute.
653
663
# @param [ String ] meth The name of the method.
664
+ #
665
+ # @api private
654
666
def create_field_getter_before_type_cast ( name , meth )
655
667
generated_methods . module_eval do
656
668
re_define_method ( "#{ meth } _before_type_cast" ) do
@@ -671,6 +683,8 @@ def create_field_getter_before_type_cast(name, meth)
671
683
# @param [ String ] name The name of the attribute.
672
684
# @param [ String ] meth The name of the method.
673
685
# @param [ Field ] field The field.
686
+ #
687
+ # @api private
674
688
def create_field_setter ( name , meth , field )
675
689
generated_methods . module_eval do
676
690
re_define_method ( "#{ meth } =" ) do |value |
@@ -690,6 +704,8 @@ def create_field_setter(name, meth, field)
690
704
#
691
705
# @param [ String ] name The name of the attribute.
692
706
# @param [ String ] meth The name of the method.
707
+ #
708
+ # @api private
693
709
def create_field_check ( name , meth )
694
710
generated_methods . module_eval do
695
711
re_define_method ( "#{ meth } ?" ) do
@@ -706,6 +722,8 @@ def create_field_check(name, meth)
706
722
#
707
723
# @param [ String ] name The name of the attribute.
708
724
# @param [ String ] meth The name of the method.
725
+ #
726
+ # @api private
709
727
def create_translations_getter ( name , meth )
710
728
generated_methods . module_eval do
711
729
re_define_method ( "#{ meth } _translations" ) do
@@ -724,6 +742,8 @@ def create_translations_getter(name, meth)
724
742
# @param [ String ] name The name of the attribute.
725
743
# @param [ String ] meth The name of the method.
726
744
# @param [ Field ] field The field.
745
+ #
746
+ # @api private
727
747
def create_translations_setter ( name , meth , field )
728
748
generated_methods . module_eval do
729
749
re_define_method ( "#{ meth } _translations=" ) do |value |
@@ -743,6 +763,8 @@ def create_translations_setter(name, meth, field)
743
763
# Person.generated_methods
744
764
#
745
765
# @return [ Module ] The module of generated methods.
766
+ #
767
+ # @api private
746
768
def generated_methods
747
769
@generated_methods ||= begin
748
770
mod = Module . new
@@ -757,38 +779,72 @@ def generated_methods
757
779
# Model.remove_defaults(name)
758
780
#
759
781
# @param [ String ] name The field name.
782
+ #
783
+ # @api private
760
784
def remove_defaults ( name )
761
785
pre_processed_defaults . delete_one ( name )
762
786
post_processed_defaults . delete_one ( name )
763
787
end
764
788
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
765
797
def field_for ( name , options )
766
798
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 )
771
821
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
775
825
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+."
776
826
else
777
827
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."
778
828
end
779
829
Mongoid . logger . warn ( warn_message )
780
830
end
781
831
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
785
833
end
786
834
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
789
845
Mongoid ::Boolean
790
846
else
791
- options [ : type] || Object
847
+ type || Object
792
848
end
793
849
end
794
850
end
0 commit comments