Created
June 5, 2014 09:01
-
-
Save stevenjack/5179795998028e6a0703 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 AOP | |
def around(fn_name) | |
old_method = instance_method(fn_name) | |
define_method(fn_name) do |*args| | |
yield :before, args if block_given? | |
old_method.bind(self).call *args | |
yield :after, args if block_given? | |
end | |
end | |
end | |
class Foo | |
extend AOP | |
def process(msg) | |
puts msg | |
end | |
end | |
Foo.around('process') do |state, args| | |
if state == :before | |
puts 'Blah 1' | |
elsif state == :after | |
puts 'Blah 2' | |
end | |
end | |
Foo.new.process('hai') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment