Created
January 15, 2009 22:05
-
-
Save roidrage/47660 to your computer and use it in GitHub Desktop.
This file contains 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
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