class RichUnits::Duration
Durations
Constants
- DAY
- HOUR
- MINUTE
- SECOND
- SEGMENTS
- WEEK
- YEAR
Public Class Methods
[](seconds, segmentA=nil, segmentB=nil)
click to toggle source
Same as new
# File lib/richunits/duration.rb, line 19 def self.[](seconds, segmentA=nil, segmentB=nil) new(seconds, segmentA, segmentB) end
new(seconds)
click to toggle source
new(seconds, max-period)
new(seconds, max-period, min-period)
new(seconds, [period1, period2, ...])
New duration.
# File lib/richunits/duration.rb, line 31 def initialize(seconds=0, segmentA=nil, segmentB=nil) @seconds = seconds.to_i reset_segments(segmentA, segmentB) end
Public Instance Methods
*(other)
click to toggle source
# File lib/richunits/duration.rb, line 148 def *(other) self.class.new(@seconds * other.to_i, segments) end
+(other)
click to toggle source
# File lib/richunits/duration.rb, line 140 def +(other) self.class.new(@seconds + other.to_i, segments) end
-(other)
click to toggle source
# File lib/richunits/duration.rb, line 144 def -(other) self.class.new(@seconds - other.to_i, segments) end
/(other)
click to toggle source
# File lib/richunits/duration.rb, line 152 def /(other) self.class.new(@seconds / other.to_i, segments) end
<=>(other)
click to toggle source
# File lib/richunits/duration.rb, line 119 def <=>(other) @seconds <=> other.to_i end
==(other)
click to toggle source
Returns true if other
is also a Duration
instance with the same value
, or if other == value
.
# File lib/richunits/duration.rb, line 111 def ==(other) if Duration === other other.seconds == seconds else other == seconds end end
after(time)
click to toggle source
# File lib/richunits/duration.rb, line 228 def after(time) @seconds.after(time) end
before(time)
click to toggle source
# File lib/richunits/duration.rb, line 223 def before(time) @seconds.before(time) end
days()
click to toggle source
# File lib/richunits/duration.rb, line 133 def days ; to_h[:days] ; end
hours()
click to toggle source
# File lib/richunits/duration.rb, line 134 def hours ; to_h[:hours] ; end
inspect()
click to toggle source
# File lib/richunits/duration.rb, line 65 def inspect h = to_h segments.reverse.collect do |l| "#{h[l.to_sym]} #{l}" end.join(' ') end
minutes()
click to toggle source
# File lib/richunits/duration.rb, line 135 def minutes ; to_h[:minutes] ; end
reset_segments(max-period)
click to toggle source
reset_segments(max-period, min-period)
reset_segments([period1, period2, ...])
Reset segments.
# File lib/richunits/duration.rb, line 46 def reset_segments(segmentA=nil, segmentB=nil) if !segmentA @segments = [:days, :hours, :minutes, :seconds] elsif !segmentB case segmentA when Array @segments = segmentA.map{ |p| (p.to_s.downcase.chomp('s') + 's').to_sym } raise ArgumentError unless @segments.all?{ |s| SEGMENTS.include?(s) } else f = SEGMENTS.index(segmentA) @segments = SEGMENTS[f..0] end else # segmentA && segmentB f = SEGMENTS.index(segmentA) t = SEGMENTS.index(segmentB) @segments = SEGMENTS[f..t] end end
seconds()
click to toggle source
# File lib/richunits/duration.rb, line 136 def seconds ; to_h[:seconds] ; end
segmented(*segments)
click to toggle source
# File lib/richunits/duration.rb, line 157 def segmented(*segments) self.class.new(@seconds, segments) #segments = segments.collect{ |p| p.to_s.downcase.chomp('s') } #y,w,d,h,m,s = nil,nil,nil,nil,nil,nil #x = @seconds #y, x = *x.divmod(YEAR) if segments.include?('year') #w, x = *x.divmod(WEEK) if segments.include?('week') #d, x = *x.divmod(DAY) if segments.include?('day') #h, x = *x.divmod(HOUR) if segments.include?('hour') #m, x = *x.divmod(MINUTE) if segments.include?('minute') #s = x if segments.include?('second') #[y, w, d, h, m, s].compact end
segments()
click to toggle source
List of period segments.
# File lib/richunits/duration.rb, line 37 def segments; @segments; end
strftime(fmt)
click to toggle source
Format duration.
Identifiers
%w -- Number of weeks %d -- Number of days %h -- Number of hours %m -- Number of minutes %s -- Number of seconds %t -- Total number of seconds %x -- Duration#to_s %% -- Literal `%' character
Example
d = Duration.new(:weeks => 10, :days => 7) => #<Duration: 11 weeks> d.strftime("It's been %w weeks!") => "It's been 11 weeks!"
# File lib/richunits/duration.rb, line 191 def strftime(fmt) h = to_h hx = { 'y' => h[:years] , 'w' => h[:weeks] , 'd' => h[:days] , 'h' => h[:hours] , 'm' => h[:minutes], 's' => h[:seconds], 't' => total, 'x' => to_s } fmt.gsub(/%?%(w|d|h|m|s|t|x)/) do |match| hx[match[1..1]] end.gsub('%%', '%') end
to_a()
click to toggle source
# File lib/richunits/duration.rb, line 79 def to_a a, s = [], @seconds a[5], s = *s.divmod(YEAR) if @segments.include?(:years) a[4], s = *s.divmod(WEEK) if @segments.include?(:weeks) a[3], s = *s.divmod(DAY) if @segments.include?(:days) a[2], s = *s.divmod(HOUR) if @segments.include?(:hours) a[1], s = *s.divmod(MINUTE) if @segments.include?(:minutes) a[0], s = *s.divmod(SECOND) if @segments.include?(:seconds) a.compact.reverse end
to_f()
click to toggle source
# File lib/richunits/duration.rb, line 73 def to_f ; @seconds.to_f ; end
to_h()
click to toggle source
# File lib/richunits/duration.rb, line 91 def to_h h, s = {}, @seconds h[:years], s = *s.divmod(YEAR) if @segments.include?(:years) h[:weeks], s = *s.divmod(WEEK) if @segments.include?(:weeks) h[:days], s = *s.divmod(DAY) if @segments.include?(:days) h[:hours], s = *s.divmod(HOUR) if @segments.include?(:hours) h[:minutes], s = *s.divmod(MINUTE) if @segments.include?(:minutes) h[:seconds], s = *s.divmod(SECOND) if @segments.include?(:seconds) h end
to_i()
click to toggle source
# File lib/richunits/duration.rb, line 72 def to_i ; @seconds.to_i ; end
Also aliased as: to_int
to_s()
click to toggle source
# File lib/richunits/duration.rb, line 102 def to_s h = to_h segments.reverse.collect do |l| "#{h[l.to_sym]} #{l}" end.join(' ') end
total()
click to toggle source
# File lib/richunits/duration.rb, line 138 def total ; seconds ; end
weeks()
click to toggle source
# File lib/richunits/duration.rb, line 132 def weeks ; to_h[:weeks] ; end
years()
click to toggle source
def self.===(other) #:nodoc:
other.is_a?(Duration) rescue super
end
# File lib/richunits/duration.rb, line 131 def years ; to_h[:years] ; end