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

- (Standard) initialize(name, options = {})

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



98
99
100
101
102
103
# File 'lib/mongoid/fields/standard.rb', line 98

def initialize(name, options = {})
  @name = name
  @options = options
  @label = options[:label]
  @default_val = options[:default]
end

Instance Attribute Details

- (Object) default_val

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

- (Object) label

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

- (Object) name

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

- (Object) options

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

- (Object) 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.

Since:

  • 2.4.0



39
40
41
# File 'lib/mongoid/fields/standard.rb', line 39

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

- (Constraint) constraint

Get the constraint from the metadata once.

Examples:

Get the constraint.

field.constraint

Returns:

  • (Constraint)

    The relation's contraint.

Since:

  • 2.1.0



51
52
53
# File 'lib/mongoid/fields/standard.rb', line 51

def constraint
  @constraint ||= .constraint
end

- (Array) demongoized_identity_for(object)

Compute the demongoized identity of an object

Examples:

Model.demongoized_identity_for(object)

Parameters:

  • object (Object)

    The object to get identity for.

Returns:

  • (Array)

    The object's identity.

Since:

  • 3.0.0



22
23
24
# File 'lib/mongoid/fields/standard.rb', line 22

def demongoized_identity_for(object)
  [ object.object_id, object.hash ]
end

- (Object) eval_default(doc)

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



66
67
68
69
70
71
72
# File 'lib/mongoid/fields/standard.rb', line 66

def eval_default(doc)
  if fields = Threaded.selection
    evaluated_default(doc) if included?(fields)
  else
    evaluated_default(doc)
  end
end

- (true, false) foreign_key?

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



82
83
84
# File 'lib/mongoid/fields/standard.rb', line 82

def foreign_key?
  false
end

- (true, false) localized?

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



113
114
115
# File 'lib/mongoid/fields/standard.rb', line 113

def localized?
  false
end

- (Metadata) metadata

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



125
126
127
# File 'lib/mongoid/fields/standard.rb', line 125

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

- (true, false) object_id_field?

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.

Since:

  • 2.2.0



137
138
139
# File 'lib/mongoid/fields/standard.rb', line 137

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

- (true, false) pre_processed?

Does the field pre-process it's 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



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

def pre_processed?
  @pre_processed ||=
    (options[:pre_processed] || (default_val && !default_val.is_a?(::Proc)))
end

- (Class) type

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



162
163
164
# File 'lib/mongoid/fields/standard.rb', line 162

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

- (true, false) versioned?

Is this field included in versioned attributes?

Examples:

Is the field versioned?

field.versioned?

Returns:

  • (true, false)

    If the field is included in versioning.

Since:

  • 2.1.0



174
175
176
# File 'lib/mongoid/fields/standard.rb', line 174

def versioned?
  @versioned ||= (options[:versioned].nil? ? true : options[:versioned])
end