Last active
December 20, 2015 16:09
-
-
Save jorrizza/6159311 to your computer and use it in GitHub Desktop.
Pubquiz code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'date' | |
class PubQuizDate | |
class << self | |
def parse(str) | |
days = %w{zon maan dins woens donder vrij zater} | |
format, pattern = { | |
next_week: /\Avolgende week (\w+)dag\Z/, | |
after_that: /\Ade (\w+)dag daarna\Z/ | |
}.find {|fmt, ptrn| str =~ ptrn} | |
wday = $~.to_a[1] | |
if !format || !days.include?(wday) | |
raise ArgumentError, "can't parse that" | |
end | |
case format | |
when :next_week | |
now = DateTime.now | |
@last_date = now + (7 - now.wday) + days.index(wday) | |
when :after_that | |
raise RuntimeError, "no previous date set" unless @last_date | |
delta = days.index(wday) - @last_date.wday | |
delta += 7 if delta < 1 | |
@last_date += delta | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment