Class: Mongoid::Fields::Standard
- Inherits:
-
Object
- Object
- Mongoid::Fields::Standard
- Extended by:
- Forwardable
- Defined in:
- lib/mongoid/fields/standard.rb
Overview
Represents a standard field definition (name, type, etc.) used to enforce consistent schema structure to the BSON documents which Mongoid persists.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#default_val ⇒ Object
Defines the behavior for defined fields in the document.
-
#label ⇒ Object
Defines the behavior for defined fields in the document.
-
#name ⇒ Object
Defines the behavior for defined fields in the document.
-
#options ⇒ Object
Defines the behavior for defined fields in the document.
Instance Method Summary collapse
-
#add_atomic_changes(_document, _name, key, mods, new, _old) ⇒ Object
Adds the atomic changes for this type of resizable field.
-
#association ⇒ Metadata
Get the metadata for the field if its a foreign key.
-
#eval_default(doc) ⇒ Object
Evaluate the default value and return it.
-
#foreign_key? ⇒ true | false
Is this field a foreign key?.
-
#initialize(name, options = {}) ⇒ Standard
constructor
Create the new field with a name and optional additional options.
-
#lazy? ⇒ true | false
Does this field do lazy default evaluation?.
-
#localize_present? ⇒ true | false
Is the localized field enforcing values to be present?.
-
#localized? ⇒ true | false
Is the field localized or not?.
-
#object_id_field? ⇒ true | false
Is the field a BSON::ObjectId?.
-
#pre_processed? ⇒ true | false
Does the field pre-process its default value?.
-
#type ⇒ Class
Get the type of this field - inferred from the class name.
Constructor Details
#initialize(name, options = {}) ⇒ Standard
Create the new field with a name and optional additional options.
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/mongoid/fields/standard.rb', line 69 def initialize(name, = {}) @name = name @options = @label = [:label] @default_val = [:default] # @todo: Durran, change API in 4.0 to take the class as a parameter. # This is here temporarily to address #2529 without changing the # constructor signature. return unless default_val.respond_to?(:call) define_default_method([:klass]) end |
Instance Attribute Details
#default_val ⇒ Object
Defines the behavior for defined fields in the document. Set readers for the instance variables.
13 14 15 |
# File 'lib/mongoid/fields/standard.rb', line 13 def default_val @default_val end |
#label ⇒ Object
Defines the behavior for defined fields in the document. Set readers for the instance variables.
13 14 15 |
# File 'lib/mongoid/fields/standard.rb', line 13 def label @label end |
#name ⇒ Object
Defines the behavior for defined fields in the document. Set readers for the instance variables.
13 14 15 |
# File 'lib/mongoid/fields/standard.rb', line 13 def name @name end |
#options ⇒ Object
Defines the behavior for defined fields in the document. Set readers for the instance variables.
13 14 15 |
# File 'lib/mongoid/fields/standard.rb', line 13 def @options end |
Instance Method Details
#add_atomic_changes(_document, _name, key, mods, new, _old) ⇒ Object
Adds the atomic changes for this type of resizable field.
field.add_atomic_changes(doc, "key", {}, [], [])
28 29 30 |
# File 'lib/mongoid/fields/standard.rb', line 28 def add_atomic_changes(_document, _name, key, mods, new, _old) mods[key] = new end |
#association ⇒ Metadata
Get the metadata for the field if its a foreign key.
119 120 121 |
# File 'lib/mongoid/fields/standard.rb', line 119 def association @association ||= [:association] end |
#eval_default(doc) ⇒ Object
Evaluate the default value and return it. Will handle the serialization, proc calls, and duplication if necessary.
41 42 43 44 45 46 47 |
# File 'lib/mongoid/fields/standard.rb', line 41 def eval_default(doc) if fields = doc.__selected_fields evaluated_default(doc) if included?(fields) else evaluated_default(doc) end end |
#foreign_key? ⇒ true | false
Is this field a foreign key?
55 56 57 |
# File 'lib/mongoid/fields/standard.rb', line 55 def foreign_key? false end |
#lazy? ⇒ true | false
Does this field do lazy default evaluation?
89 90 91 |
# File 'lib/mongoid/fields/standard.rb', line 89 def lazy? false end |
#localize_present? ⇒ true | false
Is the localized field enforcing values to be present?
109 110 111 |
# File 'lib/mongoid/fields/standard.rb', line 109 def localize_present? false end |
#localized? ⇒ true | false
Is the field localized or not?
99 100 101 |
# File 'lib/mongoid/fields/standard.rb', line 99 def localized? false end |
#object_id_field? ⇒ true | false
Is the field a BSON::ObjectId?
129 130 131 |
# File 'lib/mongoid/fields/standard.rb', line 129 def object_id_field? @object_id_field ||= (type == BSON::ObjectId) end |
#pre_processed? ⇒ true | false
Does the field pre-process its default value?
139 140 141 142 |
# File 'lib/mongoid/fields/standard.rb', line 139 def pre_processed? @pre_processed ||= [:pre_processed] || (default_val && !default_val.is_a?(::Proc)) end |
#type ⇒ Class
Get the type of this field - inferred from the class name.
150 151 152 |
# File 'lib/mongoid/fields/standard.rb', line 150 def type @type ||= [:type] || Object end |