Module: Mongoid::Fields::ClassMethods

Defined in:
lib/mongoid/fields.rb

Overview

:nodoc

Instance Method Summary collapse

Instance Method Details

#defaultsHash

Returns the default values for the fields on the document.

Examples:

Get the defaults.

Person.defaults

Returns:

  • (Hash)

    The field defaults.



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

def defaults
  @defaults ||= []
end

#defaults=(defaults) ⇒ Object

Set the defaults for the class.

Examples:

Set the defaults.

Person.defaults = defaults

Parameters:

  • defaults (Array)

    The array of defaults to set.

Since:

  • 2.0.0.rc.6



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

def defaults=(defaults)
  @defaults = defaults
end

#field(name, options = {}) ⇒ Field

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.

Examples:

Define a field.

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

Parameters:

  • name (Symbol)

    The name of the field.

  • options (Hash) (defaults to: {})

    The options to pass to the field.

Options Hash (options):

  • :type (Class)

    The type of the field.

  • :label (String)

    The label for the field.

  • :default (Object, Proc)

    The field’s default

Returns:

  • (Field)

    The generated field



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

def field(name, options = {})
  check_field_name!(name)
  add_field(name.to_s, options)
end

#fieldsHash

Return the fields for this class.

Examples:

Get the fields.

Person.fields

Returns:

  • (Hash)

    The fields for this document.

Since:

  • 2.0.0.rc.6



155
156
157
# File 'lib/mongoid/fields.rb', line 155

def fields
  @fields ||= {}
end

#fields=(fields) ⇒ Object

Set the fields for the class.

Examples:

Set the fields.

Person.fields = fields

Parameters:

  • fields (Hash)

    The hash of fields to set.

Since:

  • 2.0.0.rc.6



167
168
169
# File 'lib/mongoid/fields.rb', line 167

def fields=(fields)
  @fields = fields
end

#inherited(subclass) ⇒ Object

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.

Examples:

Inherit from this class.

Person.inherited(Doctor)

Parameters:

  • subclass (Class)

    The inheriting class.

Since:

  • 2.0.0.rc.6



181
182
183
184
# File 'lib/mongoid/fields.rb', line 181

def inherited(subclass)
  super
  subclass.defaults, subclass.fields = defaults.dup, fields.dup
end

#object_id_field?(name) ⇒ true, false

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

Examples:

Is the field a BSON::ObjectId?

Person.object_id_field?(:name)

Parameters:

Returns:

  • (true, false)

    If the field is a BSON::ObjectId.

Since:

  • 2.2.0



196
197
198
199
200
201
# File 'lib/mongoid/fields.rb', line 196

def object_id_field?(name)
  field_name = name.to_s
  field_name = "_id" if field_name == "id"
  field = fields[field_name]
  field ? field.object_id_field? : false
end

#replace_field(name, type) ⇒ Serializable

Replace a field with a new type.

Examples:

Replace the field.

Model.replace_field("_id", String)

Parameters:

  • name (String)

    The name of the field.

  • type (Class)

    The new type of field.

Returns:

Since:

  • 2.1.0



214
215
216
217
# File 'lib/mongoid/fields.rb', line 214

def replace_field(name, type)
  defaults.delete_one(name)
  add_field(name, fields[name].options.merge(:type => type))
end