Watching Rack Application Changes with Guard-Rack

Back | rack, grape, rails, ruby, open source | 2/23/2012 |

One of the frequently requested features in Grape to to automatically reload code changes, much like Rails does (#131). This is actually pretty difficult to implement and requires Grape support where an API::reload! method would blow away all of the mounted features, procs, settings, etc.

For bare Rack-mounted applications we can make our life easy with Guard. Introducing guard-rack, which will rackup your Rack application on change. On a decent developer box this just takes a second or two.  The implementation is heavily inspired (aka stolen) from guard-rails.

For a typical Rack application, including one that uses Grape, add guard, guard-bundler (to watch Gemfile changes) and guard-rack to Gemfile.

  1. group :development do
  2.   gem "guard"
  3.   gem "guard-bundler"
  4.   gem "guard-rack"
  5. end

Create a Guardfile. Watch the folders of your application.

  1. guard 'bundler' do
  2.   watch('Gemfile')
  3. end
  4.  
  5. guard 'rack' do
  6.   watch('Gemfile.lock')
  7.   watch(%r{^(config|app|api)/.*})
  8. end

Run with bundle exec guard. Watch rackup happen every time you change files.

I’ve added this to my grape-on-rack demo.