easy_timer 0.9.2 → 0.9.3
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/lib/easy_timer/version.rb +1 -1
- data/lib/easy_timer.rb +13 -13
- data/spec/easy_timer_spec.rb +2 -2
- metadata +1 -1
data/lib/easy_timer/version.rb
CHANGED
data/lib/easy_timer.rb
CHANGED
@@ -5,7 +5,7 @@ module EasyTimer
|
|
5
5
|
base.extend(ClassMethods)
|
6
6
|
end
|
7
7
|
|
8
|
-
|
8
|
+
module ClassMethods
|
9
9
|
def timer(options = {}, &block)
|
10
10
|
start_time = Time.now
|
11
11
|
yield
|
@@ -16,24 +16,24 @@ module EasyTimer
|
|
16
16
|
return total_time
|
17
17
|
end
|
18
18
|
end
|
19
|
-
|
19
|
+
end
|
20
20
|
|
21
|
-
def verbose
|
21
|
+
def verbose(options = {:msec => true})
|
22
22
|
time_hash = {}
|
23
23
|
seconds = self.to_i
|
24
|
-
|
25
|
-
time_hash[:
|
26
|
-
time_hash[:
|
27
|
-
time_hash[:
|
28
|
-
time_hash[:
|
29
|
-
time_hash[:
|
30
|
-
return time_hash.select{|k,v| v > 0}.inject([])
|
24
|
+
time_hash[:week], seconds = seconds.divmod(604800)
|
25
|
+
time_hash[:day], seconds = seconds.divmod(86400)
|
26
|
+
time_hash[:hour], seconds = seconds.divmod(3600)
|
27
|
+
time_hash[:minute], seconds = seconds.divmod(60)
|
28
|
+
time_hash[:second] = seconds
|
29
|
+
time_hash[:second] += (self.to_f - self.to_i) if options[:msec]
|
30
|
+
return time_hash.select{|k,v| v > 0}.inject([]) do |verbose, array|
|
31
31
|
k, v = array
|
32
|
-
verbose << "#{v.is_a?(Float) ? sprintf("%.2f", v) : v} #{v
|
33
|
-
|
32
|
+
verbose << "#{v.is_a?(Float) ? sprintf("%.2f", v) : v} #{k.to_s + (v == 1 ? '' : 's')}"
|
33
|
+
end.join(" ")
|
34
34
|
end
|
35
35
|
end
|
36
36
|
|
37
37
|
class Time
|
38
38
|
include EasyTimer
|
39
|
-
end
|
39
|
+
end
|
data/spec/easy_timer_spec.rb
CHANGED
@@ -14,8 +14,8 @@ describe "timer instance" do
|
|
14
14
|
expect(Time.at(1389722.22).verbose).to eql "2 weeks 2 days 2 hours 2 minutes 2.22 seconds"
|
15
15
|
end
|
16
16
|
|
17
|
-
it "should show
|
18
|
-
expect(Time.timer{sleep
|
17
|
+
it "should show 0.55 seconds" do
|
18
|
+
expect(Time.timer{sleep 0.55}.verbose).to eql "0.55 seconds"
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|