Class: ActiveSupport::Duration::HumanStringSerializer
- Inherits:
-
ISO8601Serializer
- Object
- ISO8601Serializer
- ActiveSupport::Duration::HumanStringSerializer
- Defined in:
- lib/active_support/duration/human_string.rb
Overview
Based on: active_support/duration/iso8601_serializer.rb Inherits: #normalize
Instance Method Summary collapse
-
#initialize(duration, precision: nil, separator: '', delimiter: ' ', use_2_digit_numbers: false) ⇒ HumanStringSerializer
constructor
A new instance of HumanStringSerializer.
-
#serialize ⇒ Object
Builds and returns output_parts string.
Constructor Details
#initialize(duration, precision: nil, separator: '', delimiter: ' ', use_2_digit_numbers: false) ⇒ HumanStringSerializer
Returns a new instance of HumanStringSerializer.
63 64 65 66 67 68 69 |
# File 'lib/active_support/duration/human_string.rb', line 63 def initialize(duration, precision: nil, separator: '', delimiter: ' ', use_2_digit_numbers: false) @duration = duration @precision = precision @separator = separator @delimiter = delimiter @use_2_digit_numbers = use_2_digit_numbers end |
Instance Method Details
#serialize ⇒ Object
Builds and returns output_parts string.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/active_support/duration/human_string.rb', line 72 def serialize parts, sign = normalize output_parts = [] output_parts << [parts[:years], 'y'] if parts.key?(:years) output_parts << [parts[:months], 'm'] if parts.key?(:months) output_parts << [parts[:weeks], 'w'] if parts.key?(:weeks) output_parts << [parts[:days], 'd'] if parts.key?(:days) output_parts << [parts[:hours], 'h'] if parts.key?(:hours) output_parts << [parts[:minutes], 'm'] if parts.key?(:minutes) if parts.key?(:seconds) output_parts << [sprintf(@precision ? "%0.0#{@precision}f" : '%g', parts[:seconds]), 's'] end output_parts.map!.with_index { |(n, units), i| if @use_2_digit_numbers && i >= 1 n = sprintf('%02d', n) end "#{n}#{@separator}#{units}" } output = output_parts.join(@delimiter) "#{sign}#{output}" end |