Class: Mongoid::Fields::Standard

Inherits:
Object
  • Object
show all
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

Encrypted, ForeignKey, Localized

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/mongoid/fields/standard.rb', line 69

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.
  return unless default_val.respond_to?(:call)

  define_default_method(options[:klass])
end

Instance Attribute Details

#default_valObject

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

#labelObject

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

#nameObject

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

#optionsObject

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
  @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", {}, [], [])

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.



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

#associationMetadata

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

Examples:

Get the metadata.

field.

Returns:

  • (Metadata)

    The association metadata.



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

def association
  @association ||= options[:association]
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.



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?

Examples:

Is the field a foreign key?

field.foreign_key?

Returns:

  • (true | false)

    If the field is 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?

Examples:

Is the field lazy?

field.lazy?

Returns:

  • (true | false)

    If the field is lazy.



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?

Examples:

Is the localized field enforcing values to be present?

field.localize_present?

Returns:

  • (true | false)

    If the field enforces 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?

Examples:

Is the field localized?

field.localized?

Returns:

  • (true | false)

    If the field is localized.



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?

Examples:

Is the field a BSON::ObjectId?

field.object_id_field?

Returns:

  • (true | false)

    If the field is 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?

Examples:

Does the field pre-process the default?

field.pre_processed?

Returns:

  • (true | false)

    If the field's default is pre-processed.



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

def pre_processed?
  @pre_processed ||=
    options[:pre_processed] || (default_val && !default_val.is_a?(::Proc))
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.



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

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