22

Jan

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.