class Bronze::Transforms::Attributes::DateTransform
Transform
class that normalizes a Date to a formatted string representation.
Constants
- ISO_8601
Format string for ISO 8601 date format. Equivalent to YYYY-MM-DD.
Attributes
format[R]
@return [String] the format string.
Public Class Methods
instance()
click to toggle source
@return [DateTransform] a memoized instance of DateTransform
.
# File lib/bronze/transforms/attributes/date_transform.rb, line 15 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
dates. The default is ISO 8601 format.
# File lib/bronze/transforms/attributes/date_transform.rb, line 21 def initialize(format = ISO_8601) @format = format end
Public Instance Methods
denormalize(value)
click to toggle source
Converts a formatted Date string to a Date instance.
@param value [String] The normalized string.
@return [Date] the parsed date.
# File lib/bronze/transforms/attributes/date_transform.rb, line 33 def denormalize(value) return value if value.is_a?(Date) return nil if value.nil? || value.empty? Date.strptime(value, read_format) end
normalize(value)
click to toggle source
Converts a Date to a formatted string.
@param value [Date] The Date to format.
@return [String] the formatted string.
# File lib/bronze/transforms/attributes/date_transform.rb, line 46 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_transform.rb, line 54 def read_format @read_format ||= format.gsub('%-', '%') end