Subscribe Favorite

Aquarium 0.4.2: Aspect-Oriented Programming for Ruby

Comments

Written on May 27, 2008 by ceefour

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 :(

Comments

Written on May 12, 2008 by ceefour

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

Comments

Written on March 24, 2008 by ceefour

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

Comments

Written on March 22, 2008 by ceefour

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

Comments

Written on March 20, 2008 by ceefour

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:

Using Regular Expressions with Ruby

Comments

Written on March 18, 2008 by ceefour

Ruby is a high level, object-oriented open source scripting language. It has excellent support for regular expressions as a language feature.

In Ruby, a regular expression is written in the form of /pattern/modifiers where “pattern” is the regular expression itself, and “modifiers” are a series of characters indicating various options. The “modifiers” part is optional. This syntax is borrowed from Perl.

Ruby supports the following modifiers:

  • /i makes the regex match case insensitive.
  • /m makes the dot match newlines. Ruby indeed uses /m, whereas Perl and many other programming languages use /s for “dot matches newlines”.
  • /x tells Ruby to ignore whitespace between regex tokens.
  • /o causes any #{…} substitutions in a particular regex literal to be performed just once, the first time it is evaluated. Otherwise, the substitutions will be performed every time the literal generates a Regexp object.

You can combine multiple modifiers by stringing them together as in /regex/is.

In Ruby, the caret and dollar always match before and after newlines. Ruby does not have a modifier to change this. Use \A and \Z to match at the start or the end of the string.

Since forward slashes delimit the regular expression, any forward slashes that appear in the regex need to be escaped. E.g. the regex 1/2 is written as /1\/2/ in Ruby.

Read more on: Regular-Expressions.info home page.

Advanced Rails: Go to the next level with Rails

Comments

Written on March 16, 2008 by ceefour

Advanced Rails

Advanced Rails offers you an in-depth look at techniques for dealing with databases, security, performance, web services and much more.

O’Reilly Media, Inc. published an intermediate-to-expert Rails book, authored by Brad Ediger:

Chapters in this book help you understand not only the tricks and techniques used within the Rails framework itself, but also how to make use of ideas borrowed from other programming paradigms. Advanced Rails pays particular attention to building applications that scale — whether “scale” means handling more users, or working with a bigger and more complex database.

You’ll find plenty of examples and code samples that explain:

  • Aspects of Ruby that are often confusing or misunderstood
  • Metaprogramming
  • How to develop Rails plug-ins Different database management systems
  • Advanced database features, including triggers, rules, and stored procedures
  • How to connect to multiple databases
  • When to use the Active Support library for generic, reusable functions Security principles for web application design, and security issues endemic to the Web When and when not to optimize performance
  • Why version control and issue tracking systems are essential to any large or long-lived Rails project
  • Advanced Rails also gives you a look at REST for developing web services, ways to incorporate and extend Rails, how to use internationalization, and many other topics.

Advanced Rails is an essential resource for improving your skills on Rails through advanced techniques.

Morph Application Platform Simplifies Ruby on Rails Development

Comments

Written on February 16, 2008 by ceefour

Morph logo

Morph Labs is currently beta-testing their next-generation solution in application deployment, delivery, and management, the Morph Application Platform.

Acquiring hardware and configuring software to support web apps are things of the past. Morph Labs brings you the next-generation solution in application deployment, delivery, and management. Reduce your time to market and lower your startup costs no matter if you are an ISV, a developer or a business.

About Morph Labs

Morph Labs Inc. www.morphexchange.com is a Philippine-based Web 2.0 technology company focused on providing innovative technologies and applications to support Software as a Service (SaaS) globally.

Morph offers independent software vendors (ISVs), IT consulting organizations, application developers and entrepreneurs the quickest and simplest route to SaaS-enablement. Morph simplifies deployment and management of software as a service with an elastic Web 2.0 delivery and management solution — the Morph Application Platform.

Morph Application Platform, powered by Amazon EC2 grid computing technology, combines the elasticity in delivering and managing Web 2.0 applications and the simplicity of deploying software as a service (SaaS).

With no hardware to buy and no software to install and configure, Morph allows developers to easily grow or shrink their application environment on demand without disruption to the application. The Morph Application Platform is offered through a subscription to a Morph AppSpace, which is an instance of the Morph Application Platform.

Beyond delivery platforms and managed services, Morph will build simplified software applications (that run on our platform) that leverage open source technologies for small and medium businesses.

Morph’s first on-demand application is Morph helpME (released October 2007), provided via the software as a service model and runs on top of the Morph Application Platform. It’s an on-demand help and training system that enables sharing of knowledge while reducing overall cost and technical burden.

Morph helpME is a Ruby on Rails application running on the Morph Application Platform. It’s automatically formats, structures and creates menus, enabling faster deployment of new content.

If you are Ruby on Rails developers, bring your on-demand application to life with the Morph Application and leave the details to us!

Visit Morph home page to find out more!

Rearranging Stuff Notice, `Experience Report’ Coming Up Soon!

Comments

Written on February 10, 2008 by ceefour

I’d like to express a warm notice that AdaRuby.com might be down intermittently as we’ll be having major rearrangement and server upgrades of our hosting facilities in the coming days.

The upside is, when it’s done (and oh YES it will be done!), I’ll be providing you interesting information on the stuff that we’re doing, especially our experience regarding the hosting services that we have been using all this time.

Looking forward to hearing you share your experience as well!

Ruby on Rails Web Hosting for Canadian Sites

Comments

Written on February 5, 2008 by ceefour

Canadian Web Hosting provides shared, VPS, and dedicated web hosting for Canadian sites. They operate from a 1st Class Colocation facility located at Harbour Center in downtown Vancouver, BC, Canada. The advantage to international (i.e. non-Canadian) hosting services are obvious: they are much faster to access from Canada (with an added bonus that you pay in your native Canadian Dollars currency! ;) Hence, if your customers and/or your business is based on Canada, hosting your Rails site with them might be a perfect fit.

The shared hosting plans offered by Canadian Web Hosting starts from the tight-budget CAD$3.95/mo (2 year prepayment). Ruby on Rails support is provided with the CA Pro plan which is only CAD$15.95/month (2 year prepayment), with 3500 GB bandwidth, and 300 GB space. All the “standard” web hosting stuff, like PHP 4/5, MySQL, are available, including support for advanced features such as PostgreSQL, SSH, ColdFusion MX 8 (seriously), ImageMagick, and more.

There’s no initial setup fee (hosting account setup is free!) and they provide 30-day money-back guarantee. So you can try their services and features without risking anything, really.

VPS plans are also available starting from an affordable CAD$25.95/month (2 year prepaid), that some of you demanding more control would prefer. They also provide Intel Dual Core and Quad-Core Xeon-powered dedicated servers (with Red Hat/CentOS operating system), which would be more cost-effective for those who have larger number of clients or require better stability and guaranteed resources.

This is a sponsored post.