Subscribe Favorite

Aquarium 0.4.2: Aspect-Oriented Programming for Ruby

0

Written on May 27, 2008 by Eka Riyanti

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.

New in V0.4.0: Preliminary support for advising Java classes in JRuby! See the discussion here.

See also the RubyForge project page.

Usage

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::AspectDSL
    before :calls_to => :all_methods, :in_types => /Service$/ do |join_point, object, *args|
      log "Entering: #{join_point.target_type.name}##{join_point.method_name}: object = #{object}, args = #{args}"
    end
    after :calls_to => :all_methods, :in_types => /Service$/ do |join_point, object, *args|
      log "Leaving: #{join_point.target_type.name}##{join_point.method_name}: object = #{object}, args = #{args}"
    end
end

A more succinct implementation of this behavior uses #around advice:

class ServiceTracer
    include Aquarium::Aspects::DSL::AspectsDSL
    around :calls_to => :all_methods, :in_types => /Service$/ do |join_point, object, *args|
      log "Entering: #{join_point.target_type.name}##{join_point.method_name}: object = #{object}, args = #{args}"
      result = join_point.proceed
      log "Leaving: #{join_point.target_type.name}##{join_point.method_name}: object = #{object}, args = #{args}"
      result  # block needs to return the result of the "proceed"!
    end
end

See the Examples and the API section for more details.

Start Here

$ gem install -y aquarium

See the download page for different options or go directly to Rubyforge download page.

more resources: Aquarium.rubyforge home page.

A Server Hard Drive Crash :(

0

Written on May 12, 2008 by Hendy Irawan

Just got a server hard drive crash :-(

We should be back operational soon. In the mean time please bear with us. Thank you. :-)

Fixing RubyGems in Ubuntu Gutsy Installation

0

Written on March 24, 2008 by Hendy Irawan

Ruby-like firetruck

Upgrading to the latest RubyGems in Ubuntu Gutsy is a bit non-straightforward. I’d like to share a quick fix this time. It’s trivial when you know it, but if not, a friend of mine has almost hosed his system just because of this annoying “bug”.

Installing Ruby in Ubuntu is pretty simple:

sudo aptitude install ruby ri irb rdoc rubygems libruby-extras libmysql-ruby ruby1.8-dev

(add other packages as you see fit)

The problem occurs right after you upgrade RubyGems to the latest version:

sudo gem update --system

Then you get something like this:

ceefour@caliva:/usr/bin$ gem
/usr/bin/gem:23: uninitialized constant Gem::GemRunner (NameError)

Logging in and out doesn’t work. The world is coming to an end!

Don’t worry, the world is still running. Check out your /usr/bin folder:

ceefour@caliva:/usr/bin$ ls -la gem*
-rwxr-xr-x 1 root root 701 2007-08-24 12:18 gem
-rwxr-xr-x 1 root root 698 2008-03-20 09:20 gem1.8
-rwxr-xr-x 1 root root  84 2008-03-20 09:20 gemlock
-rwxr-xr-x 1 root root  89 2008-03-20 09:20 gem_mirror
-rwxr-xr-x 1 root root  76 2008-03-20 09:20 gemri
-rwxr-xr-x 1 root root  89 2008-03-20 09:20 gem_server
-rwxr-xr-x 1 root root  86 2008-03-20 09:20 gemwhich

So, there is some mismatch between gem and gem1.8. The latter being the newer/correct version.

Simply remove the “gem” one and replace (or link) it to the “gem1.8″ one:

ceefour@caliva:/usr/bin$ sudo rm gem
ceefour@caliva:/usr/bin$ sudo ln -s gem1.8 gem

Now:

ceefour@caliva:/usr/bin$ gem -v
1.0.1

Presto! We’re back in business. :-)

Interesting RubyGems articles:

Ruby Quick Reference

1

Written on March 22, 2008 by Eka Riyanti

While using Ruby for your projects, you may need some references.

These are some references that might help you in using Ruby:

  • Language

General Syntax Rules

  • Comments start with a pound/sharp (#) character and go to EOL.
  • Ruby programs are sequence of expressions.
  • Each expression is delimited by semicolons(;) or newlines unless obviously incomplete (e.g. trailing ‘+’).
  • Backslashes at the end of line does not terminate expression.

Reserved words

alias   and     BEGIN   begin   break   case    class   def     defined
do      else    elsif   END     end     ensure  false   for     if
in      module  next    nil     not     or      redo    rescue  retry
return  self    super   then    true    undef   unless  until   when
while   yield

Type

Basic types are numbers, strings, ranges, regexen, symbols, arrays, and hashes. Also included are files because they are used so often.

Variables

$global_variable
@@class_variable
@instance_variable
[OtherClass::]CONSTANT
local_variable
  • Standard Library

Ruby comes with an extensive library of classes and modules. Some are built-in, and some are part of the standard library. You can distinguish the two by the fact that the built-in classes are in fact, built-in. There are no dot-rb files for them.

Built-in Library

Class Hierarchy:

Object

  • Hash
  • Symbol
  • IO
    • File
  • Continuation
  • File::Stat
  • Data
  • NilClass
  • Exception (see tree above)
  • Array
  • Proc
  • String

Standard Library

The essentials:

    • benchmark.rb a simple benchmarking utility
    • cgi-lib.rb decode CGI data - simpler than cgi.rb
    • cgi.rb CGI interaction
    • date.rb date object (compatible)
    • debug.rb ruby debugger
    • delegate.rb delegate messages to other object
    • English.rb access global variables by english names
    • fileutils.rb file utility methods for copying, moving, removing, etc.

For further details visit Ruby Quick Reference page at ZenSpider.

Migrating Subversion repositories using SVK

1

Written on March 20, 2008 by Hendy Irawan

Recently I got a task which involves moving, or let’s say copying, an entire Subversion repository with history to another server. Problem is, I didn’t have access to the server itself, which means I couldn’t do a regular “svnadmin dump”.

SVK comes to the rescue!

Firefighter in style!

To make it work, first of all you need to install SVK. In Ubuntu it goes like this:

sudo aptitude install svk

When you first run svk it’ll ask you to create a local depot, you can simply agree to its suggestion.

Now we mirror both of the Subversion repositories we’re trying to import and export from and to. Note that you need to create the destination repository first.

svk mirror //source http://svn.source.com/project1/
svk mirror //dest http://svn.dest.com/newproject/

A bit of niceness with this method instead of a regular svn dump/load procedure is that:

  • you can import to a different folder/subfolder instead of the root folder
  • you can do a partial export (subfolder of project repository)

Before doing the actual migration process, let’s sync these mirrors first:

svk sync //source
svk sync //dest

And then you’ll do the real thing. But we can simulate it first by using “–check-only”, kinda’ like when you simulate a DVD burning session before actually writing it.

svk smerge //source //dest --incremental --log --sync --verbatim --track-rename --baseless --check-only

There are several switches that I used above, feel free to use them only as needed:

  • -I [–incremental]: apply each change individually
  • -l [–log]: use logs of merged revisions as commit message
  • -B [–baseless]: use the earliest revision as the merge point
  • -s [–sync]: synchronize mirrored sources before update
  • –verbatim: verbatim merge log without indents and header
  • –track-rename: track changes made to renamed node
  • -C [–check-only]: try operation but make no changes

After you’re ready, redo the above command without “–check-only”:

svk smerge //source //dest --incremental --log --sync --verbatim --track-rename --baseless

Simply wait several minutes–or possibly hours (or days!) if your project is sufficiently large–for SVK to do its job for you!

Are there disadvantages of using SVK to a “genuine” SVN dump/load? Sure, among them is that the original author names are lost.

Good luck!

Related articles and resources: