module Mongoid::Fields
This module defines behavior for fields.
Constants
- Boolean
- IDS
Constant for all names of the _id field in a document.
This does not include aliases of _id field.
@api private
- StringifiedSymbol
- TYPE_MAPPINGS
For fields defined with symbols use the correct class.
@since 4.0.0
Public Class Methods
Stores the provided block to be run when the option name specified is defined on a field.
No assumptions are made about what sort of work the handler might perform, so it will always be called if the ‘option_name` key is provided in the field definition – even if it is false or nil.
@example
Mongoid::Fields.option :required do |model, field, value| model.validates_presence_of field if value end
@param [ Symbol ] option_name the option name to match against @param [ Proc ] block the handler to execute when the option is
provided.
@since 2.1.0
# File lib/mongoid/fields.rb, line 242 def option(option_name, &block) options[option_name] = block end
Return a map of custom option names to their handlers.
@example
Mongoid::Fields.options # => { :required => #<Proc:0x00000100976b38> }
@return [ Hash ] the option map
@since 2.1.0
# File lib/mongoid/fields.rb, line 255 def options @options ||= {} end
Public Instance Methods
Applies a single default value for the given name.
@example Apply a single default.
model.apply_default("name")
@param [ String ] name The name of the field.
@since 2.4.0
# File lib/mongoid/fields.rb, line 143 def apply_default(name) unless attributes.key?(name) if field = fields[name] default = field.eval_default(self) unless default.nil? || field.lazy? attribute_will_change!(name) attributes[name] = default end end end end
Apply all the defaults at once.
@example Apply all the defaults.
model.apply_defaults
@since 2.4.0
# File lib/mongoid/fields.rb, line 161 def apply_defaults apply_pre_processed_defaults apply_post_processed_defaults end
Apply all default values to the document which are procs.
@example Apply all the proc defaults.
model.apply_post_processed_defaults
@return [ Array<String ] The names of the proc defaults.
@since 2.4.0
# File lib/mongoid/fields.rb, line 129 def apply_post_processed_defaults post_processed_defaults.each do |name| apply_default(name) end end
Apply all default values to the document which are not procs.
@example Apply all the non-proc defaults.
model.apply_pre_processed_defaults
@return [ Array<String ] The names of the non-proc defaults.
@since 2.4.0
# File lib/mongoid/fields.rb, line 115 def apply_pre_processed_defaults pre_processed_defaults.each do |name| apply_default(name) end end
Returns an array of names for the attributes available on this object.
Provides the field names in an ORM-agnostic way. Rails
v3.1+ uses this method to automatically wrap params in JSON requests.
@example Get the field names
docment.attribute_names
@return [ Array<String> ] The field names
@since 3.0.0
# File lib/mongoid/fields.rb, line 177 def attribute_names self.class.attribute_names end
Get the name of the provided field as it is stored in the database. Used in determining if the field is aliased or not.
@example Get the database field name.
model.database_field_name(:authorization)
@param [ String, Symbol ] name The name to get.
@return [ String ] The name of the field as it’s stored in the db.
@since 3.0.7
# File lib/mongoid/fields.rb, line 192 def database_field_name(name) self.class.database_field_name(name) end
Is the provided field a lazy evaluation?
@example If the field is lazy settable.
doc.lazy_settable?(field, nil)
@param [ Field ] field The field. @param [ Object
] value The current value.
@return [ true, false ] If we set the field lazily.
@since 3.1.0
# File lib/mongoid/fields.rb, line 207 def lazy_settable?(field, value) !frozen? && value.nil? && field.lazy? end
Is the document using object ids?
@note Refactored from using delegate for class load performance.
@example Is the document using object ids?
model.using_object_ids?
@return [ true, false ] Using object ids.
# File lib/mongoid/fields.rb, line 219 def using_object_ids? self.class.using_object_ids? end