Class: Timerizer::Duration::RoundedTime

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/timerizer/duration/rounded_time.rb

Overview

Wraps rounding a Timerizer::Duration to the nearest value by the number of “places”, which are customary time units (seconds, minutes, hours, days, months, years, etc; decades and weeks are not included).

Constant Summary collapse

DEFAULT_PLACES =

Default “places” (units, e.g., hours/minutes) to use for rounding.

2
OMITTED_KEYS =

Default UNITS not to include in rounded value.

[:decades, :weeks]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.call(duration, places = DEFAULT_PLACES, omitted_keys = OMITTED_KEYS) ⇒ Timerizer::Duration

Given an original Timerizer::Duration instance, return a new instance that “rounds” the duration to the closest value expressed in a certain number of units (default 2).

Examples:

t = (12.hours 16.minutes 47.seconds).ago
d = Time.since(t)
d2 = RoundedTime.call(d)
d.to_s  # => "12 hours, 16 minutes, 47 seconds"
d2.to_s # => "12 hours, 17 minutes"

Parameters:

  • duration ({Timerizer::Duration})

    Object encapsulating a duration (in hours, minutes, etc) to “round” to a number of units specified by ‘places`.

  • places (Integer) (defaults to: DEFAULT_PLACES)

    Number of units to include in rounded value. Default is 2.

  • omitted_keys (Array<Symbol>) (defaults to: OMITTED_KEYS)

    Units to omit from calculation or return value. Default is ‘[:decades, :weeks]`

Returns:



46
47
48
49
# File 'lib/timerizer/duration/rounded_time.rb', line 46

def self.call(duration, places = DEFAULT_PLACES,
              omitted_keys = OMITTED_KEYS)
  new(duration, places, omitted_keys).call
end

Instance Method Details

#callTimerizer::Duration

High-level method to do calculations on component durations.

Returns:



55
56
57
58
# File 'lib/timerizer/duration/rounded_time.rb', line 55

def call
  remainder = sum_of(remainder_times)
  sum_of(times) + offset_from(remainder)
end

#remainder_timesObject



20
# File 'lib/timerizer/duration/rounded_time.rb', line 20

def_delegators :@tt, :remainder_times, :target_unit, :times

#target_unitObject



20
# File 'lib/timerizer/duration/rounded_time.rb', line 20

def_delegators :@tt, :remainder_times, :target_unit, :times

#timesObject



20
# File 'lib/timerizer/duration/rounded_time.rb', line 20

def_delegators :@tt, :remainder_times, :target_unit, :times