Class: MongoSequence

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo_sequence.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ MongoSequence

Returns a new instance of MongoSequence.



29
30
31
# File 'lib/mongo_sequence.rb', line 29

def initialize(name)
  @name = name.to_s
end

Class Attribute Details

.databaseObject



8
9
10
11
12
# File 'lib/mongo_sequence.rb', line 8

def database
  return @database if @database
  return MongoMapper.database if defined?(MongoMapper) && MongoMapper.database
  return Mongoid.database if defined?(Mongoid) && Mongoid.database
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



27
28
29
# File 'lib/mongo_sequence.rb', line 27

def name
  @name
end

Class Method Details

.[](name) ⇒ Object



18
19
20
# File 'lib/mongo_sequence.rb', line 18

def [](name)
  new(name)
end

.[]=(name, integer) ⇒ Object



22
23
24
# File 'lib/mongo_sequence.rb', line 22

def []=(name, integer)
  self[name].current = integer
end

.collectionObject



14
15
16
# File 'lib/mongo_sequence.rb', line 14

def collection
  database['sequences']
end

Instance Method Details

#collectionObject



33
34
35
# File 'lib/mongo_sequence.rb', line 33

def collection
  MongoSequence.collection
end

#currentObject



41
42
43
# File 'lib/mongo_sequence.rb', line 41

def current
  current_after_update(:$set => {}) # noop that works
end

#current=(integer) ⇒ Object



45
46
47
# File 'lib/mongo_sequence.rb', line 45

def current=(integer)
  current_after_update(:current => integer)
end

#nextObject



37
38
39
# File 'lib/mongo_sequence.rb', line 37

def next
  current_after_update(:$inc => { :current => 1 })
end