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}"
end
def do_2
puts "2 performed."
end
def 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
end
def before_do(p_invoke)
puts "Before execution."
end
def 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.
here is the attached source code.
<http://hasan.we4tech.com/wp-content/uploads/2007/11/aop_test.zip>
much better and complete aop implementation in
ruby<http://aspectr.sourceforge.net/>
--
----------------------------------------------------
nhm tanveer hossain khan (hasan)
http://hasan.we4tech.com
----------------------------------------------------
mobile: +880 1713 090 511
----------------------------------------------------
"work for fun"
[Non-text portions of this message have been removed]