Last active
December 24, 2015 01:29
-
-
Save haileys/6723428 to your computer and use it in GitHub Desktop.
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
# Method instrumentation decorator with Ruby 2.1's new "method definition returns method name" feature | |
class Module | |
def instrument(notification_name, mid) | |
meth = instance_method(mid) | |
define_method(mid) { |*args, &bk| | |
ActiveSupport::Notifications.instrument(notification_name) do | |
meth.bind(self).call(*args, &bk) | |
end | |
} | |
mid | |
end | |
end | |
class MyClass | |
instrument "my_class.some_expensive_method", | |
def some_expensive_method | |
# do things | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment