Class: Superstore::Types::DateType

Inherits:
BaseType
  • Object
show all
Defined in:
lib/superstore/types/date_type.rb

Constant Summary collapse

FORMAT =
'%Y-%m-%d'
REGEX =
/\A\d{4}-\d{2}-\d{2}\Z/

Instance Attribute Summary

Attributes inherited from BaseType

#model, #options

Instance Method Summary collapse

Methods inherited from BaseType

#initialize

Constructor Details

This class inherits a constructor from Superstore::Types::BaseType

Instance Method Details

#decode(str) ⇒ Object



12
13
14
15
# File 'lib/superstore/types/date_type.rb', line 12

def decode(str)
  return nil if str.empty?
  Date.parse(str)
end

#encode(value) ⇒ Object

Raises:

  • (ArgumentError)


7
8
9
10
# File 'lib/superstore/types/date_type.rb', line 7

def encode(value)
  raise ArgumentError.new("#{value.inspect} is not a Date") unless value.kind_of?(Date)
  value.strftime(FORMAT)
end

#typecast(value) ⇒ Object



17
18
19
# File 'lib/superstore/types/date_type.rb', line 17

def typecast(value)
  value.to_date
end