Module: Mongoid::Fields
- Extended by:
- ActiveSupport::Concern
- Included in:
- Composable
- Defined in:
- lib/mongoid/fields.rb,
lib/mongoid/fields/standard.rb,
lib/mongoid/fields/localized.rb,
lib/mongoid/fields/foreign_key.rb,
lib/mongoid/fields/validators/macro.rb
Overview
This module defines behavior for fields.
Defined Under Namespace
Modules: ClassMethods, Validators Classes: ForeignKey, Localized, Standard
Constant Summary collapse
- StringifiedSymbol =
Mongoid::StringifiedSymbol
- TYPE_MAPPINGS =
For fields defined with symbols use the correct class.
{ array: Array, big_decimal: BigDecimal, binary: BSON::Binary, boolean: Mongoid::Boolean, date: Date, date_time: DateTime, float: Float, hash: Hash, integer: Integer, object_id: BSON::ObjectId, range: Range, regexp: Regexp, set: Set, string: String, stringified_symbol: StringifiedSymbol, symbol: Symbol, time: Time }.with_indifferent_access
- IDS =
Constant for all names of the id field in a document.
[ :_id, :id, '_id', 'id' ].freeze
Class Method Summary collapse
-
.option(option_name, &block) ⇒ Object
Stores the provided block to be run when the option name specified is defined on a field.
-
.options ⇒ Hash
Return a map of custom option names to their handlers.
Instance Method Summary collapse
-
#apply_default(name) ⇒ Object
Applies a single default value for the given name.
-
#apply_defaults ⇒ Object
Apply all the defaults at once.
-
#apply_post_processed_defaults ⇒ Array<String ] The names of the proc defaults.
Apply all default values to the document which are procs.
-
#apply_pre_processed_defaults ⇒ Array<String ] The names of the non-proc defaults.
Apply all default values to the document which are not procs.
-
#attribute_names ⇒ Array<String>
Returns an array of names for the attributes available on this object.
-
#database_field_name(name) ⇒ String
Get the name of the provided field as it is stored in the database.
-
#lazy_settable?(field, value) ⇒ true, false
Is the provided field a lazy evaluation?.
-
#using_object_ids? ⇒ true, false
Is the document using object ids?.
Class Method Details
.option(option_name, &block) ⇒ Object
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.
204 205 206 |
# File 'lib/mongoid/fields.rb', line 204 def option(option_name, &block) [option_name] = block end |
.options ⇒ Hash
Return a map of custom option names to their handlers.
217 218 219 |
# File 'lib/mongoid/fields.rb', line 217 def @options ||= {} end |
Instance Method Details
#apply_default(name) ⇒ Object
Applies a single default value for the given name.
105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/mongoid/fields.rb', line 105 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_defaults ⇒ Object
Apply all the defaults at once.
123 124 125 126 |
# File 'lib/mongoid/fields.rb', line 123 def apply_defaults apply_pre_processed_defaults apply_post_processed_defaults end |
#apply_post_processed_defaults ⇒ Array<String ] The names of the proc defaults.
Apply all default values to the document which are procs.
91 92 93 94 95 |
# File 'lib/mongoid/fields.rb', line 91 def apply_post_processed_defaults post_processed_defaults.each do |name| apply_default(name) end end |
#apply_pre_processed_defaults ⇒ Array<String ] The names of the non-proc defaults.
Apply all default values to the document which are not procs.
77 78 79 80 81 |
# File 'lib/mongoid/fields.rb', line 77 def apply_pre_processed_defaults pre_processed_defaults.each do |name| apply_default(name) end end |
#attribute_names ⇒ Array<String>
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.
139 140 141 |
# File 'lib/mongoid/fields.rb', line 139 def attribute_names self.class.attribute_names end |
#database_field_name(name) ⇒ String
Get the name of the provided field as it is stored in the database. Used in determining if the field is aliased or not.
154 155 156 |
# File 'lib/mongoid/fields.rb', line 154 def database_field_name(name) self.class.database_field_name(name) end |
#lazy_settable?(field, value) ⇒ true, false
Is the provided field a lazy evaluation?
169 170 171 |
# File 'lib/mongoid/fields.rb', line 169 def lazy_settable?(field, value) !frozen? && value.nil? && field.lazy? end |
#using_object_ids? ⇒ true, false
Refactored from using delegate for class load performance.
Is the document using object ids?
181 182 183 |
# File 'lib/mongoid/fields.rb', line 181 def using_object_ids? self.class.using_object_ids? end |