This post originated from an RSS feed registered with Ruby Buzz
by rwdaigle.
Original Post: What's New in Edge Rails: Gem Dependencies
Feed Title: Ryan's Scraps
Feed URL: http://feeds.feedburner.com/RyansScraps
Feed Description: Ryan Daigle's various technically inclined rants along w/ the "What's new in Edge Rails" series.
Rails plugins are great for many reasons, one being that they provide extra functionality without being an external dependency – they’re packaged right there with your application. Until recently, there was no way do programmatically define a Rails applications’ external gem dependencies and we were left with rolling our own gem dependency solutions.
Rails::Initializer.run do |config| # Require the latest version of haml config.gems "haml"# Require a specific version of chronic config.gems "chronic", :version => '0.2.3'# Require a gem from a non-standard repo config.gems "hpricot", :source => "http://code.whytheluckystiff.net"# Require a gem that needs to require a file different than the gem's name# I.e. if you normally load the gem with require 'aws/s3' instead of# require 'aws-s3' then you would need to specify the :lib option config.gems "aws-s3", :lib => "aws/s3"end
So great – when your app loads up it will automatically find and require each of the gems you’ve listed. But what if you’re on a system that doesn’t have all of these gems installed, or you’ve just deployed to a fresh environment? There’s now a rake task that will install all the referenced config.gems on your target system:
rake gems:install
Before running this you can see what gem dependencies your app has by running rake gems.
However, this still doesn’t package these gem dependencies with the app, it only refers to system-dependent gems. If you want to pull these gems into your application source you can do so on a gem by gem basis with the new rake gems:unpack task:
rake gems:unpackGEM=hpricot
This will unpack the gem into the vendor/gems/hpricot-0.5 directory which is automatically searched as part of the config.gems startup.
Your deployment strategy can now choose to automatically install required gems in each target environment or just package gems as part of the application source.