Guest User

Untitled

a guest
Feb 21st, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. # What I want is a Rails or Merb plugin that:
  2. #
  3. # * Prints "START" at the start and "END" at the end of each method.
  4. # * That only requires me to enter acts_as_traceable in the class that I want to trace
  5. #
  6. # Output should look something like this:
  7. #
  8. # *** START myclass.mymethod ***
  9. # processing...
  10. # *** myclass.mymethod ***
  11. #
  12. # Currently, I got a class wrapper working using some ruby meta-programming magic (see below)
  13. #
  14. # I ran out of time to research this and would appreciate it if a meta-programming magician could
  15. # pick this up and run with it.
  16. #
  17. # Thanks! - Lex Sheehan
  18.  
  19. irb(main):167:0* # This is not thread-safe!
  20. irb(main):168:0* class Wrapper
  21. irb(main):169:1>
  22. irb(main):170:1* def initialize(obj)
  23. irb(main):171:2> @obj = obj
  24. irb(main):172:2> @around_filters = []
  25. irb(main):173:2> end
  26. irb(main):174:1>
  27. irb(main):175:1* def method_missing(meth, *args, &block)
  28. irb(main):176:2> puts "in method_missing"
  29. irb(main):177:2> embed(0, meth, *args, &block)
  30. irb(main):178:2> end
  31. irb(main):179:1>
  32. irb(main):180:1* def add_around_filter(proc)
  33. irb(main):181:2> @around_filters.push proc
  34. irb(main):182:2> end
  35. irb(main):183:1>
  36. irb(main):184:1* def remove_around_filter(proc)
  37. irb(main):185:2> @around_filters.delete proc
  38. irb(main):186:2> end
  39. irb(main):187:1>
  40. irb(main):188:1* def embed(filter_num, meth, *args, &block)
  41. irb(main):189:2> if (filter_num >= @around_filters.size)
  42. irb(main):190:3> return @obj.send(meth, *args, &block)
  43. irb(main):191:3> else
  44. irb(main):192:3* return @around_filters.at(filter_num).call(meth, lambda { embed(filter_num + 1, meth, *args, &block) })
  45. irb(main):193:3> end
  46. irb(main):194:2> end
  47. irb(main):195:1>
  48. irb(main):196:1* end # Wrapper
  49. => nil
  50. irb(main):197:0>
  51. irb(main):198:0* debug_stmts = lambda { |method, proc|
  52. irb(main):199:1* p "*** START " + method.to_s + " ***"
  53. irb(main):200:1> result = proc.call
  54. irb(main):201:1> p "*** END " + method.to_s + " ***"
  55. irb(main):202:1> result
  56. irb(main):203:1> }
  57. => #<Proc:0x0030fb9c@(irb):198>
  58. irb(main):204:0> class A
  59. irb(main):205:1> def show()
  60. irb(main):206:2> puts "show processing..."
  61. irb(main):207:2> end
  62. irb(main):208:1> end
  63. => nil
  64. irb(main):209:0> a = A.new
  65. => #<A:0x410a0>
  66. irb(main):210:0> a.show
  67. show processing...
  68. => nil
  69. irb(main):211:0> aw = Wrapper.new(A.new)
  70. => #<Wrapper:0x12d68 @around_filters=[], @obj=#<A:0x12d7c>>
  71. irb(main):212:0> aw.show
  72. in method_missing
  73. show processing...
  74. => nil
  75. irb(main):213:0> aw.add_around_filter debug_stmts
  76. => [#<Proc:0x0030fb9c@(irb):198>]
  77. irb(main):214:0> aw.show
  78. in method_missing
  79. "*** START show ***"
  80. show processing...
  81. "*** END show ***"
  82. => nil
  83. irb(main):215:0>
Add Comment
Please, Sign In to add comment