Class: Realize::Format::Date
- Inherits:
-
Object
- Object
- Realize::Format::Date
- Defined in:
- lib/realize/format/date.rb
Overview
Formats a date/time value. If the input is nil, it will output nil. If an input_format is configured then it will use that to explicitly parse it. If an input_format is not specified then it will use Ruby’s DateTime#parse heuristics for parsing. By default, the output format is ISO-8601 standard (YYYY-MM-DD). It is also possible to have this output formatted time as well, but since it is named ‘format_date’, it is assumed you want something date-only.
See this link for more information about Ruby’s DateTime parsing and formatting details and also for valid format tokens: ruby-doc.org/stdlib-2.6.6/libdoc/date/rdoc/DateTime.html#method-i-strftime
Constant Summary collapse
- OUTPUT_FORMAT =
'%F'
Instance Attribute Summary collapse
-
#input_format ⇒ Object
readonly
Returns the value of attribute input_format.
-
#output_format ⇒ Object
readonly
Returns the value of attribute output_format.
Instance Method Summary collapse
-
#initialize(input_format: '', output_format: OUTPUT_FORMAT) ⇒ Date
constructor
A new instance of Date.
- #transform(_resolver, value, _time, _record) ⇒ Object
Constructor Details
#initialize(input_format: '', output_format: OUTPUT_FORMAT) ⇒ Date
Returns a new instance of Date.
29 30 31 32 33 34 |
# File 'lib/realize/format/date.rb', line 29 def initialize(input_format: '', output_format: OUTPUT_FORMAT) @input_format = input_format.to_s @output_format = output_format.to_s freeze end |
Instance Attribute Details
#input_format ⇒ Object (readonly)
Returns the value of attribute input_format.
27 28 29 |
# File 'lib/realize/format/date.rb', line 27 def input_format @input_format end |
#output_format ⇒ Object (readonly)
Returns the value of attribute output_format.
27 28 29 |
# File 'lib/realize/format/date.rb', line 27 def output_format @output_format end |
Instance Method Details
#transform(_resolver, value, _time, _record) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/realize/format/date.rb', line 36 def transform(_resolver, value, _time, _record) return nil if value.to_s.empty? date_time = if input_format? DateTime.strptime(value, input_format) else DateTime.parse(value.to_s) end date_time.strftime(output_format) end |