lib/mongoid/fields.rb in mongoid-8.0.8 vs lib/mongoid/fields.rb in mongoid-8.1.0
- old
+ new
@@ -536,10 +536,12 @@
#
# @example Add to the defaults.
# Model.add_defaults(field)
#
# @param [ Field ] field The field to add for.
+ #
+ # @api private
def add_defaults(field)
default, name = field.default_val, field.name.to_s
remove_defaults(name)
unless default.nil?
if field.pre_processed?
@@ -555,10 +557,12 @@
# @example Set the field.
# Person.add_field(:name, :default => "Test")
#
# @param [ Symbol ] name The name of the field.
# @param [ Hash ] options The hash of options.
+ #
+ # @api private
def add_field(name, options = {})
aliased = options[:as]
aliased_fields[aliased.to_s] = name if aliased
field = field_for(name, options)
fields[name] = field
@@ -582,10 +586,12 @@
# field = Mongoid::Fields.new(:test, :custom => true)
# Person.process_options(field)
# # => "called"
#
# @param [ Field ] field the field to process
+ #
+ # @api private
def process_options(field)
field_options = field.options
Fields.options.each_pair do |option_name, handler|
if field_options.key?(option_name)
@@ -604,10 +610,12 @@
# person.name_before_type_cast #=> returns the field before type cast
#
# @param [ Symbol ] name The name of the field.
# @param [ Symbol ] meth The name of the accessor.
# @param [ Hash ] options The options.
+ #
+ # @api private
def create_accessors(name, meth, options = {})
field = fields[name]
create_field_getter(name, meth, field)
create_field_getter_before_type_cast(name, meth)
@@ -627,10 +635,12 @@
# Model.create_field_getter("name", "name", field)
#
# @param [ String ] name The name of the attribute.
# @param [ String ] meth The name of the method.
# @param [ Field ] field The field.
+ #
+ # @api private
def create_field_getter(name, meth, field)
generated_methods.module_eval do
re_define_method(meth) do
raw = read_raw_attribute(name)
if lazy_settable?(field, raw)
@@ -649,10 +659,12 @@
# @example Create the getter_before_type_cast.
# Model.create_field_getter_before_type_cast("name", "name")
#
# @param [ String ] name The name of the attribute.
# @param [ String ] meth The name of the method.
+ #
+ # @api private
def create_field_getter_before_type_cast(name, meth)
generated_methods.module_eval do
re_define_method("#{meth}_before_type_cast") do
if has_attribute_before_type_cast?(name)
read_attribute_before_type_cast(name)
@@ -669,10 +681,12 @@
# Model.create_field_setter("name", "name")
#
# @param [ String ] name The name of the attribute.
# @param [ String ] meth The name of the method.
# @param [ Field ] field The field.
+ #
+ # @api private
def create_field_setter(name, meth, field)
generated_methods.module_eval do
re_define_method("#{meth}=") do |value|
val = write_attribute(name, value)
if field.foreign_key?
@@ -688,10 +702,12 @@
# @example Create the check.
# Model.create_field_check("name", "name")
#
# @param [ String ] name The name of the attribute.
# @param [ String ] meth The name of the method.
+ #
+ # @api private
def create_field_check(name, meth)
generated_methods.module_eval do
re_define_method("#{meth}?") do
value = read_raw_attribute(name)
lookup_attribute_presence(name, value)
@@ -704,10 +720,12 @@
# @example Create the translation getter.
# Model.create_translations_getter("name", "name")
#
# @param [ String ] name The name of the attribute.
# @param [ String ] meth The name of the method.
+ #
+ # @api private
def create_translations_getter(name, meth)
generated_methods.module_eval do
re_define_method("#{meth}_translations") do
attributes[name] ||= {}
attributes[name].with_indifferent_access
@@ -722,10 +740,12 @@
# Model.create_translations_setter("name", "name")
#
# @param [ String ] name The name of the attribute.
# @param [ String ] meth The name of the method.
# @param [ Field ] field The field.
+ #
+ # @api private
def create_translations_setter(name, meth, field)
generated_methods.module_eval do
re_define_method("#{meth}_translations=") do |value|
attribute_will_change!(name)
value&.transform_values! do |_value|
@@ -741,10 +761,12 @@
#
# @example Include the fields.
# Person.generated_methods
#
# @return [ Module ] The module of generated methods.
+ #
+ # @api private
def generated_methods
@generated_methods ||= begin
mod = Module.new
include(mod)
mod
@@ -755,15 +777,25 @@
#
# @example Remove the default keys.
# Model.remove_defaults(name)
#
# @param [ String ] name The field name.
+ #
+ # @api private
def remove_defaults(name)
pre_processed_defaults.delete_one(name)
post_processed_defaults.delete_one(name)
end
+ # Create a field for the given name and options.
+ #
+ # @param [ Symbol ] name The name of the field.
+ # @param [ Hash ] options The hash of options.
+ #
+ # @return [ Field ] The created field.
+ #
+ # @api private
def field_for(name, options)
opts = options.merge(klass: self)
opts[:type] = retrieve_and_validate_type(name, options[:type])
return Fields::Localized.new(name, opts) if options[:localize]
return Fields::ForeignKey.new(name, opts) if options[:identity]
@@ -780,44 +812,41 @@
# @raises [ Mongoid::Errors::InvalidFieldType ] if given an invalid field
# type.
#
# @api private
def retrieve_and_validate_type(name, type)
- result = TYPE_MAPPINGS[type] || unmapped_type(type)
- raise Errors::InvalidFieldType.new(self, name, type) if !result.is_a?(Class)
-
- if unsupported_type?(result)
- warn_message = "Using #{result} as the field type is not supported. "
- if result == BSON::Decimal128
- 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+. To use literal BSON::Decimal128 fields with BSON 5, set Mongoid.allow_bson5_decimal128 to true.'
- else
- 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.'
+ type_mapping = TYPE_MAPPINGS[type]
+ result = type_mapping || unmapped_type(type)
+ if !result.is_a?(Class)
+ raise Errors::InvalidFieldType.new(self, name, type)
+ else
+ if INVALID_BSON_CLASSES.include?(result)
+ warn_message = "Using #{result} as the field type is not supported. "
+ if result == BSON::Decimal128
+ 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+."
+ else
+ 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."
+ end
+ Mongoid.logger.warn(warn_message)
end
- Mongoid.logger.warn(warn_message)
end
-
result
end
+ # Returns the type of the field if the type was not in the TYPE_MAPPINGS
+ # hash.
+ #
+ # @param [ Symbol | Class ] type The type of the field.
+ #
+ # @return [ Class ] The type of the field.
+ #
+ # @api private
def unmapped_type(type)
if "Boolean" == type.to_s
Mongoid::Boolean
else
type || Object
end
- end
-
- # Queries whether or not the given type is permitted as a declared field
- # type.
- #
- # @param [ Class ] type The type to query
- #
- # @return [ true | false ] whether or not the type is supported
- #
- # @api private
- def unsupported_type?(type)
- return !Mongoid::Config.allow_bson5_decimal128? if type == BSON::Decimal128
- INVALID_BSON_CLASSES.include?(type)
end
end
end
end