clockblock 0.0.1

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.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in clockblock.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Jack A Ross
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,4 @@
1
+ clockblock
2
+ ==========
3
+
4
+ Wrap your code in a Clock Block to measure execution duration.
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.libs << 'lib/clockblock'
8
+ t.test_files = FileList['spec/lib/clockblock/*_spec.rb']
9
+ t.verbose = true
10
+ end
11
+
12
+ task :default => :test
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/clockblock/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Jack A Ross"]
6
+ gem.email = ["jack.ross@technekes.com"]
7
+ gem.description = %q{Enables easy and DRY code timing capabilities.}
8
+ gem.summary = %q{Wrap your code in a Clock Block to measure execution duration.}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "clockblock"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Clockblock::VERSION
17
+ gem.add_development_dependency "rake"
18
+ gem.add_development_dependency "minitest"
19
+ gem.add_development_dependency "turn"
20
+ end
@@ -0,0 +1,6 @@
1
+ require "clockblock/version"
2
+ require "clockblock/timer"
3
+
4
+ module Clockblock
5
+ # Your code goes here...
6
+ end
@@ -0,0 +1,62 @@
1
+ module Clockblock
2
+ class Timer
3
+ attr_reader :started_at, :finished_at, :stage
4
+
5
+ def initialize(opts = {})
6
+ @stage = :initialized
7
+ end
8
+
9
+ def clock(name = "Anonymous Timer")
10
+ begin
11
+ start
12
+ @stage = :executing
13
+ result = yield
14
+ stop
15
+ rescue => ex
16
+ @stage = :error
17
+ raise TimerException.new("Error while executing '#{name}': '#{ex.message}'!")
18
+ end
19
+ result
20
+ end
21
+
22
+ def start
23
+ @finished_at = nil
24
+ @stage = :started
25
+ @started_at = Time.now
26
+ end
27
+
28
+ def stop
29
+ @stage = :finished
30
+ @finished_at = Time.now
31
+ end
32
+
33
+ def finish
34
+ stop
35
+ end
36
+
37
+ def duration
38
+ begin
39
+ @finished_at - @started_at
40
+ rescue
41
+ nil
42
+ end
43
+ end
44
+
45
+ def attributes
46
+ { :started_at => started_at, :finished_at => finished_at, :duration => duration, :stage => stage }
47
+ end
48
+
49
+ def inspect
50
+ attributes
51
+ end
52
+
53
+ def to_s
54
+ attributes
55
+ end
56
+
57
+ class TimerException < Exception
58
+ end
59
+
60
+ end
61
+
62
+ end
@@ -0,0 +1,3 @@
1
+ module Clockblock
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,128 @@
1
+ require_relative '../../spec_helper'
2
+
3
+ describe Clockblock::Timer do
4
+
5
+ subject { Clockblock::Timer.new }
6
+
7
+ describe "timer characteristics" do
8
+
9
+ it "must be able to start" do
10
+ subject.must_respond_to(:start)
11
+ end
12
+
13
+ it "must be able to stop" do
14
+ subject.must_respond_to(:stop)
15
+ end
16
+
17
+ it "must have a started_at accessor" do
18
+ subject.must_respond_to(:started_at)
19
+ end
20
+
21
+ it "must have a finished_at accessor" do
22
+ subject.must_respond_to(:finished_at)
23
+ end
24
+
25
+ it "must have a duration accessor" do
26
+ subject.must_respond_to(:duration)
27
+ end
28
+
29
+ it "must be able to clock" do
30
+ subject.must_respond_to(:clock)
31
+ end
32
+
33
+ it "must have an attributes accessor" do
34
+ subject.must_respond_to(:attributes)
35
+ end
36
+
37
+ it "must have a stage accessor" do
38
+ subject.must_respond_to(:stage)
39
+ end
40
+
41
+ end
42
+
43
+ describe "starting the timer" do
44
+ before do
45
+ @start_return_value = subject.start
46
+ end
47
+
48
+ let(:start_return_value) { @start_return_value }
49
+
50
+ it "must return Time" do
51
+ start_return_value.must_be_kind_of Time
52
+ end
53
+
54
+ it "must return the start time through the started_at accessor" do
55
+ start_return_value.must_equal subject.started_at
56
+ end
57
+
58
+ it "must reset finished at and duration when it starts" do
59
+ subject.finished_at.must_be_nil
60
+ subject.duration.must_be_nil
61
+ end
62
+
63
+ it "must have a stage of :started" do
64
+ subject.stage.must_equal :started
65
+ end
66
+
67
+ end
68
+
69
+ describe "stopping the timer" do
70
+ before do
71
+ subject.start
72
+ sleep 0.1
73
+ @stop_return_value = subject.stop
74
+ end
75
+
76
+ let(:started_at) { subject.started_at }
77
+ let(:stop_return_value) { @stop_return_value }
78
+ let(:finished_at) { subject.finished_at }
79
+
80
+ it "must return an instance of Time" do
81
+ stop_return_value.must_be_kind_of Time
82
+ end
83
+
84
+ it "must return the finish time through the finished_at accessor" do
85
+ stop_return_value.must_equal finished_at
86
+ end
87
+
88
+ it "must have a stage of :finished" do
89
+ subject.stage.must_equal :finished
90
+ end
91
+
92
+ specify "duration must be equal to the difference between start and finish" do
93
+ subject.duration.must_equal finished_at - started_at
94
+ end
95
+
96
+ end
97
+
98
+ describe "timer attributes" do
99
+ let(:attributes) { subject.attributes }
100
+
101
+ it "must be a hash" do
102
+ attributes.must_be_instance_of(Hash)
103
+ end
104
+
105
+ it "must have :started_at, :finished_at and :duration keys" do
106
+ attributes.must_include(:started_at)
107
+ attributes.must_include(:finished_at)
108
+ attributes.must_include(:duration)
109
+ attributes.must_include(:stage)
110
+ end
111
+
112
+ end
113
+
114
+ describe "clocking a code block" do
115
+
116
+ it "must return the result of the block" do
117
+ expected = Random.rand(1)
118
+ subject.clock{ expected }.must_equal expected
119
+ end
120
+
121
+ specify "stage must equal :error if the code block raises an error" do
122
+ lambda{ subject.clock { raise } }.must_raise Clockblock::Timer::TimerException
123
+ subject.stage.must_equal :error
124
+ end
125
+
126
+ end
127
+
128
+ end
@@ -0,0 +1,8 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/pride'
3
+
4
+ begin; require 'turn/autorun'; rescue LoadError; end
5
+
6
+ require File.expand_path('../../lib/clockblock.rb', __FILE__)
7
+
8
+ Turn.config.format = :pretty
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: clockblock
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jack A Ross
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: &70101676678120 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70101676678120
25
+ - !ruby/object:Gem::Dependency
26
+ name: minitest
27
+ requirement: &70101676677580 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70101676677580
36
+ - !ruby/object:Gem::Dependency
37
+ name: turn
38
+ requirement: &70101676676840 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70101676676840
47
+ description: Enables easy and DRY code timing capabilities.
48
+ email:
49
+ - jack.ross@technekes.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - Gemfile
56
+ - LICENSE
57
+ - README.md
58
+ - Rakefile
59
+ - clockblock.gemspec
60
+ - lib/clockblock.rb
61
+ - lib/clockblock/timer.rb
62
+ - lib/clockblock/version.rb
63
+ - spec/lib/clockblock/timer_spec.rb
64
+ - spec/spec_helper.rb
65
+ homepage: ''
66
+ licenses: []
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 1.8.15
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: Wrap your code in a Clock Block to measure execution duration.
89
+ test_files:
90
+ - spec/lib/clockblock/timer_spec.rb
91
+ - spec/spec_helper.rb