Class: Mongoid::Haystack::Sequence

Inherits:
Object
  • Object
show all
Includes:
Document
Defined in:
lib/mongoid-haystack/sequence.rb

Constant Summary collapse

Cache =
Hash.new

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.for(name) ⇒ Object Also known as: []



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

def for(name)
  name = name.to_s

  Cache[name] ||= (
    Haystack.find_or_create(
      ->{ where(:name => name).first },
      ->{ create!(:name => name) }
    )
  )
end

.sequence_name_for(klass, fieldname) ⇒ Object



33
34
35
# File 'lib/mongoid-haystack/sequence.rb', line 33

def sequence_name_for(klass, fieldname)
  "#{ klass.name.gsub(/::/, '.').downcase }-#{ fieldname }"
end

Instance Method Details

#current_valueObject



46
47
48
# File 'lib/mongoid-haystack/sequence.rb', line 46

def current_value
  reload.value
end

#nextObject



42
43
44
# File 'lib/mongoid-haystack/sequence.rb', line 42

def next
  inc(:value, 1)
end

#reset!Object



50
51
52
# File 'lib/mongoid-haystack/sequence.rb', line 50

def reset!
  update_attributes!(:value => 0)
end