Class: Achoo::UI::DateChooser

Inherits:
Object
  • Object
show all
Defined in:
lib/achoo/ui/date_chooser.rb

Direct Known Subclasses

OptionallyRangedDateChooser

Constant Summary collapse

PROMPT =
"Date ([today] | ?)"
FORMAT =
"        today | (+|-)n | [[[YY]YY]-[M]M]-[D]D"

Instance Method Summary collapse

Instance Method Details

#chooseObject



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/achoo/ui/date_chooser.rb', line 12

def choose
  loop do
    answer = Term::ask PROMPT 
    begin
      date = handle_answer(answer)
      return date if date
    rescue ArgumentError => e
      puts e
    end
  end
end

#parse_date(date_str, base = Date.today) ⇒ Object

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/achoo/ui/date_chooser.rb', line 24

def parse_date(date_str, base=Date.today)
  raise ArgumentError.new('Invalid date') if date_str.nil?
  
  # Today (default)
  if date_str == 'today' || date_str.empty?
    return Date.today
  end
  
  # Base offset
  case date_str.chars.first
  when '-'
    return base - Integer(date_str[1..-1])
  when '+'
    return base + Integer(date_str[1..-1])
  end
  
  # 
  date = date_str.split('-').collect {|d| d.to_i}
  case date.length
  when 1
    return Date.civil(base.year, base.month, *date)
  when 2
    return Date.civil(base.year, *date)
  when 3
    date[0] += 2000 if date[0] < 100
    return Date.civil(*date)
  end
  
  raise ArgumentError.new('Invalid date')
end