class XfOOrth::Duration

Formatter support for the Duration class.

Intervals support for the Duration class.

The duration class adds support for intervals of time that are not directly associated with a date or time. Like 10 minutes as opposed to July 4, 2015 5:43 PM.

Constants

AN_HOUR

Seconds in an hour.

A_DAY

Seconds in a day.

A_MINUTE

Seconds in a minute.

A_MONTH

Seconds in a (average) month.

A_SECOND

Seconds in a second.

A_YEAR

Seconds in a (average) year.

INTERVALS

An array of interval values.

LABELS

An array of interval labels.

Attributes

period[RW]

The length of time of the duration.

Public Class Methods

new(period) click to toggle source

Create a duration instance.
Parameters

  • period - The period of time of the duration.

# File lib/fOOrth/library/duration/make.rb, line 14
def initialize(period)
  @period = period.rationalize
end
pick_label(index, qty=1) click to toggle source

Pick the appropriate label
Endemic Code Smells

  • :reek:ControlParameter

# File lib/fOOrth/library/duration/intervals.rb, line 36
def self.pick_label(index, qty=1)
  result = LABELS[index]
  result = result.chop if qty == 1
  " " + result
end

Public Instance Methods

==(other)

Alias == to the eql? operator.

Alias for: eql?
as_days() click to toggle source

How many total days in this duration?

# File lib/fOOrth/library/duration/intervals.rb, line 75
def as_days
  (@period/A_DAY.to_r).to_f
end
as_hours() click to toggle source

How many total hours in this duration?

# File lib/fOOrth/library/duration/intervals.rb, line 86
def as_hours
  (@period/AN_HOUR.to_r).to_f
end
as_minutes() click to toggle source

How many total minutes in this duration?

# File lib/fOOrth/library/duration/intervals.rb, line 97
def as_minutes
  (@period/A_MINUTE.to_r).to_f
end
as_months() click to toggle source

How many total months in this duration?

# File lib/fOOrth/library/duration/intervals.rb, line 64
def as_months
  (@period/A_MONTH.to_r).to_f
end
as_seconds() click to toggle source

How many total seconds in this duration?

# File lib/fOOrth/library/duration/intervals.rb, line 108
def as_seconds
  @period.to_f
end
as_years() click to toggle source

How many total years in this duration?

# File lib/fOOrth/library/duration/intervals.rb, line 53
def as_years
  (@period/A_YEAR.to_r).to_f
end
days() click to toggle source

How many days into the month in this duration?

# File lib/fOOrth/library/duration/intervals.rb, line 70
def days
  ((@period % A_MONTH.to_r)/A_DAY.to_r).to_i
end
eql?(other) click to toggle source

Define equality for durations.

# File lib/fOOrth/library/duration/arithmetic.rb, line 26
def eql?(other)
  @period.eql?(other.to_foorth_r)
end
Also aliased as: ==
foorth_coerce(arg) click to toggle source

Coerce the argument to match my type.

# File lib/fOOrth/library/duration/arithmetic.rb, line 10
def foorth_coerce(arg)
  @period.foorth_coerce(arg)
end
hours() click to toggle source

How many hours into the day in this duration?

# File lib/fOOrth/library/duration/intervals.rb, line 81
def hours
  (((@period % A_MONTH.to_r) % A_DAY.to_r)/AN_HOUR.to_r).to_i
end
largest_interval() click to toggle source

Find the largest interval for this duration

# File lib/fOOrth/library/duration/intervals.rb, line 43
def largest_interval
  (0..5).detect {|idx| INTERVALS[idx] <= period} || 5
end
method_missing(symbol, *args, &block) click to toggle source

Pass off all unknown methods to the period data.

# File lib/fOOrth/library/duration_library.rb, line 37
def method_missing(symbol, *args, &block)
  @period.send(symbol, *args, &block)
end
minutes() click to toggle source

How many minutes into the hour in this duration?

# File lib/fOOrth/library/duration/intervals.rb, line 92
def minutes
  (((@period % A_MONTH.to_r) % AN_HOUR.to_r)/A_MINUTE.to_r).to_i
end
months() click to toggle source

How many months into the year in this duration?

# File lib/fOOrth/library/duration/intervals.rb, line 59
def months
  ((@period % A_YEAR.to_r)/A_MONTH.to_r).to_i
end
rationalize()

Alias rationalize to the to_r method.

Alias for: to_r
seconds() click to toggle source

How many seconds into the minute in this duration?

# File lib/fOOrth/library/duration/intervals.rb, line 103
def seconds
  ((@period % A_MONTH.to_r) % A_MINUTE.to_f)
end
to_a() click to toggle source

Convert this duration to an array.
Endemic Code Smells

  • :reek:FeatureEnvy – false positive.

# File lib/fOOrth/library/duration_library.rb, line 19
def to_a
  balance = @period

  Duration::INTERVALS.map do |item|
    interval = item.to_r

    if interval > 1
      value = (balance / interval).to_i
      balance -= value * interval
      value
    else
      balance.to_f
    end
  end

end
to_foorth_r()

Alias to_foorth_r to the to_r method.

Alias for: to_r
to_r() click to toggle source

Convert this duration to a rational number.

# File lib/fOOrth/library/duration/arithmetic.rb, line 15
def to_r
  @period
end
Also aliased as: rationalize, to_foorth_r
years() click to toggle source

How many whole years in this duration?

# File lib/fOOrth/library/duration/intervals.rb, line 48
def years
  (@period/A_YEAR.to_r).to_i
end