Module: Mongoid::Fields
- Extended by:
- ActiveSupport::Concern
- Included in:
- Components
- Defined in:
- lib/mongoid/fields.rb,
lib/mongoid/fields/standard.rb,
lib/mongoid/fields/localized.rb,
lib/mongoid/fields/foreign_key.rb
Overview
This module defines behaviour for fields.
Defined Under Namespace
Modules: ClassMethods Classes: ForeignKey, Localized, Standard
Class Method Summary (collapse)
-
+ (Object) option(option_name, &block)
Stores the provided block to be run when the option name specified is defined on a field.
-
+ (Hash) options
Return a map of custom option names to their handlers.
Instance Method Summary (collapse)
-
- (Object) apply_default(name)
Applies a single default value for the given name.
-
- (Object) apply_defaults
Apply all the defaults at once.
-
- (Array<String ] The names of the proc defaults.) apply_post_processed_defaults
Apply all default values to the document which are procs.
-
- (Array<String ] The names of the non-proc defaults.) apply_pre_processed_defaults
Apply all default values to the document which are not procs.
-
- (Object) demongoized
Cache of demongoized field data mapping field names to all previously demongoized attributes for this object.
-
- (true, false) using_object_ids?
Is the document using object ids?.
Class Method Details
+ (Object) option(option_name, &block)
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.
145 146 147 |
# File 'lib/mongoid/fields.rb', line 145 def option(option_name, &block) [option_name] = block end |
+ (Hash) options
Return a map of custom option names to their handlers.
158 159 160 |
# File 'lib/mongoid/fields.rb', line 158 def @options ||= {} end |
Instance Method Details
- (Object) apply_default(name)
Applies a single default value for the given name.
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/mongoid/fields.rb', line 73 def apply_default(name) unless attributes.has_key?(name) if field = fields[name] default = field.eval_default(self) unless default.nil? attribute_will_change!(name) attributes[name] = default end end end end |
- (Object) apply_defaults
Apply all the defaults at once.
91 92 93 94 |
# File 'lib/mongoid/fields.rb', line 91 def apply_defaults apply_pre_processed_defaults apply_post_processed_defaults end |
- (Array<String ] The names of the proc defaults.) apply_post_processed_defaults
Apply all default values to the document which are procs.
59 60 61 62 63 |
# File 'lib/mongoid/fields.rb', line 59 def apply_post_processed_defaults post_processed_defaults.each do |name| apply_default(name) end end |
- (Array<String ] The names of the non-proc defaults.) apply_pre_processed_defaults
Apply all default values to the document which are not procs.
45 46 47 48 49 |
# File 'lib/mongoid/fields.rb', line 45 def apply_pre_processed_defaults pre_processed_defaults.each do |name| apply_default(name) end end |
- (Object) demongoized
Cache of demongoized field data mapping field names to all previously demongoized attributes for this object.
The demongoized cache memoized values processed by field getters to provide and identiy map for a given attribute value. That is to say, for a field named 'list' and an @attributes == => [0,1,2] calling 'doc.list' will process '[0,1,2]' exacty once.
The identity of the source attribute is calculated using
122 123 124 |
# File 'lib/mongoid/fields.rb', line 122 def demongoized @demongoized ||= Hash.new{|hash, name| hash[name] = Hash.new} end |
- (true, false) using_object_ids?
Refactored from using delegate for class load performance.
Is the document using object ids?
104 105 106 |
# File 'lib/mongoid/fields.rb', line 104 def using_object_ids? self.class.using_object_ids? end |