123456789_123456789_123456789_123456789_123456789_

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

Represents a standard field definition (name, type, etc.) used to enforce consistent schema structure to the ::BSON documents which ::Mongoid persists.

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(name, options = {}) ⇒ Standard

Create the new field with a name and optional additional options.

Examples:

Create the new field.

Field.new(:name, :type => String)

Parameters:

  • options (Hash) (defaults to: {})

    The field options.

Options Hash (options):

  • :type (Class)

    The class of the field.

  • :default (Object)

    The default value for the field.

  • :label (String)

    The field’s label.

[ GitHub ]

  
# File 'lib/mongoid/fields/standard.rb', line 71

def initialize(name, options = {})
  @name = name
  @options = options
  @label = options[:label]
  @default_val = options[: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(options[:klass])
  end
end

Instance Attribute Details

#default_val (rw)

Defines the behavior for defined fields in the document. ::Set readers for the instance variables.

[ GitHub ]

  
# File 'lib/mongoid/fields/standard.rb', line 15

attr_accessor :default_val, :label, :name, :options

#foreign_key?true | false (readonly)

Is this field a foreign key?

Examples:

Is the field a foreign key?

field.foreign_key?

Returns:

  • (true | false)

    If the field is a foreign key.

[ GitHub ]

  
# 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.

[ GitHub ]

  
# File 'lib/mongoid/fields/standard.rb', line 15

attr_accessor :default_val, :label, :name, :options

#lazy?true | false (readonly)

Does this field do lazy default evaluation?

Examples:

Is the field lazy?

field.lazy?

Returns:

  • (true | false)

    If the field is lazy.

[ GitHub ]

  
# 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?

Examples:

Is the localized field enforcing values to be present?

field.localize_present?

Returns:

  • (true | false)

    If the field enforces present.

[ GitHub ]

  
# File 'lib/mongoid/fields/standard.rb', line 111

def localize_present?
  false
end

#localized?true | false (readonly)

Is the field localized or not?

Examples:

Is the field localized?

field.localized?

Returns:

  • (true | false)

    If the field is localized.

[ GitHub ]

  
# 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.

[ GitHub ]

  
# File 'lib/mongoid/fields/standard.rb', line 15

attr_accessor :default_val, :label, :name, :options

#object_id_field?true | false (readonly)

Is the field a BSON::ObjectId?

Examples:

Is the field a BSON::ObjectId?

field.object_id_field?

Returns:

[ GitHub ]

  
# File 'lib/mongoid/fields/standard.rb', line 131

def object_id_field?
  @object_id_field ||= (type == BSON::ObjectId)
end

#options (rw)

Defines the behavior for defined fields in the document. ::Set readers for the instance variables.

[ GitHub ]

  
# File 'lib/mongoid/fields/standard.rb', line 15

attr_accessor :default_val, :label, :name, :options

#pre_processed?true | false (readonly)

Does the field pre-process its default value?

Examples:

Does the field pre-process the default?

field.pre_processed?

Returns:

  • (true | false)

    If the field’s default is pre-processed.

[ GitHub ]

  
# File 'lib/mongoid/fields/standard.rb', line 141

def pre_processed?
  @pre_processed ||=
    (options[: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”, {}, [], [])

Examples:

Add the atomic changes.

Parameters:

  • document (Document)

    The document to add to.

  • name (String)

    The name of the field.

  • key (String)

    The atomic location of the field.

  • mods (Hash)

    The current modifications.

  • new (Array)

    The new elements to add.

  • old (Array)

    The old elements getting removed.

[ GitHub ]

  
# File 'lib/mongoid/fields/standard.rb', line 30

def add_atomic_changes(document, name, key, mods, new, old)
  mods[key] = new
end

#associationMetadata

Get the metadata for the field if its a foreign key.

Examples:

Get the metadata.

field.

Returns:

  • (Metadata)

    The association metadata.

[ GitHub ]

  
# File 'lib/mongoid/fields/standard.rb', line 121

def association
  @association ||= options[:association]
end

#default_nameString (private)

This method is for internal use only.

Get the name of the default method for this field.

Examples:

Get the default name.

field.default_name

Returns:

  • (String)

    The method name.

[ GitHub ]

  
# File 'lib/mongoid/fields/standard.rb', line 166

def default_name
  @default_name ||= "__#{name}_default__"
end

#define_default_method(object) (private)

This method is for internal use only.
Note:

Ruby’s instance_exec was just too slow.

Define the method for getting the default on the document.

Examples:

Define the method.

field.define_default_method(doc)

Parameters:

  • object (Class | Module)

    The class or module the field is defined on.

[ GitHub ]

  
# 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.

Examples:

Evaluate the default value.

field.eval_default(document)

Parameters:

  • doc (Document)

    The document the field belongs to.

Returns:

  • (Object)

    The serialized default value.

[ GitHub ]

  
# 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.

Examples:

Eval the default proc.

field.evaluate_default_proc(band)

Parameters:

Returns:

  • (Object)

    The called proc.

[ GitHub ]

  
# 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.

Examples:

Get the evaluated default.

field.evaluated_default.

Parameters:

  • doc (Document)

    The doc being applied to.

Returns:

  • (Object)

    The default value.

[ GitHub ]

  
# 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)

Examples:

Is the field included?

field.included?(fields)

Parameters:

  • fields (Hash)

    The field limitations.

Returns:

  • (true | false)

    If the field was included.

[ GitHub ]

  
# File 'lib/mongoid/fields/standard.rb', line 196

def included?(fields)
  (fields.values.first == 1 && fields[name.to_s] == 1) ||
    (fields.values.first == 0 && !fields.has_key?(name.to_s))
end

#serialize_default(object) ⇒ Object (private)

This method is for internal use only.

This is used when default values need to be serialized. Most of the time just return the object.

Examples:

Serialize the default value.

field.serialize_default(obj)

Parameters:

  • object (Object)

    The default.

Returns:

  • (Object)

    The serialized default.

[ GitHub ]

  
# File 'lib/mongoid/fields/standard.rb', line 241

def serialize_default(object)
  mongoize(object)
end

#typeClass

Get the type of this field - inferred from the class name.

Examples:

Get the type.

field.type

Returns:

  • (Class)

    The name of the class.

[ GitHub ]

  
# File 'lib/mongoid/fields/standard.rb', line 152

def type
  @type ||= options[:type] || Object
end