On Tue, Apr 26, 2011 at 11:08 PM, Reginald Tan <redge.tan / gmail.com> wrote:
> Btw, I find you see any better way of implementing it instead of always
> passing a block to transaction()?

Well, I have to second what Robert Klemme said and suggest that if you
don't want the transactional behavior, you may be better off using a
different mechanism. On the other hand, if its just that a lot of your
use is one-operation transactions and you just get tired of typing
store.transaction { store[:foo] } to read from the store, then you
just need to DRY up your code by creating methods that let you get the
same effect without the typing.

To avoid monkeypatching, create a subclass of PStore:

class EasyStore < PStore
  def transaction_optional(meth, *args)
    transaction { send(meth, *args) }
  rescue PStore::Error
    send(meth, *args)
  end

  alias []= put

  [:fetch, :delete, :roots, :put].each do |meth|
    define_method((meth.to_s+"!").to_sym) do |*args|
      transaction_optional(meth, *args)
    end
  end
end