Module: CoreExtensions::ActiveSupport::DurationAdditions

Defined in:
lib/core_extensions/active_support/duration.rb

Constant Summary collapse

FORMATTING_ELEMENTS =
[:years, :months, :weeks, :days, :hours, :minutes, :seconds].freeze

Instance Method Summary collapse

Instance Method Details

#to_formatted_sObject

Add a custom method on ActiveSupportDuration. This is a variation on Duration#inspect that excludes empty parts. Given an iso8601 duration of 1 week:

duration = Duration.parse("P1W")
duration.inspect        #=> "1 week, 0 days, and 0 hours"
duration.to_formatted_s #=> "1 week"

rubocop:disable Style/Semicolon, Style/EachWithObject



16
17
18
19
20
21
22
23
# File 'lib/core_extensions/active_support/duration.rb', line 16

def to_formatted_s
  parts
    .reduce(::Hash.new(0)) { |h, (l, r)| h[l] += r; h }
    .reject { |_, val| val < 1 }
    .sort_by { |unit, _| FORMATTING_ELEMENTS.index(unit) }
    .map { |unit, val| "#{val} #{val == 1 ? unit.to_s.chop : unit.to_s}" }
    .to_sentence(locale: ::I18n.default_locale)
end