Class: Mongoid::Fields::Standard

Inherits:
Object
  • Object
show all
Defined in:
lib/mongoid/fields/standard.rb

Direct Known Subclasses

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.

Since:

  • 3.0.0



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/mongoid/fields/standard.rb', line 84

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_valObject

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



8
9
10
# File 'lib/mongoid/fields/standard.rb', line 8

def default_val
  @default_val
end

#labelObject

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



8
9
10
# File 'lib/mongoid/fields/standard.rb', line 8

def label
  @label
end

#nameObject

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



8
9
10
# File 'lib/mongoid/fields/standard.rb', line 8

def name
  @name
end

#optionsObject

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



8
9
10
# File 'lib/mongoid/fields/standard.rb', line 8

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.

Since:

  • 2.4.0



25
26
27
# File 'lib/mongoid/fields/standard.rb', line 25

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

#constraintConstraint

Get the constraint from the metadata once.

Examples:

Get the constraint.

field.constraint

Returns:

  • (Constraint)

    The relation’s contraint.

Since:

  • 2.1.0



37
38
39
# File 'lib/mongoid/fields/standard.rb', line 37

def constraint
  @constraint ||= .constraint
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.

Since:

  • 2.1.8



52
53
54
55
56
57
58
# File 'lib/mongoid/fields/standard.rb', line 52

def eval_default(doc)
  if fields = Threaded.selection(doc.criteria_instance_id)
    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.

Since:

  • 2.4.0



68
69
70
# File 'lib/mongoid/fields/standard.rb', line 68

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.

Since:

  • 3.1.0



106
107
108
# File 'lib/mongoid/fields/standard.rb', line 106

def lazy?
  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.

Since:

  • 2.3.0



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

def localized?
  false
end

#metadataMetadata

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

Examples:

Get the metadata.

field.

Returns:

  • (Metadata)

    The relation metadata.

Since:

  • 2.2.0



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

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

#object_id_field?true, false

Is the field a Moped::BSON::ObjectId?

Examples:

Is the field a Moped::BSON::ObjectId?

field.object_id_field?

Returns:

  • (true, false)

    If the field is a Moped::BSON::ObjectId.

Since:

  • 2.2.0



142
143
144
# File 'lib/mongoid/fields/standard.rb', line 142

def object_id_field?
  @object_id_field ||= (type == Moped::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.

Since:

  • 3.0.0



154
155
156
157
# File 'lib/mongoid/fields/standard.rb', line 154

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.

Since:

  • 2.1.0



167
168
169
# File 'lib/mongoid/fields/standard.rb', line 167

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