Class: RichUnits::Duration
- Inherits:
-
Object
- Object
- RichUnits::Duration
- Includes:
- Comparable
- Defined in:
- lib/richunits/duration.rb
Defined Under Namespace
Constant Summary collapse
- SECOND =
1
- MINUTE =
60 * SECOND
- HOUR =
60 * MINUTE
- DAY =
24 * HOUR
- WEEK =
7 * DAY
- YEAR =
365 * DAY
- SEGMENTS =
%w{years weeks days hours minutes seconds}.collect{ |s| s.to_sym }
Class Method Summary collapse
-
.[](seconds, segmentA = nil, segmentB = nil) ⇒ Object
Same as #new.
Instance Method Summary collapse
- #*(other) ⇒ Object
- #+(other) ⇒ Object
-
#+@ ⇒ Object
:nodoc:.
- #-(other) ⇒ Object
-
#-@ ⇒ Object
:nodoc:.
- #/(other) ⇒ Object
- #<=>(other) ⇒ Object
-
#==(other) ⇒ Object
Returns true if
other
is also a Duration instance with the samevalue
, or ifother == value
. - #after(time) ⇒ Object
- #before(time) ⇒ Object
- #days ⇒ Object
- #hours ⇒ Object
-
#initialize(seconds = 0, segmentA = nil, segmentB = nil) ⇒ Duration
constructor
New duration.
- #inspect ⇒ Object
- #minutes ⇒ Object
-
#reset_segments(segmentA = nil, segmentB = nil) ⇒ Object
Reset segments.
- #seconds ⇒ Object
- #segmented(*segments) ⇒ Object
-
#segments ⇒ Object
List of period segments.
-
#strftime(fmt) ⇒ Object
Format duration.
- #to_a ⇒ Object
- #to_f ⇒ Object
- #to_h ⇒ Object
- #to_i ⇒ Object
- #to_s ⇒ Object
- #total ⇒ Object
- #weeks ⇒ Object
-
#years ⇒ Object
def self.===(other) #:nodoc: other.is_a?(Duration) rescue super end.
Constructor Details
#initialize(seconds = 0, segmentA = nil, segmentB = nil) ⇒ Duration
New duration.
call-seq:
new(seconds)
new(seconds, max-period)
new(seconds, max-period, min-period)
new(seconds, [period1, period2, ...])
29 30 31 32 |
# File 'lib/richunits/duration.rb', line 29 def initialize(seconds=0, segmentA=nil, segmentB=nil) @seconds = seconds.to_i reset_segments(segmentA, segmentB) end |
Class Method Details
.[](seconds, segmentA = nil, segmentB = nil) ⇒ Object
Same as #new
17 18 19 |
# File 'lib/richunits/duration.rb', line 17 def self.[](seconds, segmentA=nil, segmentB=nil) new(seconds, segmentA, segmentB) end |
Instance Method Details
#*(other) ⇒ Object
144 145 146 |
# File 'lib/richunits/duration.rb', line 144 def *(other) self.class.new(@seconds * other.to_i, segments) end |
#+(other) ⇒ Object
136 137 138 |
# File 'lib/richunits/duration.rb', line 136 def +(other) self.class.new(@seconds + other.to_i, segments) end |
#+@ ⇒ Object
:nodoc:
210 211 212 |
# File 'lib/richunits/duration.rb', line 210 def +@ #:nodoc: self.class.new(+@seconds) end |
#-(other) ⇒ Object
140 141 142 |
# File 'lib/richunits/duration.rb', line 140 def -(other) self.class.new(@seconds - other.to_i, segments) end |
#-@ ⇒ Object
:nodoc:
205 206 207 |
# File 'lib/richunits/duration.rb', line 205 def -@ #:nodoc: self.class.new(-@seconds) end |
#/(other) ⇒ Object
148 149 150 |
# File 'lib/richunits/duration.rb', line 148 def /(other) self.class.new(@seconds / other.to_i, segments) end |
#<=>(other) ⇒ Object
115 116 117 |
# File 'lib/richunits/duration.rb', line 115 def <=>(other) @seconds <=> other.to_i end |
#==(other) ⇒ Object
Returns true if other
is also a Duration instance with the same value
, or if other == value
.
107 108 109 110 111 112 113 |
# File 'lib/richunits/duration.rb', line 107 def ==(other) if Duration === other other.seconds == seconds else other == seconds end end |
#after(time) ⇒ Object
224 225 226 |
# File 'lib/richunits/duration.rb', line 224 def after(time) @seconds.after(time) end |
#before(time) ⇒ Object
219 220 221 |
# File 'lib/richunits/duration.rb', line 219 def before(time) @seconds.before(time) end |
#days ⇒ Object
129 |
# File 'lib/richunits/duration.rb', line 129 def days ; to_h[:days] ; end |
#hours ⇒ Object
130 |
# File 'lib/richunits/duration.rb', line 130 def hours ; to_h[:hours] ; end |
#inspect ⇒ Object
63 64 65 66 67 68 |
# File 'lib/richunits/duration.rb', line 63 def inspect h = to_h segments.reverse.collect do |l| "#{h[l.to_sym]} #{l}" end.join(' ') end |
#minutes ⇒ Object
131 |
# File 'lib/richunits/duration.rb', line 131 def minutes ; to_h[:minutes] ; end |
#reset_segments(segmentA = nil, segmentB = nil) ⇒ Object
Reset segments.
call-seq:
reset_segments(max-period)
reset_segments(max-period, min-period)
reset_segments([period1, period2, ...])
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/richunits/duration.rb', line 44 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 ⇒ Object
132 |
# File 'lib/richunits/duration.rb', line 132 def seconds ; to_h[:seconds] ; end |
#segmented(*segments) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/richunits/duration.rb', line 153 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 ⇒ Object
List of period segments.
35 |
# File 'lib/richunits/duration.rb', line 35 def segments; @segments; end |
#strftime(fmt) ⇒ Object
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!"
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/richunits/duration.rb', line 187 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 ⇒ Object
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/richunits/duration.rb', line 75 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 ⇒ Object
71 |
# File 'lib/richunits/duration.rb', line 71 def to_f ; @seconds.to_f ; end |
#to_h ⇒ Object
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/richunits/duration.rb', line 87 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 ⇒ Object
70 |
# File 'lib/richunits/duration.rb', line 70 def to_i ; @seconds.to_i ; end |
#to_s ⇒ Object
98 99 100 101 102 103 |
# File 'lib/richunits/duration.rb', line 98 def to_s h = to_h segments.reverse.collect do |l| "#{h[l.to_sym]} #{l}" end.join(' ') end |
#total ⇒ Object
134 |
# File 'lib/richunits/duration.rb', line 134 def total ; seconds ; end |
#weeks ⇒ Object
128 |
# File 'lib/richunits/duration.rb', line 128 def weeks ; to_h[:weeks] ; end |
#years ⇒ Object
127 |
# File 'lib/richunits/duration.rb', line 127 def years ; to_h[:years] ; end |