Environment Scripts in merb

The time has come to run a script using cron or another background worker and I find myself reaching for script/runner only…

It’s not there!

No worries…here’s what you do:

#!/usr/bin/env ruby
require 'rubygems'
require 'merb'
require File.dirname(__FILE__) + '/../config/dependencies'
load_paths = []
load_paths.unshift(File.join(Merb.root , '/app/models'))
# add more load paths here, such as
# load_paths.unshift(File.join(Merb.root , '/app/controllers'))
load_paths.each do |path|
  Dir.glob("#{path}/*").each { |m| require m }
end

You can avoid everything from line 4 onward by using Merb::BootLoader.load_application but it will load the routes, which takes longer.

Check in your merb gem (lib/merb/boot_loader.rb) for more help.

Also posted to fr.ivolo.us

One Comment

  1. Reid
    Posted January 29, 2009 at 2:34 am | Permalink

    very nice!

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">