Originally written to eliminate code duplication like error trapping and logging from a non-rails Ruby application I was working on. I figured it might be useful to others in a non-rails context like Sinatra, who need something lightweight with few dependencies.
SimpleAOP is obviously inspired by Rails controller filters, but much less powerful at this point. Right now only works with instance methods. Now handles nested around filters.
SimpleAOP was cribbed and adapted from bits of DataMapper’s callbacks/filters and this StackOverflow comment.
class AOPClass include SimpleAOP before :before_method, :before_filter after :after_method, :after_filter around :around_method, :around_filter around :multiple_around, :around_one around :multiple_around, :around_two def before_method puts "before method" end def after_method puts "after method" end def before_filter puts "before filter" end def after_filter puts "after filter" end def around_method puts "around method" end def around_filter puts "start around" yield puts "end around" end def multiple_around puts "multi-around" end def around_one puts "start around one" yield puts "end around one" end def around_two puts "start around two" yield puts "end around two" end end aop = AOPClass.new aop.before_method -> before filter -> before method aop.after_method -> after method -> after filter aop.around_method -> start around -> around method -> end around aop.multiple_around -> start around one -> start around two -> multi-around -> end around two -> end around one
You can also pass arrays of methods to be filtered:
before [:test_one, :test_two, :test_three], :before_filter after [:test_one, :test_two, :test_three], :after_filter around [:test_one, :test_two], :around_filter