i was suppose to work on some of my other projects, but i passed my time by writing a simple aop implementation in ruby.
it is neither powerful like AspectJ nor comparable with AspectR. however, i was having fun with my day off.
here is how my simple example is running-
def test_aop
# apply pointcuts
apply_advices(:before, /^do_.+/, SimpleService, SimpleServiceAdvice, :before_do)
apply_advices(:around, /^do_.+/, SimpleService, SimpleServiceAdvice, :around_do)
apply_advices(:after, /^do_.+/, SimpleService, SimpleServiceAdvice, :after_do)simple_service = SimpleService.new
simple_service.do_1(“A”)
end
here is the parameter name.
apply_advices(type_of_advice, pointcuts_in_regex, service_class, advice_class, advice_method)
instead of making aspectJ type pointcuts syntax, i have used regex, which is fine for the time being.
if you run this code you will find the following output -
Before execution.
Around {
1 performed – A
}
After execution.
now let’s have a look on my SimpleService class.
class SimpleService
def do_1(p_param)
puts “1 performed – #{p_param}”
enddef do_2
puts “2 performed.”
enddef no_do
puts “No do”
end
end
and here is my aspect class,
class SimpleServiceAdvice
def around_do(p_invoke)
puts “Around { “
output = p_invoke.proceede()
puts “}”
return output
enddef before_do(p_invoke)
puts “Before execution.”
enddef after_do(p_params, p_output)
puts “After execution.”
end
end
my implementation is very straight forward, actually during implementing this stuff, i really felt the strength of meta programming. it is so flexible and so easy that sky is the limit.





Recent Comments