/
date.cr
64 lines (55 loc) · 1.64 KB
/
date.cr
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
module Marten
abstract class Schema
module Field
# Represents a date schema field.
class Date < Base
def deserialize(value) : Time?
return if empty_value?(value)
date = case value
when Nil
value
when ::Time
value
when ::String
parse_date(value) || raise_unexpected_field_value(value)
when JSON::Any
deserialize(value.raw)
else
raise_unexpected_field_value(value)
end
date.in(Marten.settings.time_zone) unless date.nil?
end
def serialize(value) : ::String?
case value
when ::Time
value.to_s("%F")
else
value.try(&.to_s)
end
end
private def fetch_date_format(index)
I18n.t!("marten.schema.field.date.input_formats.#{index}")
rescue I18n::Errors::MissingTranslation
nil
end
private def invalid_error_message(_schema)
I18n.t("marten.schema.field.date.errors.invalid")
end
private def parse_date(value)
result = nil
attempt = 0
format = I18n.t("marten.schema.field.date.input_formats.#{attempt}")
while result.nil? && (format = fetch_date_format(attempt))
result = begin
Time.parse(value, format, Marten.settings.time_zone)
rescue Time::Format::Error
nil
end
attempt += 1
end
result
end
end
end
end
end