easy_timer 0.9.1 → 0.9.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.DS_Store +0 -0
- data/README.md +11 -9
- data/lib/easy_timer/version.rb +1 -1
- data/lib/easy_timer.rb +26 -26
- data/spec/easy_timer_spec.rb +34 -0
- data/spec/spec_helper.rb +7 -0
- metadata +7 -2
data/.DS_Store
ADDED
Binary file
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# EasyTimer
|
2
2
|
|
3
|
-
A simple and elegant way to add timer functionality to your projects. Adds class method timer and instance method verbose to Time class.
|
3
|
+
A simple and elegant way to add timer functionality to your projects. Adds class method :timer and instance method :verbose to Time class.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -19,22 +19,24 @@ Or install it yourself as:
|
|
19
19
|
## Usage
|
20
20
|
Calling timer will return a time object.
|
21
21
|
|
22
|
-
|
22
|
+
Time.timer{sleep 1} #=> Time object
|
23
23
|
|
24
|
-
Calling verbose on a Time
|
24
|
+
Calling verbose on a Time instance will return a formatted string.
|
25
25
|
|
26
|
-
|
26
|
+
Time.timer{sleep 1}.verbose #=> 1.00 seconds
|
27
|
+
Time.at(2.weeks + 2.days + 2.hours + 2.minutes + 2.22.seconds) #=> "2 weeks 2 days 2 hours 2 minutes 2.22 seconds"
|
28
|
+
Time.at(694861.11) #=> "1 week 1 day 1 hour 1 minute 1.11 seconds"
|
27
29
|
|
28
30
|
You can also pass :v => true or :verbose => true.
|
29
31
|
|
30
|
-
|
32
|
+
Time.timer(:v => true){sleep 1} #=> 1.00 seconds
|
31
33
|
|
32
34
|
A practical usage would be:
|
33
35
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
36
|
+
puts Time.timer do
|
37
|
+
Model.update_all({:criteria => false}, {:criteria => true})
|
38
|
+
puts "All models with criteria=false have been updated."
|
39
|
+
end.verbose
|
38
40
|
|
39
41
|
|
40
42
|
## Contributing
|
data/lib/easy_timer/version.rb
CHANGED
data/lib/easy_timer.rb
CHANGED
@@ -1,39 +1,39 @@
|
|
1
1
|
require "easy_timer/version"
|
2
2
|
|
3
3
|
module EasyTimer
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
def self.included(base)
|
5
|
+
base.extend(ClassMethods)
|
6
|
+
end
|
7
7
|
|
8
8
|
module ClassMethods
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
9
|
+
def timer(options = {}, &block)
|
10
|
+
start_time = Time.now
|
11
|
+
yield
|
12
|
+
total_time = Time.at(Time.now - start_time)
|
13
|
+
if options[:verbose] || options[:v]
|
14
|
+
return total_time.verbose
|
15
|
+
else
|
16
|
+
return total_time
|
17
|
+
end
|
18
|
+
end
|
19
19
|
end
|
20
20
|
|
21
21
|
def verbose
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
22
|
+
time_hash = {}
|
23
|
+
seconds = self.to_i
|
24
|
+
milliseconds = (self.to_f - seconds)
|
25
|
+
time_hash[:weeks], seconds = seconds.divmod(604800)
|
26
|
+
time_hash[:days], seconds = seconds.divmod(86400)
|
27
|
+
time_hash[:hours], seconds = seconds.divmod(3600)
|
28
|
+
time_hash[:minutes], seconds = seconds.divmod(60)
|
29
|
+
time_hash[:seconds] = seconds + milliseconds
|
30
|
+
return time_hash.select{|k,v| v > 0}.inject([]){|verbose, array|
|
31
|
+
k, v = array
|
32
|
+
verbose << "#{v.is_a?(Float) ? sprintf("%.2f", v) : v} #{v > 1 ? k : k.to_s.chars.to_a[0..-2].join}"
|
33
|
+
}.join(" ")
|
34
34
|
end
|
35
35
|
end
|
36
36
|
|
37
37
|
class Time
|
38
|
-
|
38
|
+
include EasyTimer
|
39
39
|
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "timer instance" do
|
4
|
+
let(:verbose_time){Time.at(694861.111111).verbose}
|
5
|
+
it "should return a string" do
|
6
|
+
expect(verbose_time.class).to eql String
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should be a singular verbose time" do
|
10
|
+
expect(verbose_time).to eql "1 week 1 day 1 hour 1 minute 1.11 seconds"
|
11
|
+
end
|
12
|
+
|
13
|
+
it "shold be a plural verbose time" do
|
14
|
+
expect(Time.at(1389722.22).verbose).to eql "2 weeks 2 days 2 hours 2 minutes 2.22 seconds"
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should show 1 seconds" do
|
18
|
+
expect(Time.timer{sleep 1}.verbose).to eql "1.00 seconds"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "Timer" do
|
23
|
+
before :all do
|
24
|
+
@five = Time.timer{sleep 5}
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should return a Time object" do
|
28
|
+
expect(@five.class).to eql Time
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should return 5 seconds" do
|
32
|
+
expect(@five.to_f).to be_within(0.1).of(5)
|
33
|
+
end
|
34
|
+
end
|
data/spec/spec_helper.rb
ADDED
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.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -18,6 +18,7 @@ executables: []
|
|
18
18
|
extensions: []
|
19
19
|
extra_rdoc_files: []
|
20
20
|
files:
|
21
|
+
- .DS_Store
|
21
22
|
- .gitignore
|
22
23
|
- Gemfile
|
23
24
|
- LICENSE
|
@@ -26,6 +27,8 @@ files:
|
|
26
27
|
- easy_timer.gemspec
|
27
28
|
- lib/easy_timer.rb
|
28
29
|
- lib/easy_timer/version.rb
|
30
|
+
- spec/easy_timer_spec.rb
|
31
|
+
- spec/spec_helper.rb
|
29
32
|
homepage: https://github.com/isaacsloan/easy_timer
|
30
33
|
licenses: []
|
31
34
|
post_install_message:
|
@@ -50,4 +53,6 @@ rubygems_version: 1.8.24
|
|
50
53
|
signing_key:
|
51
54
|
specification_version: 3
|
52
55
|
summary: Adds easy to use timer method to Time class.
|
53
|
-
test_files:
|
56
|
+
test_files:
|
57
|
+
- spec/easy_timer_spec.rb
|
58
|
+
- spec/spec_helper.rb
|