module Mongoid::Autoinc::ClassMethods

Mongoid::Autoinc class methods to allow for autoincrementing fields.

Public Instance Methods

incrementing_fields() click to toggle source

Returns all incrementing fields of the document

@example

class Invoice
  include Mongoid::Document
  include Mongoid::Autoinc

  field :number, type: Integer
  increments :number
end
Invoice.incrementing_fields # => {number: {auto: true}}

@return [ Hash ] Hash with fields and their autoincrement options

# File lib/autoinc.rb, line 39
def incrementing_fields
  if superclass.respond_to?(:incrementing_fields)
    @incrementing_fields ||= superclass.incrementing_fields.dup
  else
    @incrementing_fields ||= {}
  end
end
increments(field, options = {}) click to toggle source

Set an autoincrementing field for a Mongoid::Document

@param [ Symbol ] field The name of the field to apply autoincrement to @param [ Hash ] options The options to pass to that field

@example

class Invoice
  include Mongoid::Document
  include Mongoid::Autoinc

  field :number, type: Integer
  increments :number
end

@example

class User
  include Mongoid::Document
  include Mongoid::Autoinc

  field :number, type: Integer
  increments :number, auto: false
end
# File lib/autoinc.rb, line 69
def increments(field, options = {})
  incrementing_fields[field] = options.reverse_merge!(auto: true)
  attr_protected(field) if respond_to?(:attr_protected)
end