Module: Mongoid::Fields

Extended by:
ActiveSupport::Concern
Included in:
Composable
Defined in:
lib/mongoid/fields.rb,
lib/mongoid/fields/standard.rb,
lib/mongoid/fields/localized.rb,
lib/mongoid/fields/foreign_key.rb,
lib/mongoid/fields/validators/macro.rb

Overview

This module defines behaviour for fields.

Defined Under Namespace

Modules: ClassMethods, Validators Classes: ForeignKey, Localized, Standard

Constant Summary

TYPE_MAPPINGS =

For fields defined with symbols use the correct class.

Since:

  • 4.0.0

{
  array: Array,
  big_decimal: BigDecimal,
  binary: BSON::Binary,
  boolean: Mongoid::Boolean,
  date: Date,
  date_time: DateTime,
  float: Float,
  hash: Hash,
  integer: Integer,
  object_id: BSON::ObjectId,
  range: Range,
  regexp: Regexp,
  set: Set,
  string: String,
  symbol: Symbol,
  time: Time
}.with_indifferent_access

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.option(option_name, &block) ⇒ Object

Stores the provided block to be run when the option name specified is defined on a field.

No assumptions are made about what sort of work the handler might perform, so it will always be called if the `option_name` key is provided in the field definition – even if it is false or nil.

Examples:

Mongoid::Fields.option :required do |model, field, value|
  model.validates_presence_of field if value
end

Parameters:

  • option_name (Symbol)

    the option name to match against

  • block (Proc)

    the handler to execute when the option is provided.

Since:

  • 2.1.0



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

def option(option_name, &block)
  options[option_name] = block
end

.optionsHash

Return a map of custom option names to their handlers.

Examples:

Mongoid::Fields.options
# => { :required => #<Proc:0x00000100976b38> }

Returns:

  • (Hash)

    the option map

Since:

  • 2.1.0



207
208
209
# File 'lib/mongoid/fields.rb', line 207

def options
  @options ||= {}
end

Instance Method Details

#apply_default(name) ⇒ Object

Applies a single default value for the given name.

Examples:

Apply a single default.

model.apply_default("name")

Parameters:

  • name (String)

    The name of the field.

Since:

  • 2.4.0



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/mongoid/fields.rb', line 95

def apply_default(name)
  unless attributes.has_key?(name)
    if field = fields[name]
      default = field.eval_default(self)
      unless default.nil? || field.lazy?
        attribute_will_change!(name)
        attributes[name] = default
      end
    end
  end
end

#apply_defaultsObject

Apply all the defaults at once.

Examples:

Apply all the defaults.

model.apply_defaults

Since:

  • 2.4.0



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

def apply_defaults
  apply_pre_processed_defaults
  apply_post_processed_defaults
end

#apply_post_processed_defaultsArray<String ] The names of the proc defaults.

Apply all default values to the document which are procs.

Examples:

Apply all the proc defaults.

model.apply_post_processed_defaults

Returns:

  • (Array<String ] The names of the proc defaults.)

    Array<String ] The names of the proc defaults.

Since:

  • 2.4.0



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

def apply_post_processed_defaults
  post_processed_defaults.each do |name|
    apply_default(name)
  end
end

#apply_pre_processed_defaultsArray<String ] The names of the non-proc defaults.

Apply all default values to the document which are not procs.

Examples:

Apply all the non-proc defaults.

model.apply_pre_processed_defaults

Returns:

  • (Array<String ] The names of the non-proc defaults.)

    Array<String ] The names of the non-proc defaults.

Since:

  • 2.4.0



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

def apply_pre_processed_defaults
  pre_processed_defaults.each do |name|
    apply_default(name)
  end
end

#attribute_namesArray<String>

Returns an array of names for the attributes available on this object.

Provides the field names in an ORM-agnostic way. Rails v3.1+ uses this method to automatically wrap params in JSON requests.

Examples:

Get the field names

docment.attribute_names

Returns:

  • (Array<String>)

    The field names

Since:

  • 3.0.0



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

def attribute_names
  self.class.attribute_names
end

#database_field_name(name) ⇒ String

Get the name of the provided field as it is stored in the database. Used in determining if the field is aliased or not.

Examples:

Get the database field name.

model.database_field_name(:authorization)

Parameters:

  • name (String, Symbol)

    The name to get.

Returns:

  • (String)

    The name of the field as it's stored in the db.

Since:

  • 3.0.7



144
145
146
# File 'lib/mongoid/fields.rb', line 144

def database_field_name(name)
  self.class.database_field_name(name)
end

#lazy_settable?(field, value) ⇒ true, false

Is the provided field a lazy evaluation?

Examples:

If the field is lazy settable.

doc.lazy_settable?(field, nil)

Parameters:

  • field (Field)

    The field.

  • value (Object)

    The current value.

Returns:

  • (true, false)

    If we set the field lazily.

Since:

  • 3.1.0



159
160
161
# File 'lib/mongoid/fields.rb', line 159

def lazy_settable?(field, value)
  !frozen? && value.nil? && field.lazy?
end

#using_object_ids?true, false

Note:

Refactored from using delegate for class load performance.

Is the document using object ids?

Examples:

Is the document using object ids?

model.using_object_ids?

Returns:

  • (true, false)

    Using object ids.



171
172
173
# File 'lib/mongoid/fields.rb', line 171

def using_object_ids?
  self.class.using_object_ids?
end