0

I'm using ruby 1.9.2-p136

What i want to do is, create two methods: before_method and after_method

When i do this in a class:

before_method :foo, :bar

it should run these two methods before any called method, i.e. if i call a class method foo1, the order in which the methods should be executed is: foo bar foo1

Also, i want a method after_method which does the opposite of before_method

Can anyone tell how to go about it??

Thanks in advance :)

Gaurav Manchanda
  • 544
  • 5
  • 22
  • possible duplicate of [Executing code for every method call in a Ruby module](http://stackoverflow.com/questions/5513558/executing-code-for-every-method-call-in-a-ruby-module) Also: http://stackoverflow.com/questions/7137238/how-can-i-intercept-method-call-in-ruby – Mladen Jablanović Dec 21 '11 at 13:01
  • Can't you just used `alias_method` to do this trivially? Seems like this is a few lines of code. – Dave Newton Dec 21 '11 at 13:37
  • Sounds like you are looking for an Aspect framework for Ruby? – KingCronus Dec 21 '11 at 12:34

1 Answers1

0

Are you looking to do this for ActiveRecord Models? Or just generic classes? Because I've written a rails plugin that adds before_filter and after_filter methods to the ActiveRecord Models.

Maybe you can use it, or look at the code and go from there: https://github.com/deleteman/Filtered-Models

Hope that helps!

If you have questions, feel free to ask :)

Deleteman
  • 8,500
  • 6
  • 25
  • 39