Module: SimpleMapper::Attributes::Types::Timestamp
- Defined in:
- lib/simple_mapper/attributes/types.rb
Overview
Provides timezone-aware second-resolution timestamp support for basic attributes.
Attributes of this type will have values that are instances of DateTime
. These DateTime
values will be reduced to strings of format ‘%Y-%m-%d %H:%M:%S%z’ when converting to a simple structure.
On input, a DateTime
instance or a string matching the above format will be accepted and map to the proper DateTime
.
Decoding or encoding nils will simply pass nil through.
Registered as type :timestamp
Constant Summary collapse
- FORMAT =
'%Y-%m-%d %H:%M:%S%z'
Class Method Summary collapse
-
.decode(value) ⇒ Object
Decode a value string of format +‘%Y-%m-%d %H:%M:%S%z’ into a
DateTime
instance. -
.default ⇒ Object
Return a new
DateTime
instance representing the current time. -
.encode(value) ⇒ Object
Encode a
DateTime
value as a string of format ‘%Y-%m-%d %H:%M:%S%z’.
Class Method Details
.decode(value) ⇒ Object
Decode a value string of format +‘%Y-%m-%d %H:%M:%S%z’ into a DateTime
instance. If given a DateTime
instance for value, that instance will be returned. Given nil
for value, nil
will be returned.
155 156 157 158 159 |
# File 'lib/simple_mapper/attributes/types.rb', line 155 def self.decode(value) return nil if value.nil? return value if value.instance_of? DateTime DateTime.strptime(value, FORMAT) end |
.default ⇒ Object
Return a new DateTime
instance representing the current time. Note that this will include fractional seconds; this precision is lost when encoded to string, as the string representation only has a precision to the second.
164 165 166 |
# File 'lib/simple_mapper/attributes/types.rb', line 164 def self.default DateTime.now end |
.encode(value) ⇒ Object
Encode a DateTime
value as a string of format ‘%Y-%m-%d %H:%M:%S%z’. Given nil
for _value, nil
will be returned.
147 148 149 150 |
# File 'lib/simple_mapper/attributes/types.rb', line 147 def self.encode(value) return nil if value.nil? value.strftime FORMAT end |