chronological 1.0.0beta5 → 1.0.0beta6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md
CHANGED
|
@@ -282,19 +282,19 @@ MyTimeRangeClass.ended :as_of => 42.years.from_now #=> [range_instance]
|
|
|
282
282
|
|
|
283
283
|
When calling `duration` on a Chronological, by default it will return
|
|
284
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 `:
|
|
285
|
+
combination of these pieces, you can pass them in an `:in` options like
|
|
286
286
|
so:
|
|
287
287
|
|
|
288
288
|
```ruby
|
|
289
289
|
# range_instance's total duration is 5 days, 1 hour, 44 minutes and 23 seconds
|
|
290
290
|
|
|
291
|
-
range_instance.duration :
|
|
291
|
+
range_instance.duration :in => [:days, :hours, :seconds]
|
|
292
292
|
#=> { :days => 5, :hours => 1, :seconds => 2663 }
|
|
293
293
|
|
|
294
|
-
range_instance.duration :
|
|
294
|
+
range_instance.duration :in => [:days, :hours, :minutes]
|
|
295
295
|
#=> { :days => 5, :hours => 1, :minutes => 44 }
|
|
296
296
|
|
|
297
|
-
range_instance.duration :
|
|
297
|
+
range_instance.duration :in => [:hours, :minutes, :seconds]
|
|
298
298
|
#=> { :hours => 121, :minutes => 44, :seconds => 23 }
|
|
299
299
|
```
|
|
300
300
|
|
|
@@ -34,6 +34,8 @@ module Chronological
|
|
|
34
34
|
end
|
|
35
35
|
|
|
36
36
|
def duration_in_seconds(object)
|
|
37
|
+
return nil unless object.send(field_names[:ending_time]).present? && object.send(field_names[:starting_time]).present?
|
|
38
|
+
|
|
37
39
|
(object.send(field_names[:ending_time]) - object.send(field_names[:starting_time]))
|
|
38
40
|
end
|
|
39
41
|
|
|
@@ -22,8 +22,8 @@ module Chronological
|
|
|
22
22
|
!object.in_progress?
|
|
23
23
|
end
|
|
24
24
|
|
|
25
|
-
def duration(object, options = { :
|
|
26
|
-
duration_parts = options[:
|
|
25
|
+
def duration(object, options = { :in => [:days, :hours, :minutes, :seconds] })
|
|
26
|
+
duration_parts = options[:in]
|
|
27
27
|
remaining_duration = duration_in_seconds(object)
|
|
28
28
|
|
|
29
29
|
return Hash.new unless remaining_duration.present?
|
|
@@ -613,5 +613,28 @@ describe Chronological::AbsoluteStrategy, :timecop => true do
|
|
|
613
613
|
end
|
|
614
614
|
end
|
|
615
615
|
end
|
|
616
|
+
|
|
617
|
+
describe '#duration_in_seconds' do
|
|
618
|
+
let(:strategy) { Chronological::AbsoluteStrategy.new( starting_time: :started_at_utc,
|
|
619
|
+
ending_time: :ended_at_utc) }
|
|
620
|
+
|
|
621
|
+
context 'when the starting time is not set' do
|
|
622
|
+
let(:start_time) { nil }
|
|
623
|
+
let(:end_time) { now }
|
|
624
|
+
|
|
625
|
+
it 'is nil' do
|
|
626
|
+
strategy.send(:duration_in_seconds, chronologicable).should be_nil
|
|
627
|
+
end
|
|
628
|
+
end
|
|
629
|
+
|
|
630
|
+
context 'when the starting time is not set' do
|
|
631
|
+
let(:start_time) { now }
|
|
632
|
+
let(:end_time) { nil }
|
|
633
|
+
|
|
634
|
+
it 'is nil' do
|
|
635
|
+
strategy.send(:duration_in_seconds, chronologicable).should be_nil
|
|
636
|
+
end
|
|
637
|
+
end
|
|
638
|
+
end
|
|
616
639
|
end
|
|
617
640
|
end
|
|
@@ -72,7 +72,7 @@ describe Chronological::BaseStrategy do
|
|
|
72
72
|
|
|
73
73
|
describe '#duration' do
|
|
74
74
|
context 'when passed an options hash limiting the parts of the output' do
|
|
75
|
-
let(:duration_hash) { strategy.duration(chrono, :
|
|
75
|
+
let(:duration_hash) { strategy.duration(chrono, :in => [:days, :hours, :seconds]) }
|
|
76
76
|
|
|
77
77
|
before { strategy.should_receive(:duration_in_seconds).and_return(438263) }
|
|
78
78
|
|