Today I was discussing with my friend Juampi Garcia who is writing a nice sample application that leverages the waz-storage gem and some other interesting things. While we were talking about his application, he asked me for some way of overriding the default connection for performing a bunch of operations as he needed it for his application.

I started thinking and looking around some samples from DataMapper and other ORM frameworks that have the ability to override the current context by calling a method or specifying connection name. I looked deeply into it, but end up thinking that it will be better to have a little stack of connections in order to handle different contexts.

I end up writing a new method to WAZ::Storage::Base that enables you to scope your code to some specific context and once that context is left you get back to the default connection.

Consider the following sample in order to better understand what I did

  WAZ::Storage::Base.establish_connection!(:account_name => 'name', :access_key => 'key')

  # any operation performed here will be on the default context expressed above. e.g.
  container = WAZ::Blobs::Container.find('my-container')

  # So, now let's suppose that I need to perform an operation on another context,
  # but those are just a few, and very scoped that isn't worth switching the whole
  # context to that. Leveraging the new method, I'll just do:
  WAZ::Storage::Base.establish_connection(:account_name => 'another', :access_key => 'key') do
     # any operation that I perform here, will be on this new context. e.g.
     another_container = WAZ::Blobs::Container.find('my-container')
     # Note that the operation above is performing a look-up of the container on another
     # storage account.
  end

  # Now that I've left the block, I'm back to my original context.

This new feature has been added to project at github.com/johnnyhalife/waz-storage and the new documentation is also available at waz-storage.heroku.com.

You can upgrade your gem, in order to have this new feature or just using the bleeding edge by cloning the repository on github (git://github.com/johnnyhalife/waz-storage.git)

Another small change

If you are (or tried) using waz-storage on Heroku.com without rails, you will realize that some exceptions about ‘String.start_with?’ being undefined may appear. I’ve added an snippet to implement this method, on the rails way, when loading the waz gem (if it was previously undefined).

thanks,
~johnny

Leave a Reply