Class: DateParser

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

Overview

Can translate various string representations of a date (e.g. 12.04.95) into an instance of Day.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(date, month) ⇒ DateParser

Returns a new instance of DateParser.


10
11
12
13
# File 'lib/date_parser.rb', line 10

def initialize(date, month)
  @date = date
  @month = month
end

Instance Attribute Details

#dateObject (readonly)

Returns the value of attribute date


8
9
10
# File 'lib/date_parser.rb', line 8

def date
  @date
end

#monthObject (readonly)

Returns the value of attribute month


8
9
10
# File 'lib/date_parser.rb', line 8

def month
  @month
end

Class Method Details

.parse(date, month) ⇒ Object


4
5
6
# File 'lib/date_parser.rb', line 4

def self.parse(date, month)
  new(date, month).parse
end

Instance Method Details

#parseObject


15
16
17
18
19
20
21
22
23
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
# File 'lib/date_parser.rb', line 15

def parse
  case date

  when /^\d{1,2}$/
    d = date.to_i
    if d > hand_in_date
      y, m = month.prev_month_year
    else
      m = month.number
      y = month.year
    end

  when /^\d{1,2}\.\d{1,2}$/
    d, m = date.split('.').map(&:to_i)
    if d > hand_in_date
      y = month.prev_month_year.year
    else
      y = month.year
    end


  when /^\d{1,2}\.\d{1,2}\.\d{2,4}$/
    d, m, y = date.split('.').map(&:to_i)
    y += 2000 if y < 100

  else
    raise "\"#{date}\" is in an unknown date format"
  end

  Day.new(
    :day => d,
    :month => m,
    :year => y
  )
end