LoginSignup
9
3

More than 5 years have passed since last update.

2017年まであと何日?rake taskで書いてみた

Last updated at Posted at 2016-12-19

Shinosaka.rb Advent Calendar 21日目の記事です。
今回は、Ruby on Railsの中で使われる Rake taskについて書いてみました!

年末ということで、
「xxxx年まであとxx日xx時間xx分!」
と教えてくれるtaskを書いてみました。

Rake taskとは

Rubyの便利ツールRake。処理内容を定義できるビルドツールです。

みなさん、普段開発していて 繰り返し実行したい処理 ってよくありませんか?
それをコマンド一発で叩けたら嬉しい!
それを実現してくれるのが、rake taskです。

詳しい概要はこちら。個人的にわかりやすかった

今回は、rake taskの実行方法、引数のやり方を
例を用いて解説したいと思います。

つくってみよう

まずはtaskをつくる

rails g task countdown
create  lib/tasks/countdown.rake

作られました。

rake -T

で実行の仕方がわかります。

rake countdown:run

いろいろパターンを作ってみました。
時間の計算の仕方は、秒を時間表示へ変換するを参考にさせていただきました。ありがとうございます!

1.実行したら「2017年まであと 何日何時間何分!」と表示する

rake countdown:run

namespace :countdown do
  desc "カウントダウンします"
  task run: :environment do
    year = 2017
    sec = (Time.zone.parse("#{year}-01-01 00:00:00") - Time.now - 1.day)
    day, sec_r = sec.divmod(86400)
    p (Time.parse("1/1") + sec_r).strftime("#{year}年まであと #{day}日%H時間%M分!")
  end
end

2.引数で「xxxx年まで」を指定する ver.1

rake countdown:run[year]

namespace :countdown do
  desc "カウントダウンします"
  task :run, ['year'] => :environment do |t, args|

    year = 2017
    unless args[:year].blank?
      year = args[:year].to_i
    end

    sec = (Time.zone.parse("#{year}-01-01 00:00:00") - Time.now - 1.day)
    day, sec_r = sec.divmod(86400)
    p (Time.parse("1/1") + sec_r).strftime("#{year}年まであと #{day}日%H時間%M分!")
  end
end

3.引数で「xxxx年まで」を指定する ver.2

ENV に変えただけですが..

rake countdown:run year=xxxx

namespace :countdown do
  desc "カウントダウンします"
    task run: :environment do

    year = 2017
    unless ENV['year'].blank?
      year = ENV['year'].to_i
    end

    sec = (Time.zone.parse("#{year}-01-01 00:00:00") - Time.now - 1.day)
    day, sec_r = sec.divmod(86400)
    p (Time.parse("1/1") + sec_r).strftime("#{year}年まであと #{day}日%H時間%M分!")
  end
end

4.yes,no判定をつけてみました

bundle exec 'rake countdown:run[2019]'

namespace :countdown do
  desc "カウントダウンします"
  task :run, ['year'] => :environment do |t, args|

    year = 2017
    unless args[:year].blank?
      year = args[:year].to_i
    end

    STDOUT.puts "Calculate from #{Time.now.strftime('%Y/%m/%d')} to #{year}/1/1. Is it OK? (y/N)"
    if STDIN.gets.strip == 'y'
      show_future_days(year)
    end
  end
end

def show_future_days(year)
  sec = (Time.zone.parse("#{year}-01-01 00:00:00") - Time.now - 1.day)
  day, sec_r = sec.divmod(86400)
  p (Time.parse("1/1") + sec_r).strftime("#{year}年まであと #{day}日%H時間%M分!")
end

実行すると、

bundle exec 'rake countdown:run[2018]'
Calculate from 2016/12/19 to 2018/1/1. Is it OK? (y/N)
y
"2018年まであと 376日16時間04分!"

ちなみに

今は、
year = 2017 で固定していますが

来年以降は
year = Date.current.next_year.year

としたら使えます(^o^)

まとめ

rake taskは繰り返し実行したい処理をまとめるのにとっても便利です。

2017年まであと何日でしょう?
よいおとしを〜(゜∀゜)

9
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
3