class XfOOrth::Duration
-
library/duration/arithmetic.rb - Arithmetic operator support for durations.
Formatter support for the Duration class.
Intervals support for the Duration class.
-
library/duration/make.rb - Support for duration constructor.
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
The length of time of the duration.
Public Class Methods
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 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
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
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
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
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
How many total seconds in this duration?
# File lib/fOOrth/library/duration/intervals.rb, line 108 def as_seconds @period.to_f end
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
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
Define equality for durations.
# File lib/fOOrth/library/duration/arithmetic.rb, line 26 def eql?(other) @period.eql?(other.to_foorth_r) end
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
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
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
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
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
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
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
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
Convert this duration to a rational number.
# File lib/fOOrth/library/duration/arithmetic.rb, line 15 def to_r @period end
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