Class: MongoidAutoIncrement::Incrementor::Sequence
- Inherits:
-
Object
- Object
- MongoidAutoIncrement::Incrementor::Sequence
- Defined in:
- lib/mongoid_auto_increment/incrementor.rb
Instance Method Summary collapse
- #current ⇒ Object
- #inc ⇒ Object
-
#initialize(sequence, collection_name, seed, step) ⇒ Sequence
constructor
A new instance of Sequence.
Constructor Details
#initialize(sequence, collection_name, seed, step) ⇒ Sequence
Returns a new instance of Sequence.
6 7 8 9 10 11 |
# File 'lib/mongoid_auto_increment/incrementor.rb', line 6 def initialize(sequence, collection_name, seed, step) @sequence = sequence.to_s @collection = collection_name.to_s exists? || create(seed) @step = step.to_i end |
Instance Method Details
#current ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/mongoid_auto_increment/incrementor.rb', line 28 def current if defined?(::Mongoid::VERSION) && ::Mongoid::VERSION >= '3' collection.find(query).one["number"] else collection.find_one(query)["number"] end end |
#inc ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/mongoid_auto_increment/incrementor.rb', line 13 def inc if defined?(::Mongoid::VERSION) && ::Mongoid::VERSION >= '5' collection.find(query).find_one_and_update({ '$inc' => { number: @step } }, new: true, upsert: true, return_document: :after)['number'] elsif defined?(::Mongoid::VERSION) && ::Mongoid::VERSION >= '3' collection.find(query).modify({ '$inc' => { number: @step } }, new: true, upsert: true)['number'] else opts = { "query" => query, "update" => {"$inc" => { "number" => @step }}, "new" => true # return the modified document } collection.find_and_modify(opts)["number"] end end |