Class: JsonTableSchema::Types::DateTime

Inherits:
Base
  • Object
show all
Defined in:
lib/jsontableschema/types/datetime.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#cast, #initialize, #set_format, #test

Methods included from Helpers

#convert_to_boolean, #false_values, #get_class_for_type, #true_values, #type_class_lookup

Constructor Details

This class inherits a constructor from JsonTableSchema::Types::Base

Class Method Details

.supported_constraintsObject



9
10
11
12
13
14
15
16
17
# File 'lib/jsontableschema/types/datetime.rb', line 9

def self.supported_constraints
  [
    'required',
    'pattern',
    'enum',
    'minimum',
    'maximum'
  ]
end

Instance Method Details

#cast_any(value) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/jsontableschema/types/datetime.rb', line 36

def cast_any(value)
  return value if value.is_a?(type)

  begin
    date = ::DateTime._parse(value)
    if date.values.count >= 4
      ::DateTime.parse(value)
    else
      raise JsonTableSchema::InvalidDateTimeType.new("#{value} is not a valid datetime")
    end
  rescue ArgumentError
    raise JsonTableSchema::InvalidDateTimeType.new("#{value} is not a valid datetime")
  end
end

#cast_default(value) ⇒ Object

raw_formats = [‘DD/MM/YYYYThh/mm/ss’] py_formats = [‘%Y/%m/%dT%H:%M:%S’] format_map = dict(zip(raw_formats, py_formats))



31
32
33
34
# File 'lib/jsontableschema/types/datetime.rb', line 31

def cast_default(value)
  @format_string = iso8601
  cast_fmt(value)
end

#cast_fmt(value) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/jsontableschema/types/datetime.rb', line 51

def cast_fmt(value)
  return value if value.is_a?(type)

  begin
    return ::DateTime.strptime(value, @format_string)
  rescue ArgumentError
    raise JsonTableSchema::InvalidDateTimeType.new("#{value} is not a valid date")
  end
end

#iso8601Object



23
24
25
# File 'lib/jsontableschema/types/datetime.rb', line 23

def iso8601
  '%Y-%m-%dT%H:%M:%SZ'
end

#nameObject



5
6
7
# File 'lib/jsontableschema/types/datetime.rb', line 5

def name
  'datetime'
end

#typeObject



19
20
21
# File 'lib/jsontableschema/types/datetime.rb', line 19

def type
  ::DateTime
end