2013-04-26 6 views
5

をタスクを呼び出します。どのように私は引数でこのタスクを実行できますが、コマンドラインからは実行できません。次の実装は素晴らしいでしょう。カピストラーノ:私は複数のユーザーのパスワードを変更するために、これを使用したい私はこのコード</p> <pre class="lang-rb prettyprint-override"><code>namespace :mysql do task :change_password do run "mysqladmin -u #{user} -p#{old} password #{new}" end end </code></pre> <p>を持っている(内部)の引数で

task :change_passwords do 
    mysql.change_password('user1', 'oldpass', 'newpass'); 
    mysql.change_password('user2', 'oldpass', 'newpass'); 
    mysql.change_password('user3', 'oldpass', 'newpass'); 
end 

残念ながら、これは機能しません。この作業を行う1つの方法は、タスクを実行する前に毎回グローバル変数を設定することですが、それは洗練された解決策ではありません。

これを実装するより良い方法を教えてもらえますか?私はルビーを知らない

PS、私はちょうど

答えて

1

タスクは、任意の引数を取らない展開にカピストラーノを使用しています。ただし、あなたのCapfileに任意のルビコードを含めることができます。だから、実行を呼び出すメソッドを定義することができます

def mysql_change_password(user, old, new) 
    run "mysqladmin -u #{user} -p#{old} password #{new}" 
end 

また、イテレータを使用することができます。

task :change_passwords do 
    [ 
    ['user1', 'oldpass', 'newpass'], 
    ['user2', 'oldpass', 'newpass'], 
    ['user3', 'oldpass', 'newpass'] 
    ].each do |ary| 
    mysql_change_password(*ary) 
    end 
end 
関連する問題

 関連する問題