Class: Holiday::Parser

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

Overview

Extracts the relevant information from strings

Class Method Summary collapse

Class Method Details

.month(string) ⇒ Object



20
21
22
# File 'lib/holiday/parser.rb', line 20

def month(string)
  (string.split(' ').map(&:downcase) & months).join
end

.monthsObject



36
37
38
# File 'lib/holiday/parser.rb', line 36

def months
  %W(january february march april may june july august september october november december)
end

.occurrence(string) ⇒ Object



24
25
26
# File 'lib/holiday/parser.rb', line 24

def occurrence(string)
  string.scan(/\d/).join.to_i
end

.parse(string) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/holiday/parser.rb', line 5

def parse(string)
  if string =~ /in/
    w,m,o = weekday(string), month(string), occurrence(string)
    d = wday(w)
    
    { :weekday => w, :wday => d, :month => m, :occurrence => o }          
  else
    string
  end
end

.wday(which) ⇒ Object



28
29
30
# File 'lib/holiday/parser.rb', line 28

def wday(which)
  weekdays.index(which)
end

.weekday(string) ⇒ Object



16
17
18
# File 'lib/holiday/parser.rb', line 16

def weekday(string)
  (string.split(' ').map(&:downcase) & weekdays).join
end

.weekdaysObject



32
33
34
# File 'lib/holiday/parser.rb', line 32

def weekdays
  %W(sunday monday tuesday wednesday thursday friday saturday)
end