views:

129

answers:

1

I have following code in my deploy.rb

namespace :app do
  desc "copies the configuration frile from ~/shared/config/*.yml to ~/config"
  task :copy_config_files,:roles => :app do
    run "cp -fv #{deploy_to}/shared/config/hoptoad.rb #{release_path}/config/initializers"
    run "cp -fv #{deploy_to}/shared/config/app_config.yml #{release_path}/config/app_config.yml"
  end
end

I thought it would be a good idea to keep my deploy.rb file clean and I attempted to move above code to capistrano_utilities.rb under config. I am using Rails application. And I added following line of code to deploy.rb

require File.expand_path(File.dirname(__FILE__) + "/../lib/capistrano_utilities")

Now I am getting following error.

undefined method `namespace' for main:Object (NoMethodError)

The value of self in the deploy.rb is Capistrano::Configuration . While the value of self in capistrano_utilities is Main. So I understand why I am getting namespace method error. What is the fix for this problem?

A: 

In your config/deploy.rb, try load instead of require. Also, capistrano already runs as if you're at the RAILS_ROOT, so there's no need to use __FILE__:

load "lib/capistrano_utilities"

In a capistrano config file, load is redefined to load another configuration file into the current configuration. When passing a path to it, it actually calls load_from_file (a private method defined by capistrano) that just reads the file from disk and instance_eval's it.

Ryan McGeary
I though the only difference between require and load was that one of them rereads the file into memory multiple times and the other one reads only once. Can you explain why load works and require does not.
Roger
Roger, more details added about capistrano's specifics.
Ryan McGeary
thanks Ryan. Another wonderful magic from ruby ecosystem.
Roger