Skip to content

Instantly share code, notes, and snippets.

@rjungemann
Created February 25, 2010 23:00
Show Gist options
  • Save rjungemann/315146 to your computer and use it in GitHub Desktop.
Save rjungemann/315146 to your computer and use it in GitHub Desktop.
Moneta transactions
# This code adds an instance method called "transact" to all Moneta stores.
# You pass it a key and a block, and it pipes in the value for that key in the
# store. Whatever gets returned by the block gets saved over the value in the
# store.
module Moneta
module Expires
def transact(key, &blk); self[key] = yield(self[key]) end
end
end
# # usage:
# require 'moneta'
# require 'moneta/memory'
# require "#{File.dirname(__FILE__)}/transact"
#
# m = Moneta::Memory.new
# m.transact("user-john") do |user|
# user ||= {}
# user["last_name"] = "doe"
# user
# end
# m["user-john"] #=> { "last_name" => "doe" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment