Module Mongoid::Fields::ClassMethods
In: lib/mongoid/fields.rb

Methods

Public Instance methods

Returns the default values for the fields on the document.

@example Get the defaults.

  Person.defaults

@return [ Hash ] The field defaults.

Set the defaults for the class.

@example Set the defaults.

  Person.defaults = defaults

@param [ Array ] defaults The array of defaults to set.

@since 2.0.0.rc.6

Defines all the fields that are accessible on the Document For each field that is defined, a getter and setter will be added as an instance method to the Document.

@example Define a field.

  field :score, :type => Integer, :default => 0

@param [ Symbol ] name The name of the field. @param [ Hash ] options The options to pass to the field.

@option options [ Class ] :type The type of the field. @option options [ String ] :label The label for the field. @option options [ Object, Proc ] :default The field‘s default

@return [ Field ] The generated field

Return the fields for this class.

@example Get the fields.

  Person.fields

@return [ Hash ] The fields for this document.

@since 2.0.0.rc.6

Set the fields for the class.

@example Set the fields.

  Person.fields = fields

@param [ Hash ] fields The hash of fields to set.

@since 2.0.0.rc.6

When inheriting, we want to copy the fields from the parent class and set the on the child to start, mimicking the behaviour of the old class_inheritable_accessor that was deprecated in Rails edge.

@example Inherit from this class.

  Person.inherited(Doctor)

@param [ Class ] subclass The inheriting class.

@since 2.0.0.rc.6

Is the field with the provided name a BSON::ObjectId?

@example Is the field a BSON::ObjectId?

  Person.object_id_field?(:name)

@param [ String, Symbol ] name The name of the field.

@return [ true, false ] If the field is a BSON::ObjectId.

@since 2.2.0

Replace a field with a new type.

@example Replace the field.

  Model.replace_field("_id", String)

@param [ String ] name The name of the field. @param [ Class ] type The new type of field.

@return [ Serializable ] The new field.

@since 2.1.0

Protected Instance methods

Define a field attribute for the Document.

@example Set the field.

  Person.add_field(:name, :default => "Test")

@param [ Symbol ] name The name of the field. @param [ Hash ] options The hash of options.

Determine if the field name is allowed, if not raise an error.

@example Check the field name.

  Model.check_field_name!(:collection)

@param [ Symbol ] name The field name.

@raise [ Errors::InvalidField ] If the name is not allowed.

@since 2.1.8

Create the field accessors.

@example Generate the accessors.

  Person.create_accessors(:name, "name")
  person.name #=> returns the field
  person.name = "" #=> sets the field
  person.name? #=> Is the field present?

@param [ Symbol ] name The name of the field. @param [ Symbol ] meth The name of the accessor. @param [ Hash ] options The options.

Include the field methods as a module, so they can be overridden.

@example Include the fields.

  Person.generated_field_methods

Run through all custom options stored in Mongoid::Fields.options and execute the handler if the option is provided.

@example

  Mongoid::Fields.option :custom do
    puts "called"
  end

  field = Mongoid::Fields.new(:test, :custom => true)
  Person.process_options(field)
  # => "called"

@param [ Field ] field the field to process

[Validate]