Module: Woli::Cli::DateParser

Defined in:
lib/woli/cli/date_parser.rb

Class Method Summary collapse

Class Method Details

.parse_date(fuzzy_date) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/woli/cli/date_parser.rb', line 24

def self.parse_date(fuzzy_date)
  case fuzzy_date
  when 'today', 't'
    Date.today
  when 'yesterday', 'y'
    Date.today - 1
  when /\A\^(?<days_ago>\d+)/
    Date.today - Integer($~[:days_ago])
  when /\A(?<day>\d+)([-\.\/](?<month>\d+)([-\.\/](?<year>\d+))?)?\Z/
    today = Date.today
    year = $~[:year] ? Integer($~[:year]) : today.year
    month = $~[:month] ? Integer($~[:month]) : today.month
    day = $~[:day] ? Integer($~[:day]) : today.day
    Date.new(year, month, day)
  else
    raise Thor::MalformattedArgumentError, "'#{fuzzy_date}' is not a valid way to specify a date."
  end
end

.parse_date_long_descObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/woli/cli/date_parser.rb', line 4

def self.parse_date_long_desc
  <<-END
    Specifying DAY:

    1) Implicitly: If no date is given, 'today' is assumed.

    2) Via a keyword: woli edit DAYNAME
    (Where DAYNAME can be:
    'today' or 't';
    'yesterday' or 'y')

    3) Via a date: woli edit DD[-MM[-YYYY]]
    (Use hyphens, dots or slashes to separate day/month/year.
    If year or month is not given, current year / current month is assumed.)

    4) Via days ago: woli edit ^DAYS
    (Where DAYS is a number of days ago. ^1 is yesterday, ^7 is a week ago etc.)
  END
end