class Bronze::Transforms::Attributes::DateTimeTransform
Transform
class that normalizes a DateTime to a formatted string representation.
Constants
- ISO_8601
Format string for ISO 8601 date+time format. Equivalent to YYYY-MM-DDTHH:MM:SS+ZZZZ.
Attributes
format[R]
@return [String] the format string.
Public Class Methods
instance()
click to toggle source
@return [DateTimeTransform] a memoized instance of DateTimeTransform
.
# File lib/bronze/transforms/attributes/date_time_transform.rb, line 17 def self.instance @instance ||= new end
new(format = ISO_8601)
click to toggle source
@param format [String] The format string used to normalize and denormalize
date times. The default is ISO 8601 format.
# File lib/bronze/transforms/attributes/date_time_transform.rb, line 23 def initialize(format = ISO_8601) @format = format end
Public Instance Methods
denormalize(value)
click to toggle source
Converts a formatted DateTime string to a Date instance.
@param value [String] The normalized string.
@return [DateTime] the parsed date+time.
# File lib/bronze/transforms/attributes/date_time_transform.rb, line 35 def denormalize(value) return value if value.is_a?(DateTime) return nil if value.nil? || value.empty? DateTime.strptime(value, read_format) end
normalize(value)
click to toggle source
Converts a DateTime to a formatted string.
@param value [DateTime] The DateTime to format.
@return [String] the formatted string.
# File lib/bronze/transforms/attributes/date_time_transform.rb, line 48 def normalize(value) return nil if value.nil? value.strftime(format) end
Private Instance Methods
read_format()
click to toggle source
# File lib/bronze/transforms/attributes/date_time_transform.rb, line 56 def read_format @read_format ||= format.gsub('%-', '%') end