Overview
Aquarium is a framework that implements Aspect-Oriented Programming (AOP) for Ruby. The premise of AOP is that some concerns in an application will cut across the natural object boundaries of the problem domain. Rather than scatter duplicated code in each object to handle the cross-cutting concern, AOP modularizes the specification of which execution points are affected (called join points) and the actions that should be invoked at those points.
For example, persistence of “model” objects is a cross-cutting concern, in the sense that the desired persistence approach (database, flat files, replication, etc.) is independent of the domain logic represented by the model. So, why should the model code have any persistence logic? Instead, capture the details of mapping the domain to the persistence approach in separate “components” and programmatically or declaratively modify the model objects to synchronize state changes with the persistent memory of the state.
See also the RubyForge project page.
How to Use Aquarium
Aquarium provides a Domain Specific Language (DSL) with which you can express “aspectual” system behaviour in a modular way, i.e., using a succinct language and without repeating yourself all over your code base!
Imagine you want to trace all invocations of the public, instance methods in all classes whose names end with “Service”. Here’s how you can implement that behavior in Aquarium:
class ServiceTracer
include Aquarium::Aspects::DSL::AspectsDSL
before :types => /Service$/, :methods => :all do |execution_point, *args|
log "Entering: #{execution_point.target_type.name}##{execution_point.method_name}: args = #{args.inspect}"
end
after :types => /Service$/, :methods => :all do |execution_point, *args|
log "Leaving: #{execution_point.target_type.name}##{execution_point.method_name}: args = #{args.inspect}"
end
end
The #before
advice adds behavior that is invoked before each method is invoked, in this case, it logs a message with the name of the executing class and method, followed by the list of arguments passed to the method.
The #after
advice adds similar behavior the is invoked after each method is invoked.
A more succinct implementation of this behavior uses #around
advice:
class ServiceTracer
include Aquarium::Aspects::DSL::AspectsDSL
around :types => /Service$/, :methods => :all do |execution_point, *args|
log "Entering: #{execution_point.target_type.name}##{execution_point.method_name}: args = #{args.inspect}"
result = execution_point.proceed
log "Leaving: #{execution_point.target_type.name}##{execution_point.method_name}: args = #{args.inspect}"
result # block needs to return the result of the "proceed"!
end
end
The special method #proceed
invokes the advised method, passing it the args list (by default). For #around
advice, you must call #proceed
unless you specifically don’t want the original method called!
See the Examples and the API section for more details.
Start Here
$ gem install aquarium
See the download page for different options or go directly to Rubyforge download page.