Created
February 25, 2010 23:00
-
-
Save rjungemann/315146 to your computer and use it in GitHub Desktop.
Moneta transactions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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