This post originated from an RSS feed registered with Ruby Buzz
by Gabriel Horner.
Original Post: LocalGem Loads Your Current Code Now!
Feed Title: Tagaholic
Feed URL: http://feeds2.feedburner.com/tagaholic
Feed Description: My ruby/rails/knowledge management thoughts
The other day while actively developing a gem, I got tired of rake reinstalling it to test its effect in irb with some other gems. I wanted to use the edge version of my gem, version now. So I hacked up $LOAD_PATH:
$LOAD_PATH.unshift '/my/path/to/gem/lib'
This effectively made my live gem directory a gem, as far as requiring files is concerned. However once I did this in irb a couple of times and even in a file, it started to feel dirty and repetitious. Why couldn't I just require my local gem just like I do when it's installed in my gem directories? So I hacked on it later and out came LocalGem.
LocalGem simply maps names to file paths and when given a name, adds its corresponding path to $LOAD_PATH like above. You can setup the name-path mapping via a yaml config file or a method call.
require 'local_gem' LocalGem.setup_config do |c| c.gems = {'mygem'=>'/path/to/mygem', 'anothergem'=>'/path/to/anothergem'} end
LocalGem then provides two methods, local_gem() and local_require() to act like gem() and require() but with knowledge of your local gems. You can use these two methods in one of three ways, depending on how much you want LocalGem to invade your namespace: