Directory Conventions for Rack Middleware RubyGems

May 13th, 2010 by John Trupiano

I just wanted to make a quick note about directory conventions for rack middleware gems. For all gems you should follow the convention of housing all of your code inside a single file and directory of the same name as your gem within lib, e.g.


$> ls -l ~/projects/timecop/lib
drwxr-xr-x 5 john staff 170 Jan 14 20:31 timecop
-rw-r--r-- 1 john staff 82 Jan 14 20:31 timecop.rb

The reason for this is related to how RubyGems hijacks Ruby’s require method. When a gem is activated its lib/ folder is added to the load path. This means that anything inside that directory is now accessible via the require method. In order to avoid file naming collisions across gems, you must name these exactly the same as your gem. (see slides for I Don’t Trust Your Code for a more complete discussions of this)

However, this is slightly different with rack gems. The convention for naming rack middleware is by using a dash, e.g. rack-rewrite. The convention for requiring rack middleware though is to replace that dash with a slash, e.g. require 'rack/rewrite'.

The convention I’ve adopted for structuring rack middleware within a gem is to include a file by the same name as the gem and a rack directory in lib/, and then to include the second part of the middleware name as a subdirectory under that.


~/projects/rack-rewrite (master) $> ls -l lib/
total 8
drwxr-xr-x 4 john staff 136 Apr 17 18:02 rack
-rw-r--r--@ 1 john staff 22 Apr 17 18:02 rack-rewrite.rb

~/projects/rack-rewrite (master) $> ls -l lib/rack
total 8
drwxr-xr-x 3 john staff 102 May 13 11:09 rewrite
-rw-r--r--@ 1 john staff 827 Apr 17 18:02 rewrite.rb

This allows my users to use either require 'rack-rewrite' or require 'rack/rewrite'.

Rack::Rewrite 1.0.0 Released

May 13th, 2010 by John Trupiano

Rack::Rewrite 0.2.1 Released

January 6th, 2010 by John Trupiano

Timecop 0.3.4 Released

December 7th, 2009 by John Trupiano

Rack::Rewrite + Google Analytics Makes Site Transitions Seamless

November 24th, 2009 by John Trupiano

Rack::Rewrite for Site Maintenance and Downtime

November 16th, 2009 by John Trupiano

Timecop 0.3.0 Released

September 20th, 2009 by John Trupiano

Shell Script to Upgrade Ruby Enterprise Edition while Maintaining Directory Naming Sanity

June 10th, 2009 by John Trupiano

Including external .rake files in your project’s Rakefile — keep your rake tasks organized!

May 26th, 2009 by Glenn Gentzke

Find the Unique Sessions for a Rails Application

May 4th, 2009 by Nick Gauthier