class Timerizer::Duration::RoundedTime::TermTimes

Convert a single {Timerizer::Duration} instance into an enumeration of per-unit Duration instances (hours, minutes, etc).

@private

Attributes

remainder_times[R]

Time units to be “rounded” to adjust resulting Duration value. @return [Array<{Timerizer::Duration}>] Remaining time-unit values.

target_unit[R]

Least-significant time unit (e.g., “:days`) used for rounding. @return [Symbol] Least-significant time unit in the resulting value.

times[R]

Time units to be included in “rounded” result, before rounding. @return [Array<{Timerizer::Duration}>] Base result time-unit values.

Public Class Methods

new(duration, omitted_keys) click to toggle source

Build array of time-part values based on input {Timerizer::Duration}.

@param [{Timerizer::Duration}] duration Time differential used as

input.

@param [Array<Symbol>] omitted_keys {Timerizer::Duration::UNITS}

values to exclude from resulting value.
# File lib/timerizer/duration/rounded_time.rb, line 127
def initialize(duration, omitted_keys)
  @part_values = filter_units(duration, omitted_keys)
end

Public Instance Methods

call(places) click to toggle source

Compute per-unit {Timerizer::Duration} values, split based on unit count

@param [Integer] places Number of time units to include in main Array.

# File lib/timerizer/duration/rounded_time.rb, line 135
def call(places)
  # Note that `places` may exceed the number of actual units in the
  # value. For example, with a duration of `(10.days)` and a `places`
  # value of 2. Adjust as needed.
  places = @part_values.count - 1 if @part_values.count < places
  # If a zero-time `duration` is passed in, then @part_values will be
  # empty, and the key arithmetic will return `nil`. Adjust as needed.
  @target_unit = @part_values.keys[places - 1] || :seconds
  term_times = unit_times
  @times = term_times[0..places - 1]
  @remainder_times = term_times[@times.count..-1]
  self
end

Private Instance Methods

filter_units(duration, omitted_keys) click to toggle source
# File lib/timerizer/duration/rounded_time.rb, line 151
def filter_units(duration, omitted_keys)
  unit_keys = Timerizer::Duration::UNITS.keys - omitted_keys
  duration.to_units(*unit_keys).reject { |_, v| v.zero? }
end
unit_times() click to toggle source
# File lib/timerizer/duration/rounded_time.rb, line 156
def unit_times
  @part_values.map { |unit, val| Timerizer::Duration.new(unit => val) }
end