Advertisement
Guest User

Untitled

a guest
May 29th, 2015
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. class Object
  2. def self.measure(method, category_name=nil)
  3. alias_method "#{method}_unmeasured", method
  4. define_method method do |*args|
  5. cat_name = if category_name.respond_to?(:call)
  6. # If proc eval in current context and pass args
  7. self.instance_exec args, &category_name
  8. elsif category_name && category_name.respond_to?(:to_s)
  9. # Stringify that thing you gave me
  10. category_name.to_s
  11. else
  12. # Try to be smart
  13. args.first.try(:to_s)
  14. end
  15.  
  16. ActiveSupport::Notifications.instrument(
  17. "#{self.class.name}.#{method.to_s}",
  18. :name => cat_name
  19. ) do
  20. send "#{method}_unmeasured", *args
  21. end
  22. end
  23. end
  24. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement