Skip to content

Instantly share code, notes, and snippets.

@roidrage
Created January 15, 2009 22:05
Show Gist options
  • Save roidrage/47660 to your computer and use it in GitHub Desktop.
Save roidrage/47660 to your computer and use it in GitHub Desktop.
module Persistence
def self.included(base)
base.extend(ClassMethods)
end
def start_transaction
puts "Starting transaction"
end
def commit
puts "Comitting"
end
module ClassMethods
def method_added(name)
return if @__wrapping_transactional_method
original_name = "#{name} without transaction"
@__wrapping_transactional_method = true
alias_method original_name, name
define_method(name) do
start_transaction
send(original_name)
commit
end
@__wrapping_transactional_method = false
end
end
end
class User
include Persistence
def save
puts "Saving instance..."
end
end
User.new.save
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment