-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathdate.rb
113 lines (100 loc) · 2.61 KB
/
date.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
require 'date'
module SmartAnswer
module Question
class Date < Base
def validate_in_range
@validate_in_range = true
end
def from(&block)
if block_given?
@from_func = block
else
@from_func && @from_func.call
end
end
def to(&block)
if block_given?
@to_func = block
else
@to_func && @to_func.call
end
end
def default_day(&block)
if block_given?
@default_day_func = block
else
@default_day_func && @default_day_func.call
end
end
def default_month(&block)
if block_given?
@default_month_func = block
else
@default_month_func && @default_month_func.call
end
end
def default_year(&block)
if block_given?
@default_year_func = block
else
@default_year_func && @default_year_func.call
end
end
def range
@range ||= from && to ? from..to : false
end
def parse_input(input)
date = begin
case input
when Hash, ActiveSupport::HashWithIndifferentAccess
input = input.symbolize_keys
year_month_and_day = [
default_year || input[:year],
default_month || input[:month],
default_day || input[:day],
]
raise InvalidResponse unless year_month_and_day.all?(&:present?)
::Date.new(*year_month_and_day.map(&:to_i))
when String
::Date.parse(input)
when ::Date
input
else
raise InvalidResponse, "Bad date", caller
end
end
validate_input(date) if @validate_in_range
date
rescue ArgumentError => e
if e.message =~ /invalid date/
raise InvalidResponse, "Bad date: #{input.inspect}", caller
else
raise
end
end
def to_response(input)
date = parse_input(input)
{
day: date.day,
month: date.month,
year: date.year
}
rescue InvalidResponse
nil
end
def date_of_birth_defaults
from { 122.years.ago.beginning_of_year.to_date }
to { ::Date.today.end_of_year }
validate_in_range
end
private
def validate_input(date)
return unless range
min, max = [range.begin, range.end].sort
if date < min || date > max
raise InvalidResponse, "Provided date is out of range: #{date}", caller
end
end
end
end
end