Class: XapianDb::TypeCodec::DateCodec
- Inherits:
-
Object
- Object
- XapianDb::TypeCodec::DateCodec
- Defined in:
- lib/type_codec.rb
Class Method Summary collapse
-
.decode(date_as_string) ⇒ Date
Decode a string to a date.
-
.encode(date) ⇒ String
Encode a date to a string in the format ‘yyyymmdd’.
Class Method Details
.decode(date_as_string) ⇒ Date
Decode a string to a date
105 106 107 108 109 110 111 112 |
# File 'lib/type_codec.rb', line 105 def self.decode(date_as_string) return nil if date_as_string.nil? || date_as_string.strip == "" begin Date.parse date_as_string rescue ArgumentError raise ArgumentError.new "'#{date_as_string}' cannot be converted to a date" end end |
.encode(date) ⇒ String
Encode a date to a string in the format ‘yyyymmdd’
93 94 95 96 97 98 99 100 |
# File 'lib/type_codec.rb', line 93 def self.encode(date) return nil if date.nil? begin date.strftime "%Y%m%d" rescue NoMethodError raise ArgumentError.new "#{date} was expected to be a date" end end |