timer 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/Rakefile +8 -33
  2. data/VERSION +1 -1
  3. data/lib/timer.rb +24 -22
  4. data/spec/timer_spec.rb +116 -0
  5. data/timer.gemspec +12 -2
  6. metadata +25 -4
data/Rakefile CHANGED
@@ -6,12 +6,14 @@ begin
6
6
  Jeweler::Tasks.new do |gem|
7
7
  gem.name = "timer"
8
8
  gem.summary = %Q{Simple timer to perform a block and display the elapsed time}
9
- gem.description = %Q{Simple timer to perform a block and display the elapsed time}
9
+ gem.description = %Q{Simple timer to perform a block and display the elapsed time. Growls message if growl is turned on.}
10
10
  gem.email = "progressions@gmail.com"
11
11
  gem.homepage = "http://github.com/progressions/timer"
12
12
  gem.authors = ["Jeff Coleman"]
13
13
  gem.add_development_dependency "ruby-growl", ">= 0"
14
14
  gem.add_development_dependency "progressions-g", ">= 0"
15
+ gem.add_development_dependency "rspec", ">= 0"
16
+ gem.add_development_dependency "timecop", ">= 0"
15
17
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
18
  end
17
19
  Jeweler::GemcutterTasks.new
@@ -19,36 +21,9 @@ rescue LoadError
19
21
  puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
22
  end
21
23
 
22
- require 'rake/testtask'
23
- Rake::TestTask.new(:test) do |test|
24
- test.libs << 'lib' << 'test'
25
- test.pattern = 'test/**/test_*.rb'
26
- test.verbose = true
27
- end
28
-
29
- begin
30
- require 'rcov/rcovtask'
31
- Rcov::RcovTask.new do |test|
32
- test.libs << 'test'
33
- test.pattern = 'test/**/test_*.rb'
34
- test.verbose = true
35
- end
36
- rescue LoadError
37
- task :rcov do
38
- abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
39
- end
40
- end
41
-
42
- task :test => :check_dependencies
43
-
44
- task :default => :test
45
-
46
- require 'rake/rdoctask'
47
- Rake::RDocTask.new do |rdoc|
48
- version = File.exist?('VERSION') ? File.read('VERSION') : ""
49
-
50
- rdoc.rdoc_dir = 'rdoc'
51
- rdoc.title = "timer #{version}"
52
- rdoc.rdoc_files.include('README*')
53
- rdoc.rdoc_files.include('lib/**/*.rb')
24
+ require 'spec/rake/spectask'
25
+ desc 'run all specs'
26
+ Spec::Rake::SpecTask.new do |t|
27
+ t.spec_files = FileList['spec/**/*_spec.rb']
28
+ t.spec_opts = ['-c']
54
29
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
data/lib/timer.rb CHANGED
@@ -2,43 +2,42 @@ require 'rubygems'
2
2
  require 'g'
3
3
 
4
4
  class Timer
5
- attr_accessor :timer_title, :growl
5
+ attr_accessor :title, :growl
6
6
 
7
7
  def initialize(options={})
8
- @timer_title = options[:title]
9
- if options[:growl] == nil
10
- options[:growl] = true
8
+ @title = options[:title]
9
+ @growl = true
10
+ if options.has_key?(:growl)
11
+ @growl = options[:growl]
11
12
  end
12
- @growl = options[:growl]
13
13
  end
14
14
 
15
15
  def time(message="", options={})
16
- title = options[:title] || timer_title
16
+ current_title = options[:title] || title
17
17
 
18
18
  start_time = Time.now
19
19
  yield
20
20
  end_time = Time.now
21
- put_elapsed_time(start_time, end_time, message, title)
21
+ put_elapsed_time(start_time, end_time, message, current_title)
22
22
  end
23
23
 
24
- private
25
-
26
24
  def growl?
27
25
  @growl
28
26
  end
27
+
28
+ private
29
29
 
30
- def put_elapsed_time(start_time, end_time, message="", title=nil)
30
+ def put_elapsed_time(start_time, end_time, message="", current_title=nil)
31
31
  elapsed_time = format_time(start_time, end_time)
32
32
 
33
- puts
34
33
  e = ""
35
34
  if message.to_s.strip != ""
36
35
  message += ". "
37
36
  end
38
37
  e = message + e
39
38
  e += "Elapsed time: #{elapsed_time}"
40
- puts e
41
- growl(e, title)
39
+ $stdout.puts "\n" + e
40
+ growl(e, current_title)
42
41
  end
43
42
 
44
43
  def format_time(start_time, end_time=nil)
@@ -47,23 +46,26 @@ class Timer
47
46
  if seconds > 59
48
47
  minutes = seconds / 60.0
49
48
  seconds = (minutes - minutes.to_i) * 60
49
+ m = minutes.to_i
50
+ s = seconds.to_i
50
51
 
51
- m = minutes.to_i > 1 ? "minutes" : "minute"
52
- elapsed_time = "#{minutes.to_i} #{m}"
53
- if seconds > 0
54
- s = seconds.to_i > 1 ? "seconds" : "minutes"
55
- elapsed_time += ", #{seconds.to_i} #{s}"
52
+ minutes_msg = m != 1 ? "minutes" : "minute"
53
+ elapsed_time = "#{m} #{minutes_msg}"
54
+ if s > 0
55
+ seconds_msg = s != 1 ? "seconds" : "second"
56
+ elapsed_time += ", #{s} #{seconds_msg}"
56
57
  end
57
58
  else
58
- s = seconds.to_i > 1 ? "seconds" : "minutes"
59
+ s = seconds.to_i != 1 ? "seconds" : "second"
59
60
  elapsed_time = "#{seconds.to_i} #{s}"
60
- end
61
+ end
62
+ elapsed_time
61
63
  end
62
64
 
63
- def growl(message, title)
65
+ def growl(message, current_title)
64
66
  if growl?
65
67
  if title
66
- g(message, :title => title)
68
+ g(message, :title => current_title)
67
69
  else
68
70
  g(message)
69
71
  end
@@ -0,0 +1,116 @@
1
+ $:.unshift File.dirname(__FILE__) + '/../lib'
2
+ require 'rubygems'
3
+ require 'timecop'
4
+ require 'timer'
5
+
6
+ describe "Timer" do
7
+ before do
8
+ $stdout.stub!(:puts).as_null_object
9
+ @g = Object.new
10
+ Growl.stub(:new).and_return(@g)
11
+ @g.stub(:notify).as_null_object
12
+ end
13
+
14
+ describe "growl" do
15
+ it "should growl a message" do
16
+ @g.should_receive(:notify).with(anything, anything, /hello/, anything, anything)
17
+ Timer.new.time("hello") do
18
+ x = 1
19
+ end
20
+ end
21
+
22
+ it "should growl a title" do
23
+ @g.should_receive(:notify).with(anything, /word/, anything, anything, anything)
24
+ Timer.new(:title => "word").time("hello") do
25
+ x = 1
26
+ end
27
+ end
28
+
29
+ it "should turn off growl" do
30
+ @timer = Timer.new(:growl => false)
31
+ @timer.growl?.should be_false
32
+ end
33
+
34
+ it "shouldn't growl a message" do
35
+ Growl.should_not_receive(:new)
36
+ Timer.new(:growl => false).time("hello") do
37
+ x = 1
38
+ end
39
+ end
40
+
41
+ it "should fail gracefully if growl is off" do
42
+ @g.should_receive(:notify).and_raise("Connection refused: send(2)")
43
+ lambda {
44
+ Timer.new.time("hello") do
45
+ x = 1
46
+ end
47
+ }.should_not raise_error
48
+ end
49
+
50
+ it "should raise other exceptions" do
51
+ @g.should_receive(:notify).and_raise("Something else")
52
+ lambda {
53
+ Timer.new.time("hello") do
54
+ x = 1
55
+ end
56
+ }.should raise_error("Something else")
57
+ end
58
+ end
59
+
60
+ it "puts a message" do
61
+ $stdout.should_receive(:puts).with(/hello/)
62
+ Timer.new.time("hello") do
63
+ x = 1
64
+ end
65
+ end
66
+
67
+ describe "title" do
68
+ it "should set the title" do
69
+ Timer.new(:title => "YO").title.should == "YO"
70
+ end
71
+
72
+ it "shuld reset the title" do
73
+ @timer = Timer.new(:title => "YO")
74
+ @timer.title = "HEY"
75
+ @timer.title.should == "HEY"
76
+ end
77
+
78
+ it "should override the title" do
79
+ @g.should_receive(:notify).with(anything, /YEAH/, anything, anything, anything)
80
+ @timer = Timer.new(:title => "word")
81
+ @timer.time("hello", :title => "YEAH") do
82
+ x = 1
83
+ end
84
+ end
85
+ end
86
+
87
+ describe "timing the action" do
88
+ before(:each) do
89
+ Timecop.return
90
+ end
91
+
92
+ it "should report seconds" do
93
+ @g.should_receive(:notify).with(anything, anything, /Elapsed time: 3 seconds\"\n$/, anything, anything)
94
+ @timer = Timer.new
95
+ @timer.time("hello") do
96
+ Timecop.freeze(Time.now + 3)
97
+ end
98
+ end
99
+
100
+ it "should report only minutes when it's even" do
101
+ @g.should_receive(:notify).with(anything, anything, /Elapsed time: 2 minutes\"\n$/, anything, anything)
102
+ @timer = Timer.new
103
+ @timer.time("hello") do
104
+ Timecop.freeze(Time.now + 120)
105
+ end
106
+ end
107
+
108
+ it "should report minutes and seconds" do
109
+ @g.should_receive(:notify).with(anything, anything, /Elapsed time: 1 minute, 14 seconds\"\n$/, anything, anything)
110
+ @timer = Timer.new
111
+ @timer.time("hello") do
112
+ Timecop.freeze(Time.now + 74)
113
+ end
114
+ end
115
+ end
116
+ end
data/timer.gemspec CHANGED
@@ -5,12 +5,12 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{timer}
8
- s.version = "0.1.0"
8
+ s.version = "0.1.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Jeff Coleman"]
12
12
  s.date = %q{2010-01-11}
13
- s.description = %q{Simple timer to perform a block and display the elapsed time}
13
+ s.description = %q{Simple timer to perform a block and display the elapsed time. Growls message if growl is turned on.}
14
14
  s.email = %q{progressions@gmail.com}
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE",
@@ -24,6 +24,7 @@ Gem::Specification.new do |s|
24
24
  "Rakefile",
25
25
  "VERSION",
26
26
  "lib/timer.rb",
27
+ "spec/timer_spec.rb",
27
28
  "timer.gemspec"
28
29
  ]
29
30
  s.homepage = %q{http://github.com/progressions/timer}
@@ -31,6 +32,9 @@ Gem::Specification.new do |s|
31
32
  s.require_paths = ["lib"]
32
33
  s.rubygems_version = %q{1.3.5}
33
34
  s.summary = %q{Simple timer to perform a block and display the elapsed time}
35
+ s.test_files = [
36
+ "spec/timer_spec.rb"
37
+ ]
34
38
 
35
39
  if s.respond_to? :specification_version then
36
40
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
@@ -39,13 +43,19 @@ Gem::Specification.new do |s|
39
43
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
40
44
  s.add_development_dependency(%q<ruby-growl>, [">= 0"])
41
45
  s.add_development_dependency(%q<progressions-g>, [">= 0"])
46
+ s.add_development_dependency(%q<rspec>, [">= 0"])
47
+ s.add_development_dependency(%q<timecop>, [">= 0"])
42
48
  else
43
49
  s.add_dependency(%q<ruby-growl>, [">= 0"])
44
50
  s.add_dependency(%q<progressions-g>, [">= 0"])
51
+ s.add_dependency(%q<rspec>, [">= 0"])
52
+ s.add_dependency(%q<timecop>, [">= 0"])
45
53
  end
46
54
  else
47
55
  s.add_dependency(%q<ruby-growl>, [">= 0"])
48
56
  s.add_dependency(%q<progressions-g>, [">= 0"])
57
+ s.add_dependency(%q<rspec>, [">= 0"])
58
+ s.add_dependency(%q<timecop>, [">= 0"])
49
59
  end
50
60
  end
51
61
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: timer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Coleman
@@ -32,7 +32,27 @@ dependencies:
32
32
  - !ruby/object:Gem::Version
33
33
  version: "0"
34
34
  version:
35
- description: Simple timer to perform a block and display the elapsed time
35
+ - !ruby/object:Gem::Dependency
36
+ name: rspec
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ - !ruby/object:Gem::Dependency
46
+ name: timecop
47
+ type: :development
48
+ version_requirement:
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ description: Simple timer to perform a block and display the elapsed time. Growls message if growl is turned on.
36
56
  email: progressions@gmail.com
37
57
  executables: []
38
58
 
@@ -49,6 +69,7 @@ files:
49
69
  - Rakefile
50
70
  - VERSION
51
71
  - lib/timer.rb
72
+ - spec/timer_spec.rb
52
73
  - timer.gemspec
53
74
  has_rdoc: true
54
75
  homepage: http://github.com/progressions/timer
@@ -78,5 +99,5 @@ rubygems_version: 1.3.5
78
99
  signing_key:
79
100
  specification_version: 3
80
101
  summary: Simple timer to perform a block and display the elapsed time
81
- test_files: []
82
-
102
+ test_files:
103
+ - spec/timer_spec.rb