chronological 1.0.0beta3 → 1.0.0beta4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -278,6 +278,28 @@ MyTimeRangeClass.ended #=> []
278
278
  MyTimeRangeClass.ended :as_of => 42.years.from_now #=> [range_instance]
279
279
  ```
280
280
 
281
+ #### Duration Components
282
+
283
+ When calling `duration` on a Chronological, by default it will return
284
+ the days, hours, minutes and seconds in a Hash. If you need a specific
285
+ combination of these pieces, you can pass them in an `:as` options like
286
+ so:
287
+
288
+ ```ruby
289
+ # range_instance's total duration is 5 days, 1 hour, 44 minutes and 23 seconds
290
+
291
+ range_instance.duration :as => [:days, :hours, :seconds]
292
+ #=> { :days => 5, :hours => 1, :seconds => 2663 }
293
+
294
+ range_instance.duration :as => [:days, :hours, :minutes]
295
+ #=> { :days => 5, :hours => 1, :minutes => 44 }
296
+
297
+ range_instance.duration :as => [:hours, :minutes, :seconds]
298
+ #=> { :hours => 121, :minutes => 44, :seconds => 23 }
299
+ ```
300
+
301
+ Affected methods:
302
+
281
303
  ## Is It Scheduled?
282
304
 
283
305
  Even though Chronological does not handle anything having to do with time
@@ -22,16 +22,32 @@ module Chronological
22
22
  !object.in_progress?
23
23
  end
24
24
 
25
- def duration(object)
26
- calculated_duration = duration_in_seconds(object)
25
+ def duration(object, options = { :as => [:days, :hours, :minutes, :seconds] })
26
+ duration_parts = options[:as]
27
+ remaining_duration = duration_in_seconds(object)
27
28
 
28
- return Hash.new unless calculated_duration.present?
29
+ return Hash.new unless remaining_duration.present?
29
30
 
30
- hours = (calculated_duration / 3600).to_i
31
- minutes = ((calculated_duration % 3600) / 60).to_i
32
- seconds = (calculated_duration % 60).to_i
31
+ if duration_parts.include? :days
32
+ days = remaining_duration / 86400
33
+ remaining_duration = remaining_duration % 86400
34
+ end
33
35
 
34
- { :hours => hours, :minutes => minutes, :seconds => seconds }
36
+ if duration_parts.include? :hours
37
+ hours = remaining_duration / 3600
38
+ remaining_duration = remaining_duration % 3600
39
+ end
40
+
41
+ if duration_parts.include? :minutes
42
+ minutes = remaining_duration / 60
43
+ remaining_duration = remaining_duration % 60
44
+ end
45
+
46
+ if duration_parts.include? :seconds
47
+ seconds = remaining_duration
48
+ end
49
+
50
+ { :days => days, :hours => hours, :minutes => minutes, :seconds => seconds }.select {|k,v| !v.nil?}
35
51
  end
36
52
 
37
53
  def in_progress?(object)
@@ -1,3 +1,3 @@
1
1
  module Chronological
2
- VERSION = '1.0.0beta3'
2
+ VERSION = '1.0.0beta4'
3
3
  end
@@ -71,8 +71,28 @@ describe Chronological::BaseStrategy do
71
71
  end
72
72
 
73
73
  describe '#duration' do
74
+ context 'when passed an options hash limiting the parts of the output' do
75
+ let(:duration_hash) { strategy.duration(chrono, :as => [:days, :hours, :seconds]) }
76
+
77
+ before { strategy.should_receive(:duration_in_seconds).and_return(438263) }
78
+
79
+ it 'removes it from the resulting hash' do
80
+ duration_hash.should_not have_key :minutes
81
+ end
82
+
83
+ it 'makes up for it in the following more granular item' do
84
+ duration_hash[:days].should eql 5
85
+ duration_hash[:hours].should eql 1
86
+ duration_hash[:seconds].should eql 2663
87
+ end
88
+ end
89
+
74
90
  context 'when the strategy represents something with a duration' do
75
- before { strategy.should_receive(:duration_in_seconds).and_return(6263) }
91
+ before { strategy.should_receive(:duration_in_seconds).and_return(438263) }
92
+
93
+ it 'is a hash with the correct days' do
94
+ strategy.duration(chrono)[:days].should eql 5
95
+ end
76
96
 
77
97
  it 'is a hash with the correct hours' do
78
98
  strategy.duration(chrono)[:hours].should eql 1
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chronological
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0beta3
4
+ version: 1.0.0beta4
5
5
  prerelease: 5
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-10-31 00:00:00.000000000 Z
13
+ date: 2012-11-05 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rspec