| Class | Duration |
| In: |
lib/more/facets/duration.rb
|
| Parent: | Object |
| 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 } |
# File lib/more/facets/duration.rb, line 14
14: def self.[](seconds, *segments)
15: new(seconds, *segments)
16: end
# File lib/more/facets/duration.rb, line 19
19: def initialize(seconds=0, *segments)
20: @seconds = seconds.to_i
21: reset_segments(*segments)
22: end
# File lib/more/facets/duration.rb, line 123
123: def +(other)
124: self.class.new(@seconds + other.to_i, segments)
125: end
# File lib/more/facets/duration.rb, line 131
131: def +(other)
132: self.class.new(@seconds * other.to_i, segments)
133: end
# File lib/more/facets/duration.rb, line 127
127: def -(other)
128: self.class.new(@seconds - other.to_i, segments)
129: end
# File lib/more/facets/duration.rb, line 135
135: def /(other)
136: self.class.new(@seconds / other.to_i, segments)
137: end
# File lib/more/facets/duration.rb, line 102
102: def <=>(other)
103: @seconds <=> other.to_i
104: end
# File lib/more/facets/duration.rb, line 211
211: def after(time)
212: @seconds.after(time)
213: end
# File lib/more/facets/duration.rb, line 206
206: def before(time)
207: @seconds.before(time)
208: end
# File lib/more/facets/duration.rb, line 50
50: def inspect
51: h = to_h
52: segments.reverse.collect do |l|
53: "#{h[l.to_sym]} #{l}"
54: end.join(' ')
55: end
# File lib/more/facets/duration.rb, line 28
28: def reset_segments(*segments)
29: case segments.size
30: when 0
31: @segments = [:days, :hours, :minutes, :seconds]
32: when 1
33: case segments = segments[0]
34: when Array
35: @segments = segments.collect{ |p| (p.to_s.downcase.chomp('s') + 's').to_sym }
36: raise ArgumentError unless @segments.all?{ |s| SEGMENTS.include?(s) }
37: else
38: f = SEGMENTS.index(segments)
39: @segments = SEGMENTS[f..0]
40: end
41: when 2
42: f = SEGMENTS.index(segments[0])
43: t = SEGMENTS.index(segments[1])
44: @segments = SEGMENTS[f..t]
45: else
46: raise ArgumentError
47: end
48: end
# File lib/more/facets/duration.rb, line 140
140: def segmented(*segments)
141: self.class.new(@seconds, segments)
142: #segments = segments.collect{ |p| p.to_s.downcase.chomp('s') }
143: #y,w,d,h,m,s = nil,nil,nil,nil,nil,nil
144: #x = @seconds
145: #y, x = *x.divmod(YEAR) if segments.include?('year')
146: #w, x = *x.divmod(WEEK) if segments.include?('week')
147: #d, x = *x.divmod(DAY) if segments.include?('day')
148: #h, x = *x.divmod(HOUR) if segments.include?('hour')
149: #m, x = *x.divmod(MINUTE) if segments.include?('minute')
150: #s = x if segments.include?('second')
151: #[y, w, d, h, m, s].compact
152: end
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/more/facets/duration.rb, line 174
174: def strftime(fmt)
175: h = to_h
176: hx = {
177: 'y' => h[:years] ,
178: 'w' => h[:weeks] ,
179: 'd' => h[:days] ,
180: 'h' => h[:hours] ,
181: 'm' => h[:minutes],
182: 's' => h[:seconds],
183: 't' => total,
184: 'x' => to_s
185: }
186: fmt.gsub(/%?%(w|d|h|m|s|t|x)/) do |match|
187: hx[match[1..1]]
188: end.gsub('%%', '%')
189: end
# File lib/more/facets/duration.rb, line 62
62: def to_a
63: a, s = [], @seconds
64: a[5], s = *s.divmod(YEAR) if @segments.include?(:years)
65: a[4], s = *s.divmod(WEEK) if @segments.include?(:weeks)
66: a[3], s = *s.divmod(DAY) if @segments.include?(:days)
67: a[2], s = *s.divmod(HOUR) if @segments.include?(:hours)
68: a[1], s = *s.divmod(MINUTE) if @segments.include?(:minutes)
69: a[0], s = *s.divmod(SECOND) if @segments.include?(:seconds)
70: a.compact.reverse
71: end
# File lib/more/facets/duration.rb, line 74
74: def to_h
75: h, s = {}, @seconds
76: h[:years], s = *s.divmod(YEAR) if @segments.include?(:years)
77: h[:weeks], s = *s.divmod(WEEK) if @segments.include?(:weeks)
78: h[:days], s = *s.divmod(DAY) if @segments.include?(:days)
79: h[:hours], s = *s.divmod(HOUR) if @segments.include?(:hours)
80: h[:minutes], s = *s.divmod(MINUTE) if @segments.include?(:minutes)
81: h[:seconds], s = *s.divmod(SECOND) if @segments.include?(:seconds)
82: h
83: end