Class: Mongoid::Fields::Standard
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Subclasses:
|
|
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
Forwardable
|
|
Inherits: | Object |
Defined in: | lib/mongoid/fields/standard.rb |
Overview
Class Method Summary
-
.new(name, options = {}) ⇒ Standard
constructor
Create the new field with a name and optional additional options.
Instance Attribute Summary
-
#default_val
rw
Defines the behavior for defined fields in the document.
-
#foreign_key? ⇒ true | false
readonly
Is this field a foreign key?
-
#label
rw
Defines the behavior for defined fields in the document.
-
#lazy? ⇒ true | false
readonly
Does this field do lazy default evaluation?
-
#localize_present? ⇒ true | false
readonly
Is the localized field enforcing values to be present?
-
#localized? ⇒ true | false
readonly
Is the field localized or not?
-
#name
rw
Defines the behavior for defined fields in the document.
-
#object_id_field? ⇒ true | false
readonly
Is the field a BSON::ObjectId?
-
#options
rw
Defines the behavior for defined fields in the document.
-
#pre_processed? ⇒ true | false
readonly
Does the field pre-process its default value?
Instance Method Summary
-
#add_atomic_changes(document, name, key, mods, new, old)
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.
-
#type ⇒ Class
Get the type of this field - inferred from the class name.
-
#default_name ⇒ String
private
Internal use only
Internal use only
Get the name of the default method for this field.
-
#define_default_method(object)
private
Internal use only
Internal use only
Define the method for getting the default on the document.
-
#evaluate_default_proc(doc) ⇒ Object
private
Evaluate the default proc.
-
#evaluated_default(doc) ⇒ Object
private
Get the evaluated default.
-
#included?(fields) ⇒ true | false
private
Is the field included in the fields that were returned from the database? We can apply the default if:
-
#serialize_default(object) ⇒ Object
private
Internal use only
Internal use only
This is used when default values need to be serialized.
Constructor Details
.new(name, options = {}) ⇒ Standard
Create the new field with a name and optional additional options.
# 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 (rw)
Defines the behavior for defined fields in the document. ::Set
readers for the instance variables.
#foreign_key? ⇒ true
| false
(readonly)
Is this field a foreign key?
# File 'lib/mongoid/fields/standard.rb', line 57
def foreign_key? false end
#label (rw)
Defines the behavior for defined fields in the document. ::Set
readers for the instance variables.
# File 'lib/mongoid/fields/standard.rb', line 15
attr_accessor :default_val, :label, :name, :
#lazy? ⇒ true
| false
(readonly)
Does this field do lazy default evaluation?
# File 'lib/mongoid/fields/standard.rb', line 91
def lazy? false end
#localize_present? ⇒ true
| false
(readonly)
Is the localized field enforcing values to be present?
# File 'lib/mongoid/fields/standard.rb', line 111
def localize_present? false end
#localized? ⇒ true
| false
(readonly)
Is the field localized or not?
# File 'lib/mongoid/fields/standard.rb', line 101
def localized? false end
#name (rw)
Defines the behavior for defined fields in the document. ::Set
readers for the instance variables.
# File 'lib/mongoid/fields/standard.rb', line 15
attr_accessor :default_val, :label, :name, :
#object_id_field? ⇒ true
| false
(readonly)
Is the field a BSON::ObjectId?
#options (rw)
Defines the behavior for defined fields in the document. ::Set
readers for the instance variables.
# File 'lib/mongoid/fields/standard.rb', line 15
attr_accessor :default_val, :label, :name, :
#pre_processed? ⇒ true
| false
(readonly)
Does the field pre-process its default value?
# File 'lib/mongoid/fields/standard.rb', line 141
def pre_processed? @pre_processed ||= ( [:pre_processed] || (default_val && !default_val.is_a?(::Proc))) end
Instance Method Details
#add_atomic_changes(document, name, key, mods, new, old)
Adds the atomic changes for this type of resizable field.
field.add_atomic_changes(doc, “key”, {}, [], [])
#association ⇒ Metadata
Get the metadata for the field if its a foreign key.
# File 'lib/mongoid/fields/standard.rb', line 121
def association @association ||= [:association] end
#default_name ⇒ String (private)
Get the name of the default method for this field.
# File 'lib/mongoid/fields/standard.rb', line 166
def default_name @default_name ||= "__#{name}_default__" end
#define_default_method(object) (private)
Ruby’s instance_exec was just too slow.
Define the method for getting the default on the document.
# File 'lib/mongoid/fields/standard.rb', line 181
def define_default_method(object) object.__send__(:define_method, default_name, default_val) end
#eval_default(doc) ⇒ Object
Evaluate the default value and return it. Will handle the serialization, proc calls, and duplication if necessary.
# 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
#evaluate_default_proc(doc) ⇒ Object
(private)
Evaluate the default proc. In some cases we need to instance exec, in others we don’t.
# File 'lib/mongoid/fields/standard.rb', line 226
def evaluate_default_proc(doc) serialize_default(doc.__send__(default_name)) end
#evaluated_default(doc) ⇒ Object
(private)
Get the evaluated default.
# File 'lib/mongoid/fields/standard.rb', line 209
def evaluated_default(doc) if default_val.respond_to?(:call) evaluate_default_proc(doc) else serialize_default(default_val.__deep_copy__) end end
#included?(fields) ⇒ true
| false
(private)
Is the field included in the fields that were returned from the database? We can apply the default if:
1. The field is included in an only limitation (field: 1)
2. The field is not excluded in a without limitation (field: 0)
#serialize_default(object) ⇒ Object
(private)
This is used when default values need to be serialized. Most of the time just return the object.
# File 'lib/mongoid/fields/standard.rb', line 241
def serialize_default(object) mongoize(object) end
#type ⇒ Class
Get the type of this field - inferred from the class name.
# File 'lib/mongoid/fields/standard.rb', line 152
def type @type ||= [:type] || Object end