Class: Blodsband::Riak::Counter
- Inherits:
-
Object
- Object
- Blodsband::Riak::Counter
- Defined in:
- lib/blodsband/riak/counter.rb
Instance Method Summary collapse
-
#inc(step = 1) ⇒ Integer
Increment this counter.
-
#initialize(bucket, key) ⇒ Counter
constructor
Create a concurrent counter.
-
#val(current = nil) ⇒ Integer
Get the value of this counter.
Constructor Details
#initialize(bucket, key) ⇒ Counter
Create a concurrent counter.
14 15 16 17 |
# File 'lib/blodsband/riak/counter.rb', line 14 def initialize(bucket, key) @bucket = bucket @key = key end |
Instance Method Details
#inc(step = 1) ⇒ Integer
Increment this counter.
28 29 30 31 32 33 34 |
# File 'lib/blodsband/riak/counter.rb', line 28 def inc(step = 1) raise RuntimeError.new("#{self}.inc(..) only allows positive values") if step < 0 current = get_resolved current["last"] = current["last"] + current["step"] current["step"] = step val(get_resolved(@bucket.put(@key, current, :riak_params => {:readbody => true}))) end |
#val(current = nil) ⇒ Integer
Get the value of this counter.
43 44 45 46 |
# File 'lib/blodsband/riak/counter.rb', line 43 def val(current = nil) current = get_resolved if current.nil? return current["last"] + current["step"] end |