Module: Mongoid::Fields

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

Overview

:nodoc:

Defined Under Namespace

Modules: ClassMethods, 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



86
87
88
# File 'lib/mongoid/fields.rb', line 86

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



99
100
101
# File 'lib/mongoid/fields.rb', line 99

def options
  @options ||= {}
end

Instance Method Details

#defaultsArray<String>

Note:

Refactored from using delegate for class load performance.

Get the default fields.

Examples:

Get the defaults.

model.defaults

Returns:



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

def defaults
  self.class.defaults
end

#fieldsHash

Note:

Refactored from using delegate for class load performance.

Get the document’s fields.

Examples:

Get the fields.

model.fields

Returns:

  • (Hash)

    The fields.



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

def fields
  self.class.fields
end