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