Module: Mongoid::Sequence

Extended by:
ActiveSupport::Concern
Defined in:
lib/mongoid-sequence.rb,
lib/mongoid-sequence/version.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

VERSION =
"0.1"

Instance Method Summary collapse

Instance Method Details

#set_sequenceObject



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mongoid-sequence.rb', line 21

def set_sequence
  sequences = self.db.collection("__sequences")
  self.class.sequence_fields.each do |field|
    next_sequence = sequences.find_and_modify(:query  => {"_id" => "#{self.class.name.underscore}_#{field}"},
                                              :update => {"$inc" => {"seq" => 1}},
                                              :new    => true,
                                              :upsert => true)

    self[field] = next_sequence["seq"]
  end if self.class.sequence_fields
end