simple AOP implementation in ruby

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.

much better and complete aop implementation in ruby

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

my tweets

Error: Twitter did not respond. Please wait a few minutes and refresh this page.

November 2007
S S M T W T F
« Oct   Dec »
 12
3456789
10111213141516
17181920212223
24252627282930

Flickr Photos

RSS my shared links on google reader

  • An error has occurred; the feed is probably down. Try again later.
%d bloggers like this: