easy_timer 0.9.3 → 0.9.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/easy_timer.rb +11 -4
- data/lib/easy_timer/version.rb +1 -1
- data/spec/easy_timer_spec.rb +16 -8
- metadata +2 -2
data/lib/easy_timer.rb
CHANGED
@@ -11,25 +11,32 @@ module EasyTimer
|
|
11
11
|
yield
|
12
12
|
total_time = Time.at(Time.now - start_time)
|
13
13
|
if options[:verbose] || options[:v]
|
14
|
-
return total_time.verbose
|
14
|
+
return total_time.verbose
|
15
15
|
else
|
16
16
|
return total_time
|
17
17
|
end
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
|
-
def verbose
|
21
|
+
def verbose
|
22
22
|
time_hash = {}
|
23
23
|
seconds = self.to_i
|
24
|
+
|
25
|
+
precision = case seconds
|
26
|
+
when 0..30 then 3
|
27
|
+
when 31..360 then 2
|
28
|
+
else 0
|
29
|
+
end
|
30
|
+
|
24
31
|
time_hash[:week], seconds = seconds.divmod(604800)
|
25
32
|
time_hash[:day], seconds = seconds.divmod(86400)
|
26
33
|
time_hash[:hour], seconds = seconds.divmod(3600)
|
27
34
|
time_hash[:minute], seconds = seconds.divmod(60)
|
28
35
|
time_hash[:second] = seconds
|
29
|
-
time_hash[:second] += (self.to_f - self.to_i) if
|
36
|
+
time_hash[:second] += (self.to_f - self.to_i) if precision > 0
|
30
37
|
return time_hash.select{|k,v| v > 0}.inject([]) do |verbose, array|
|
31
38
|
k, v = array
|
32
|
-
verbose << "#{v.is_a?(Float) ? sprintf("
|
39
|
+
verbose << "#{v.is_a?(Float) ? sprintf("%.#{precision}f", v) : v} #{k.to_s + (v == 1 ? '' : 's')}"
|
33
40
|
end.join(" ")
|
34
41
|
end
|
35
42
|
end
|
data/lib/easy_timer/version.rb
CHANGED
data/spec/easy_timer_spec.rb
CHANGED
@@ -7,28 +7,36 @@ describe "timer instance" do
|
|
7
7
|
end
|
8
8
|
|
9
9
|
it "should be a singular verbose time" do
|
10
|
-
expect(verbose_time).to eql "1 week 1 day 1 hour 1 minute 1
|
10
|
+
expect(verbose_time).to eql "1 week 1 day 1 hour 1 minute 1 second"
|
11
11
|
end
|
12
12
|
|
13
|
-
it "
|
14
|
-
expect(Time.at(1389722.22).verbose).to eql "2 weeks 2 days 2 hours 2 minutes 2
|
13
|
+
it "should be a plural verbose time" do
|
14
|
+
expect(Time.at(1389722.22).verbose).to eql "2 weeks 2 days 2 hours 2 minutes 2 seconds"
|
15
15
|
end
|
16
16
|
|
17
|
-
it "should
|
18
|
-
expect(Time.
|
17
|
+
it "should display 3 decimal places" do
|
18
|
+
expect(Time.at(22.222).verbose).to eql "22.222 seconds"
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should display 2 decimal places" do
|
22
|
+
expect(Time.at(60.222).verbose).to eql "1 minute 0.22 seconds"
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should display no decimal places" do
|
26
|
+
expect(Time.at(361.222).verbose).to eql "6 minutes 1 second"
|
19
27
|
end
|
20
28
|
end
|
21
29
|
|
22
30
|
describe "Timer" do
|
23
31
|
before :all do
|
24
|
-
@five = Time.timer{sleep
|
32
|
+
@five = Time.timer{sleep 2}
|
25
33
|
end
|
26
34
|
|
27
35
|
it "should return a Time object" do
|
28
36
|
expect(@five.class).to eql Time
|
29
37
|
end
|
30
38
|
|
31
|
-
it "should return
|
32
|
-
expect(@five.to_f).to be_within(0.1).of(
|
39
|
+
it "should return 2 seconds" do
|
40
|
+
expect(@five.to_f).to be_within(0.1).of(2)
|
33
41
|
end
|
34
42
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: easy_timer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-10-
|
12
|
+
date: 2012-10-12 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Easy Timer
|
15
15
|
email:
|