Class: TableSchema::Types::Date
- Inherits:
-
Base
- Object
- Base
- TableSchema::Types::Date
show all
- Defined in:
- lib/tableschema/types/date.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Base
#cast, #initialize, #test
Methods included from Helpers
#deep_symbolize_keys, #get_class_for_type, #type_class_lookup
Class Method Details
.supported_constraints ⇒ Object
9
10
11
12
13
14
15
16
17
18
|
# File 'lib/tableschema/types/date.rb', line 9
def self.supported_constraints
[
'required',
'unique',
'pattern',
'enum',
'minimum',
'maximum',
]
end
|
Instance Method Details
#cast_any(value) ⇒ Object
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/tableschema/types/date.rb', line 33
def cast_any(value)
return value if value.is_a?(type)
date = ::Date._parse(value)
if date.values.count == 3
::Date.parse(value)
else
raise TableSchema::InvalidDateType.new("#{value} is not a valid date")
end
end
|
#cast_default(value) ⇒ Object
28
29
30
31
|
# File 'lib/tableschema/types/date.rb', line 28
def cast_default(value)
@format_string = iso8601
cast_fmt(value)
end
|
#cast_fmt(value) ⇒ Object
44
45
46
47
48
49
50
51
52
|
# File 'lib/tableschema/types/date.rb', line 44
def cast_fmt(value)
return value if value.is_a?(type)
begin
return ::Date.strptime(value, @format_string)
rescue ArgumentError
raise TableSchema::InvalidDateType.new("#{value} is not a valid date")
end
end
|
24
25
26
|
# File 'lib/tableschema/types/date.rb', line 24
def iso8601
'%Y-%m-%d'
end
|
5
6
7
|
# File 'lib/tableschema/types/date.rb', line 5
def name
'date'
end
|
20
21
22
|
# File 'lib/tableschema/types/date.rb', line 20
def type
::Date
end
|