2013-02-23 74 views
1

簡單的問題;所以在我的Capistrano的腳本deploy.rb看起來像這樣,在那裏我可以使用捕獲功能輕鬆:如何從外部靜態類訪問Capistrano方法?

namespace :mycompany do 
    def some_function() 
     mylog = capture("some_command") 
    end 

    desc <<-DESC 
     some task description 
    DESC 
    task :some_task do 
     mylog = capture("some_command") 
    end 
end 

不過,如果我使用的方法在一個類中,像這樣:

namespace :mycompany do 
    class SomeClass 
     def self.some_static_method() 
      mylog = capture("some_command") 
     end 
    end 
end 

它慘不忍睹:

/config/deploy.rb:120:in `some_static_method': undefined method `capture' for #<Class:0x000000026234f8>::SomeClass (NameError) 

如何做到這一點呢?這種方法似乎並不爲靜態:(這裏面這裏(Capistrano的來源):

module Capistrano 
    class Configuration 
     module Actions 
      module Inspect 

       # Executes the given command on the first server targetted by the 
       # current task, collects it's stdout into a string, and returns the 
       # string. The command is invoked via #invoke_command. 
       def capture(command, options={}) 
        ... 

回答

1

有人建議使用包括這樣:

namespace :mycompany do 
    class SomeClass 
     include Capistrano::Configuration::Actions::Inspect 

     def self.some_static_method() 
      mylog = self.new.capture("some_command") 
     end 
    end 
end 

但是,這裏面失敗,錯誤捕獲:

/var/lib/gems/1.9.1/gems/capistrano-2.14.2/lib/capistrano/configuration/actions/inspect.rb:34:in `capture': undefined local variable or method `sudo' for #<#<Class:0x00000001cbb8e8>::SomeClass:0x000000027034e0> (NameError) 

所以我壓根兒是通過實例作爲一個參數(哈克,但它的工作原理)

namespace :mycompany do 
    def some_function() 
     SomeClass.some_static_method(self) 
    end 

    class SomeClass 
     def self.some_static_method(capistrano) 
      mylog = capistrano.capture("some_command") 
     end 
    end 
end 

FML