Module: MongoidAutoIncrement::ClassMethods

Defined in:
lib/mongoid_auto_increment.rb

Instance Method Summary collapse

Instance Method Details

#auto_increment(name, options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/mongoid_auto_increment.rb', line 7

def auto_increment(name, options={})
  field name, :type => Integer

  unless defined? @@incrementor
    @@incrementor = MongoidAutoIncrement::Incrementor.new
  end

  options ||= {}
  seq_name  = options[:name] || "#{self.name.downcase}_#{name}"

  before_create do
    send("#{name}=", @@incrementor.inc(seq_name, options))
  end
end