mongoid-autoinc

A mongoid plugin to add auto incrementing fields to your documents.

<img src=“https://secure.travis-ci.org/80beans/mongoid-autoinc.png” />

Installation

in gemfile:

gem 'mongoid-autoinc'

in class:

require 'autoinc'

Usage

# app/models/user.rb
class User
  include Mongoid::Document
  include Mongoid::Autoinc
  field :name
  field :number, :type => Integer

  auto_increment :number
end

user = User.create(:name => 'Dr. Percival "Perry" Ulysses Cox')
user.id # BSON::ObjectId('4d1d150d30f2246bc6000001')
user.number # 1

another_user = User.create(:name => 'Bob Kelso')
another_user.number # 2

Scopes

You can scope on document fields. For example:

class PatientFile
  include Mongoid::Document
  include Mongoid::Autoinc

  field :name
  field :number, :type => Integer

  auto_increment :number, :scope => :patient_id

  belongs_to :patient

end

Development

$ gem install bundler (if you don't have it)
$ bundle install
$ bundle exec spec

See LICENSE for details