- 更新日: 2014年2月6日
 - Rails
 
Rails で Rake タスク作成と rake コマンド実行の色々な方法
スポンサーリンク
    
    
    
    
  独自の Rake タスクを作成していて、書き方が色々あることを知ったのでまとめました。rails と rake を bundler でインストール済みと前提します。
【お知らせ】 英単語を画像イメージで楽に暗記できる辞書サイトを作りました。英語学習中の方は、ぜひご利用ください!
スポンサーリンク
— 環境 —
Rails 4.1
Rake 10.3.2
Rake タスクの作成、確認、実行
Rake タスク用のファイルを作成するコマンドを実行。
| 
					 1 2 3  | 
						$ bundle exec rails genarate task greeting  | 
					
lib/tasks/greeting.rake が作成されるので、そのファイルの中に rake タスクを記述。
lib/tasks/greeting.rake
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13  | 
						namespace :greeting do   desc "Say hello world"   task :hello do     puts "hello world"   end   # モデルにアクセスする場合は :environment を指定   desc "Say hello to a user"   task :hello_user => :environment do     user = User.find(:first)     puts "hello #{user.name}"   end end  | 
					
作った Rake タスクを確認。ここでエラーが出たら書き間違えているので修正します。
| 
					 1 2 3 4 5  | 
						$ bundle exec rake -vT rake greeting:hello               # Say hello world rake greeting:hello_user          # Say hello to a user  | 
					
hello タスクを実行。
| 
					 1 2 3 4  | 
						$ bundle exec rake greeting:hello hello world  | 
					
namespace を入れ子にする
namespace は入れ子にできます。
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21  | 
						namespace :greeting do   namespace :morning do     desc "Say good morning"     task :morning do       puts "good morning"     end     desc "Say guten morgen"     task :morgen do       puts "guten morgen"     end   end   namespace :afternoon do     desc "Say hello world"     task :hello do       puts "hello world"     end   end end  | 
					
確認。
| 
					 1 2 3 4 5 6  | 
						$ bundle exec rake -vT rake greeting:afternoon:hello     # Say hello world rake greeting:morning:morgen      # Say guten morgen rake greeting:morning:morning     # Say good morning  | 
					
morgen タスクを実行。
| 
					 1 2 3 4  | 
						$ bundle exec rake greeting:morning:morgen guten morgen  | 
					
データベースの環境を指定する
task に => で他のタスクを指定してやると、その環境でタスクを実行することができます。
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27  | 
						namespace :db do   # database.yml からロードする場合   desc "Load database.yml"   task :configuration => :environment do     @config = YAML.load_file('config/databases.yml')[DATABASE_ENV]   end   # database.yml からロードした @config を establish_connection に渡してDB接続   # @config なしで establish_connection に直接DB接続情報の引数を渡しても良い   desc "Connect the database"   task :configure_connection => :configuration do     ActiveRecord::Base.establish_connection @config   end   # database.yml から現在の DATABASE_ENV でDB作成、configure_connection を指定する   desc 'Create the database'   task :create => :configure_connection do     # create_database は別に実装が必要     create_database @config   end   # DB を削除   desc 'Drops the database'   task :drop => :configure_connection do     ActiveRecord::Base.connection.drop_database @config['database']   end end  | 
					
これは便利です。
Rake コマンドに引数を渡す
rake コマンド実行時に引数を渡せる、Rake タスクの書き方があります。
| 
					 1 2 3 4 5 6 7 8 9 10 11  | 
						namespace :greeting do   task :greet, ['greet', 'user'] do |task, args|     puts "#{args.greet} #{args.user}"   end   # または以下   task :another_greet, 'greet', 'user'   task :another_greet do |task, args|     puts "#{args.greet} #{args.user}"   end end  | 
					
実行。
| 
					 1 2 3 4  | 
						$ bundle exec rake "greeting:greet[hello, taka]" hello taka  | 
					
| 
					 1 2 3 4  | 
						$ bundle exec rake "greeting:another_greet[guten tag, taka]" guten tag taka  | 
					
rake コマンド実行時に引数を渡す場合は、ブラケット([])で引数渡しということでちょっと分かり辛い。また rake タスクを”(ダブルクォート)で囲まないといけない。
- – 参考リンク –
 - Rails – Rakeタスクをつくる – Qiita [キータ]
 - Exposing Gotchas: ActiveRecord migrations without Rails
 - rake でのコマンドライン引数の扱い – 君の瞳はまるでルビー – Ruby 関連まとめサイト
 - transitive.info – Rake コマンドの使い方
 
スポンサーリンク
パーフェクト Ruby on Rails は、最近読んだ Rails 本の中では一番役に立った本です。Chef や Capistrano など Rails と共によく使用される技術にも触れてあります。Ruby on Rails 4 アプリケーションプログラミングは、入門的な内容で Rails の機能全体を網羅されています。
      
      
    
>> 次の記事 : Rails4 で lib ディレクトリの自作ライブラリを autoload
- Rails の関連記事
 - RailsでMySQLパーティショニングのマイグレーション
 - Rails ActiveRecordでdatetime型カラムのGROUP BY集計にタイムゾーンを考慮する
 - RailsプラグインGemの作成方法、RSpecテストまで含めたrails pluginの作り方
 - RailsでAMPに対応するgemをリリースしました
 - Railsでrequest.urlとrequest.original_urlの違い
 - Railsでwheneverによるcronバッチ処理
 - Google AnalyticsのRails Turbolinks対応
 - Railsアプリにソーシャル・シェアボタンを簡単設置
 - Rails監視ツール用にErrbitをHerokuで運用
 - Facebook APIバージョンのアップグレード手順(Rails OmniAuth)
 

画像付き英語辞書 Imagict | 英単語をイメージで暗記





Leave Your Message!