class TableSchema::Types::YearMonth

Public Class Methods

supported_constraints() click to toggle source
# File lib/tableschema/types/yearmonth.rb, line 9
def self.supported_constraints
  [
    'required',
    'unique',
    'pattern',
    'enum',
    'minimum',
    'maximum',
  ]
end

Public Instance Methods

cast_default(value) click to toggle source
# File lib/tableschema/types/yearmonth.rb, line 24
def cast_default(value)
  value = array_to_yearmonth_string(value) if value.class == ::Array
  cast = ::Date._strptime(value, '%Y-%m')
  unless cast.nil? || cast.include?(:leftover)
    array_to_yearmonth(cast.values)
  else
    raise TableSchema::InvalidYearMonthType.new("#{value} is not a valid yearmonth")
  end
end
name() click to toggle source
# File lib/tableschema/types/yearmonth.rb, line 5
def name
  'yearmonth'
end
type() click to toggle source
# File lib/tableschema/types/yearmonth.rb, line 20
def type
  ::Hash
end

Private Instance Methods

array_to_yearmonth(value_array) click to toggle source
# File lib/tableschema/types/yearmonth.rb, line 36
def array_to_yearmonth(value_array)
  {
    year: value_array[0],
    month: value_array[1],
  }.freeze
end
array_to_yearmonth_string(value) click to toggle source
# File lib/tableschema/types/yearmonth.rb, line 43
def array_to_yearmonth_string(value)
  if value.length != 2
    raise TableSchema::InvalidYearMonthType.new("#{value} is not a valid yearmonth")
  end
  "#{value[0]}-#{value[1]}"
end