Sha256: 756867aaa37e4188c08b555dd51d78f1ae24a15c2412f254a0d4835cac535a99

Contents?: true

Size: 591 Bytes

Versions: 1

Compression:

Stored size: 591 Bytes

Contents

require 'ostruct'

module HashPersistent  
  class Counter
    def initialize(store, key)
      raise ArgumentError unless store.is_a?(HashPersistent::Store) and key.respond_to?(:to_s) and key.to_s.length != 0
      @store = store
      @key = key
      
      unless @store.find(@key)
        counter = OpenStruct.new
        counter.key = @key
        counter.count = 0
        @store.save(counter)
      end
    end
    
    def next
      counter = @store.find(@key)
      next_count = counter.count
      counter.count += 1
      @store.save(counter)
      next_count
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
kissifer-hash-persistent-0.0.0 lib/hash-persistent/counter.rb