Class: MicroManager::CLI::RelativeDate

Inherits:
Object
  • Object
show all
Defined in:
lib/cli/relative_date.rb

Class Method Summary collapse

Class Method Details

.date_offset(num, modifier) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/cli/relative_date.rb', line 21

def self.date_offset(num, modifier)
  modifier_days = {
    "day" => 1,
    "week" => 7,
    "month" => 30
  }

  num.to_i * modifier_days[modifier]
end

.parse(string, *args) ⇒ Object



6
7
8
9
10
11
# File 'lib/cli/relative_date.rb', line 6

def self.parse(string, *args)
  date = parse_relative_date(string)
  return date if date

  Date.parse(string, *args)
end

.parse_relative_date(string) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/cli/relative_date.rb', line 13

def self.parse_relative_date(string)
  regex = /(?<n>\d+)-(?<modifier>day|week|month)/
  matches = string.match(regex)
  return unless matches

  Date.today + date_offset(matches[:n], matches[:modifier])
end