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.
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/mongoid/fields/standard.rb', line 71 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. if default_val.respond_to?(:call) define_default_method([:klass]) end end |
Instance Attribute Details
#default_val ⇒ Object
Defines the behavior for defined fields in the document. Set readers for the instance variables.
15 16 17 |
# File 'lib/mongoid/fields/standard.rb', line 15 def default_val @default_val end |
#label ⇒ Object
Defines the behavior for defined fields in the document. Set readers for the instance variables.
15 16 17 |
# File 'lib/mongoid/fields/standard.rb', line 15 def label @label end |
#name ⇒ Object
Defines the behavior for defined fields in the document. Set readers for the instance variables.
15 16 17 |
# File 'lib/mongoid/fields/standard.rb', line 15 def name @name end |
#options ⇒ Object
Defines the behavior for defined fields in the document. Set readers for the instance variables.
15 16 17 |
# File 'lib/mongoid/fields/standard.rb', line 15 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”, {}, [], [])
30 31 32 |
# File 'lib/mongoid/fields/standard.rb', line 30 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.
121 122 123 |
# File 'lib/mongoid/fields/standard.rb', line 121 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.
43 44 45 46 47 48 49 |
# File 'lib/mongoid/fields/standard.rb', line 43 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?
57 58 59 |
# File 'lib/mongoid/fields/standard.rb', line 57 def foreign_key? false end |
#lazy? ⇒ true | false
Does this field do lazy default evaluation?
91 92 93 |
# File 'lib/mongoid/fields/standard.rb', line 91 def lazy? false end |
#localize_present? ⇒ true | false
Is the localized field enforcing values to be present?
111 112 113 |
# File 'lib/mongoid/fields/standard.rb', line 111 def localize_present? false end |
#localized? ⇒ true | false
Is the field localized or not?
101 102 103 |
# File 'lib/mongoid/fields/standard.rb', line 101 def localized? false end |
#object_id_field? ⇒ true | false
Is the field a BSON::ObjectId?
131 132 133 |
# File 'lib/mongoid/fields/standard.rb', line 131 def object_id_field? @object_id_field ||= (type == BSON::ObjectId) end |
#pre_processed? ⇒ true | false
Does the field pre-process its default value?
141 142 143 144 |
# File 'lib/mongoid/fields/standard.rb', line 141 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.
152 153 154 |
# File 'lib/mongoid/fields/standard.rb', line 152 def type @type ||= [:type] || Object end |