Module: Mongoid::Fields

Extended by:
ActiveSupport::Concern
Included in:
Components
Defined in:
lib/mongoid/fields.rb,
lib/mongoid/fields/mappings.rb,
lib/mongoid/fields/internal/set.rb,
lib/mongoid/fields/serializable.rb,
lib/mongoid/fields/internal/date.rb,
lib/mongoid/fields/internal/hash.rb,
lib/mongoid/fields/internal/time.rb,
lib/mongoid/fields/internal/array.rb,
lib/mongoid/fields/internal/float.rb,
lib/mongoid/fields/internal/range.rb,
lib/mongoid/fields/internal/bignum.rb,
lib/mongoid/fields/internal/binary.rb,
lib/mongoid/fields/internal/fixnum.rb,
lib/mongoid/fields/internal/object.rb,
lib/mongoid/fields/internal/string.rb,
lib/mongoid/fields/internal/symbol.rb,
lib/mongoid/fields/internal/boolean.rb,
lib/mongoid/fields/internal/integer.rb,
lib/mongoid/fields/internal/date_time.rb,
lib/mongoid/fields/internal/localized.rb,
lib/mongoid/fields/internal/nil_class.rb,
lib/mongoid/fields/internal/object_id.rb,
lib/mongoid/fields/internal/true_class.rb,
lib/mongoid/fields/internal/big_decimal.rb,
lib/mongoid/fields/internal/false_class.rb,
lib/mongoid/fields/internal/timekeeping.rb,
lib/mongoid/fields/internal/time_with_zone.rb,
lib/mongoid/fields/internal/foreign_keys/array.rb,
lib/mongoid/fields/internal/foreign_keys/object.rb

Overview

:nodoc:

Defined Under Namespace

Modules: ClassMethods, Internal, Mappings, Serializable

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



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

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



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

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



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/mongoid/fields.rb', line 91

def apply_default(name)
  unless attributes.has_key?(name)
    if field = fields[name]
      default = field.eval_default(self)
      unless default.nil?
        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



109
110
111
112
# File 'lib/mongoid/fields.rb', line 109

def apply_defaults
  apply_non_proc_defaults
  apply_proc_defaults
end

#apply_non_proc_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_non_proc_defaults

Returns:

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

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

Since:

  • 2.4.0



63
64
65
66
67
# File 'lib/mongoid/fields.rb', line 63

def apply_non_proc_defaults
  non_proc_defaults.each do |name|
    apply_default(name)
  end
end

#apply_proc_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_proc_defaults

Returns:

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

    Array<String ] The names of the proc defaults.

Since:

  • 2.4.0



77
78
79
80
81
# File 'lib/mongoid/fields.rb', line 77

def apply_proc_defaults
  proc_defaults.each do |name|
    apply_default(name)
  end
end

#defaultsArray<String ] The names of all defaults.

Get a list of all the default fields for the model.

Examples:

Get a list of the defaults.

model.defaults

Returns:

Since:

  • 2.4.0



122
123
124
# File 'lib/mongoid/fields.rb', line 122

def defaults
  self.class.defaults
end