allotment 1.0.4 → 1.1.0

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.
@@ -1,42 +1,99 @@
1
- class Allotment
2
- class Stopwatch
3
- attr_reader :name, :status
4
-
5
- def initialize name = nil
6
- @stopwatch = self.class.uniqe_name
7
- @name = name || @stopwatch
8
- @status = 'running'
9
- @start_time = Time.now
10
- end
11
-
12
- def start
13
- @start_time = Time.now - (@current_time || 0)
14
- @status = 'running'
15
- end
16
-
17
- def stop
18
- @status = 'stopped'
19
- @current_time = Time.now - @start_time
20
- end
21
-
22
- def reset
23
- @start_time = Time.now
24
- @current_time = nil
25
- end
26
-
27
- def lap
28
- @new_lap = Time.now - (@lap_time || @start_time)
29
- @lap_time = Time.now
30
- return @new_lap
31
- end
32
-
33
- def split
34
- Time.now - @start_time
35
- end
36
-
37
- private
38
- def self.uniqe_name
39
- "stopwatch_" + (@id ? @id += 1 : @id = 0).to_s
40
- end
41
- end
1
+ module Allotment
2
+ # Stopwatch Class
3
+ # Strangely enough works just like a stopwatch
4
+ # @!attribute [r] name
5
+ # @return [String] the current name
6
+ # @!attribute [r] status
7
+ # @return [String] the current status (running|stopped)
8
+ #
9
+ class Stopwatch
10
+ attr_reader :name, :status
11
+
12
+ # Overwite the obj inspect
13
+ #
14
+ def inspect
15
+ "#<Stopwatch:%s>" % @status
16
+ end
17
+
18
+ # If the stopwatch is not given a name the uniqe_name class method is called
19
+ # @param name [String] choosen name
20
+ #
21
+ def initialize name = self.class.uniqe_name
22
+ @name = name
23
+ @status = 'stopped'
24
+ @sw_time = 0
25
+ end
26
+
27
+ # if the stopwatch was previously stopped
28
+ # then the current time is removed from the current time
29
+ # this means that it is in effect added to the total time
30
+ # @return [Allotment::Stopwatch] self
31
+ #
32
+ def start
33
+ if status == 'stopped'
34
+ @last_start = Time.now
35
+ @sw_start = Time.now - @sw_time
36
+ @status = 'running'
37
+ end
38
+ self
39
+ end
40
+
41
+ # sets the current_time
42
+ # this is where the start time could be Time.now - current_time
43
+ # @return [Float] the stopwatch time
44
+ #
45
+ def stop
46
+ if status == 'running'
47
+ @status = 'stopped'
48
+ @last_stop = Time.now
49
+ @sw_time = Time.now - @sw_start
50
+ else
51
+ time
52
+ end
53
+ end
54
+
55
+ # sets all times to 0
56
+ # if the timer is running then the start time is just overwritten
57
+ # if the timer is not then the start time will be over written on start
58
+ # @return [Allotment::Stopwatch] self
59
+ #
60
+ def reset
61
+ @sw_start = Time.now
62
+ @sw_time = 0
63
+ self
64
+ end
65
+
66
+ # A lap is the amount of time from the very start or from the end of the last lap
67
+ # Accumilated lap time is retained accross any stopages of the stopwatch
68
+ # @return [Float] the lap time
69
+ #
70
+ def lap
71
+ new_lap = time - (@lp_start || 0)
72
+ @lp_start = time
73
+ new_lap
74
+ end
75
+
76
+ # A split is the amount of time from the last start of the stopwatch
77
+ # it uses the sw time so it will keep accumelated time accross stoppages
78
+ # @return [Float] the split time
79
+ #
80
+ def split
81
+ @status == 'running' ? Time.now - @last_start : @last_stop - @last_start
82
+ end
83
+
84
+ # The accumelated current time on the stopwatch
85
+ # @return [Float] the current time
86
+ #
87
+ def time
88
+ @status == 'running' ? Time.now - @sw_start : @sw_time
89
+ end
90
+
91
+ private
92
+
93
+ # ensures that if a stopwatch is unnamed it has a uniqe name
94
+ # @return [String] stopwatch uniqe name
95
+ def self.uniqe_name
96
+ "stopwatch_" + (@id ? @id += 1 : @id = 0).to_s
97
+ end
98
+ end
42
99
  end
@@ -1,4 +1,6 @@
1
- class Allotment
2
- VERSION = "1.0.4".freeze
3
- DATE = "2013-09-02".freeze
1
+ module Allotment
2
+ # Current Allotment verion
3
+ VERSION = "1.1.0".freeze
4
+ # Date version created
5
+ DATE = "2013-09-24".freeze
4
6
  end
@@ -1,139 +1,129 @@
1
1
  require 'helper'
2
2
 
3
3
  describe Allotment do
4
- it "is a class" do
5
- Allotment.class.should eq Class
4
+ it "is a module" do
5
+ Allotment.class.should eq Module
6
6
  end
7
7
 
8
- describe ".record_event" do
9
- it "records the time for the block" do
10
- Allotment.record_event('my_recording 0.1') { sleep 0.01 }
11
- end
12
-
13
- it "records the time for the proc" do
14
- Allotment.record_event 'my_recording 0.2' do
15
- sleep 0.01
16
- end
17
- end
18
-
19
- it "returns a float" do
20
- result = Allotment.record_event('my_recording 0.3') { sleep 0.01 }
21
- result.class.should eq Float
8
+ describe ".on_start" do
9
+ it "stores the passed block" do
10
+ Allotment.on_start { 'test_returned_string' }
11
+ Allotment.on_start.call.should eq 'test_returned_string'
22
12
  end
13
+ end
23
14
 
24
- it "returns the execute time of the block" do
25
- result = Allotment.record_event('my_recording 0.4') { sleep 0.01 }
26
- result.round(2).should eq 0.01
27
- end
28
-
29
- it "returns still records the performance upon error" do
30
- expect { Allotment.record_event('failed_recording') { sleep 0.01; raise 'error' } }.to raise_error RuntimeError, "error"
31
- Allotment.results['failed_recording'].first.round(2).should eq 0.01
15
+ describe ".on_stop" do
16
+ it "stores the passed block" do
17
+ Allotment.on_stop { 'test_returned_string' }
18
+ Allotment.on_stop.call.should eq 'test_returned_string'
32
19
  end
33
20
  end
34
21
 
35
- describe ".start_recording" do
22
+ describe ".start" do
36
23
  it "returns a stopwatch instance" do
37
- result = Allotment.start_recording
24
+ result = Allotment.start
38
25
  result.class.should eq Allotment::Stopwatch
39
26
  end
40
27
 
41
28
  it "returns an unnamed stopwatch if no name given" do
42
- result = Allotment.start_recording
43
- result.name.should eq 'unnamed'
29
+ result = Allotment.start
30
+ result.name.should eq 'unnamed_recording'
44
31
  end
45
32
 
46
33
  it "returns a stopwatch with the given name" do
47
- result = Allotment.start_recording 'my_recording'
48
- result.name.should eq 'my_recording'
34
+ result = Allotment.start 'my_recording 1.1'
35
+ result.name.should eq 'my_recording 1.1'
49
36
  end
50
37
 
51
38
  it "returns a stopwatch that is 'running'" do
52
- result = Allotment.start_recording
39
+ result = Allotment.start
53
40
  result.status.should eq 'running'
54
41
  end
42
+
43
+ it "runs the on_start block" do
44
+ Allotment.on_start { @start_test = 'successful' }
45
+ @start_test.should eq nil
46
+ Allotment.start
47
+ @start_test.should eq 'successful'
48
+ end
55
49
  end
56
50
 
57
- describe ".stop_recording" do
51
+ describe ".stop" do
58
52
  it "returns a float" do
59
- Allotment.start_recording 'my_recording1'
53
+ Allotment.start 'my_recording 2.1'
60
54
  sleep 0.01
61
- result = Allotment.stop_recording 'my_recording1'
55
+ result = Allotment.stop 'my_recording 2.1'
62
56
  result.class.should eq Float
63
57
  end
64
58
 
65
59
  it "returns the execute time of the code" do
66
- Allotment.start_recording 'my_recording2'
60
+ Allotment.start 'my_recording 2.2'
67
61
  sleep 0.01
68
- result = Allotment.stop_recording 'my_recording2'
62
+ result = Allotment.stop 'my_recording 2.2'
69
63
  result.round(2).should eq 0.01
70
64
  end
71
65
 
72
66
  it "raises an error if the recording does not exist" do
73
- expect { Allotment.stop_recording 'my_recording3' }.to raise_error NameError, "No recording:my_recording3"
67
+ expect { Allotment.stop 'my_recording 2.3' }.to raise_error NameError, "Unknown recording:my_recording 2.3"
68
+ end
69
+
70
+ it "runs the on_stop block" do
71
+ Allotment.start 'my_recording 2.4'
72
+ Allotment.on_stop { @stop_test = 'successful' }
73
+ @stop_test.should eq nil
74
+ Allotment.stop 'my_recording 2.4'
75
+ @stop_test.should eq 'successful'
74
76
  end
75
77
  end
76
78
 
77
- describe ".before" do
78
- it "adds a hook to the before hooks array" do
79
- hooks = Allotment.before { nil }
80
- Allotment._hooks[:before].should eq hooks
79
+ describe ".record" do
80
+ it "records the time for the block" do
81
+ Allotment.record('my_recording 3.1') { sleep 0.01 }
81
82
  end
82
83
 
83
- it "runs the hook before the performance recording" do
84
- Allotment.before { @before = Time.now.to_f }
85
- Allotment.record_event('before') { @record = Time.now.to_f }
86
- @record.should be > Allotment.instance_variable_get("@before")
84
+ it "records the time for the proc" do
85
+ Allotment.record 'my_recording 3.2' do
86
+ sleep 0.01
87
+ end
88
+ end
89
+
90
+ it "returns a float" do
91
+ result = Allotment.record('my_recording 3.3') { sleep 0.01 }
92
+ result.class.should eq Float
87
93
  end
88
- end
89
94
 
90
- describe ".after" do
91
- it "adds a hook to the after hooks array" do
92
- hooks = Allotment.after { nil }
93
- Allotment._hooks[:after].should eq hooks
95
+ it "returns the execute time of the block" do
96
+ result = Allotment.record('my_recording 3.4') { sleep 0.01 }
97
+ result.round(2).should eq 0.01
94
98
  end
95
99
 
96
- it "runs the hook after the performance recording" do
97
- Allotment.after { @after = Time.now.to_f }
98
- Allotment.record_event('after') { @record = Time.now.to_f }
99
- @record.should be < Allotment.instance_variable_get("@after")
100
+ it "returns still records the performance upon error" do
101
+ expect { Allotment.record('my_recording 3.5') { sleep 0.01; raise 'error' } }.to raise_error RuntimeError, "error"
102
+ Allotment.results['my_recording 3.5'].first.round(2).should eq 0.01
100
103
  end
101
104
  end
102
105
 
103
106
  describe ".results" do
104
107
  it "returns a hash" do
105
- Allotment.record_event('my_recording4') { sleep 0.01 }
108
+ Allotment.record('my_recording 4.1') { sleep 0.01 }
106
109
  Allotment.results.class.should eq Hashie::Mash
107
110
  end
108
111
 
109
112
  it "returns a hash with the event in" do
110
- Allotment.record_event('my_recording5') { sleep 0.01 }
111
- Allotment.results.should include('my_recording5')
113
+ Allotment.record('my_recording 4.2') { sleep 0.01 }
114
+ Allotment.results.should include('my_recording 4.2')
112
115
  end
113
116
 
114
117
  it "stores the result under the event" do
115
- Allotment.record_event('my_recording6') { sleep 0.01 }
116
- Allotment.results['my_recording6'][0].round(2).should eq 0.01
118
+ Allotment.record('my_recording 4.3') { sleep 0.01 }
119
+ Allotment.results['my_recording 4.3'][0].round(2).should eq 0.01
117
120
  end
118
121
 
119
122
  it "stores each result of each event" do
120
- Allotment.record_event('my_recording7') { sleep 0.01 }
121
- Allotment.record_event('my_recording7') { sleep 0.02 }
122
- Allotment.results['my_recording7'][0].round(2).should eq 0.01
123
- Allotment.results['my_recording7'][1].round(2).should eq 0.02
124
- end
125
- end
126
-
127
- describe ".results_string" do
128
- it "returns a string" do
129
- Allotment.record_event('my_recording8') { sleep 0.01 }
130
- Allotment.results_string.class.should eq String
131
- end
132
-
133
- it "returns a string with the event in" do
134
- Allotment.record_event('my_recording9') { sleep 0.03 }
135
- Allotment.results_string.should include 'my_recording9'
136
- Allotment.results_string.should include '0.03'
123
+ Allotment.record('my_recording 4.4') { sleep 0.01 }
124
+ Allotment.record('my_recording 4.4') { sleep 0.02 }
125
+ Allotment.results['my_recording 4.4'][0].round(2).should eq 0.01
126
+ Allotment.results['my_recording 4.4'][1].round(2).should eq 0.02
137
127
  end
138
128
  end
139
129
  end
@@ -1,3 +1,5 @@
1
+ require 'helper'
2
+
1
3
  describe Array do
2
4
  describe "#average" do
3
5
  it "returns a float" do
@@ -1,11 +1,9 @@
1
1
  require 'coveralls'
2
- require 'pry'
3
-
4
- require 'allotment'
5
- require 'allotment/methods'
6
2
 
7
3
  Coveralls.wear!
8
4
 
5
+ require 'allotment'
6
+
9
7
  RSpec.configure do |config|
10
8
  config.color_enabled = true
11
9
  config.formatter = :documentation
@@ -0,0 +1,268 @@
1
+ require 'helper'
2
+
3
+ describe Allotment::Stopwatch do
4
+ it "is a class" do
5
+ Allotment::Stopwatch.class.should eq Class
6
+ end
7
+
8
+ describe ".new" do
9
+ it "returns a class of Stopwatch" do
10
+ Allotment::Stopwatch.new.class.should eq Allotment::Stopwatch
11
+ end
12
+
13
+ it "sets the stopwatch status to stopped" do
14
+ Allotment::Stopwatch.new.status.should eq 'stopped'
15
+ end
16
+
17
+ it "gives the stopwatch a name if none given" do
18
+ Allotment::Stopwatch.new.name.should =~ /stopwatch_\d+/
19
+ end
20
+
21
+ it "sets the name of the stopwatch" do
22
+ Allotment::Stopwatch.new('stopwatch').name.should eq 'stopwatch'
23
+ end
24
+ end
25
+
26
+ describe "#inspect" do
27
+ it "returns the correct inspect string when created" do
28
+ sw = Allotment::Stopwatch.new
29
+ sw.inspect.should eq "#<Stopwatch:stopped>"
30
+ end
31
+
32
+ it "returns the correct inspect string when running" do
33
+ sw = Allotment::Stopwatch.new.start
34
+ sw.inspect.should eq "#<Stopwatch:running>"
35
+ end
36
+
37
+ it "returns the correct inspect string when stopped" do
38
+ sw = Allotment::Stopwatch.new.start
39
+ sw.stop
40
+ sw.inspect.should eq "#<Stopwatch:stopped>"
41
+ end
42
+ end
43
+
44
+ describe "#start" do
45
+ it "returns and instance of self" do
46
+ Allotment::Stopwatch.new.start.class.should eq Allotment::Stopwatch
47
+ end
48
+
49
+ it "sets the stopwatch status to running" do
50
+ sw = Allotment::Stopwatch.new.start
51
+ sw.stop
52
+ sw.start
53
+ sw.status.should eq 'running'
54
+ end
55
+
56
+ it "does not change the time when called twice" do
57
+ sw = Allotment::Stopwatch.new.start
58
+ sleep 0.01
59
+ sw.start
60
+ sleep 0.01
61
+ sw.stop.round(2).should eq 0.02
62
+ end
63
+
64
+ it "keeps its stopwatch name" do
65
+ sw = Allotment::Stopwatch.new('stopwatch').start
66
+ sw.stop
67
+ sw.start
68
+ sw.name.should eq 'stopwatch'
69
+ end
70
+ end
71
+
72
+ describe "#stop" do
73
+ it "returns a float" do
74
+ sw = Allotment::Stopwatch.new.start
75
+ sw.stop.class.should eq Float
76
+ end
77
+
78
+ it "returns the correct time" do
79
+ sw = Allotment::Stopwatch.new.start
80
+ sleep 0.01
81
+ sw.stop.round(2).should eq 0.01
82
+ end
83
+
84
+ it "keeps track of total time" do
85
+ sw = Allotment::Stopwatch.new.start
86
+ sleep 0.01
87
+ sw.stop
88
+ sleep 0.01
89
+ sw.start
90
+ sleep 0.01
91
+ sw.stop.round(2).should eq 0.02
92
+ end
93
+
94
+ it "returns the correct time if called twice" do
95
+ sw = Allotment::Stopwatch.new.start
96
+ sleep 0.01
97
+ sw.stop.round(2).should eq 0.01
98
+ sleep 0.01
99
+ sw.stop.round(2).should eq 0.01
100
+ end
101
+
102
+ it "sets the stopwatch status to stopped" do
103
+ sw = Allotment::Stopwatch.new.start
104
+ sw.stop
105
+ sw.status.should eq 'stopped'
106
+ end
107
+
108
+ it "keeps its stopwatch name" do
109
+ sw = Allotment::Stopwatch.new('stopwatch').start
110
+ sw.stop
111
+ sw.name.should eq 'stopwatch'
112
+ end
113
+ end
114
+
115
+ describe "#reset" do
116
+ it "resets the stopwatch total time when stopped" do
117
+ sw = Allotment::Stopwatch.new.start
118
+ sleep 0.01
119
+ sw.stop
120
+ sw.reset
121
+ sw.start
122
+ sleep 0.01
123
+ sw.stop.round(2).should eq 0.01
124
+ end
125
+
126
+ it "resets the stopwatch total time when running" do
127
+ sw = Allotment::Stopwatch.new.start
128
+ sleep 0.01
129
+ sw.reset
130
+ sleep 0.01
131
+ sw.stop.round(2).should eq 0.01
132
+ end
133
+
134
+ it "should retuen an instance of self" do
135
+ sw = Allotment::Stopwatch.new
136
+ sw.reset.class.should eq Allotment::Stopwatch
137
+ end
138
+
139
+ it "keeps its stopwatch name" do
140
+ sw = Allotment::Stopwatch.new('stopwatch').start
141
+ sw.reset
142
+ sw.name.should eq 'stopwatch'
143
+ end
144
+ end
145
+
146
+ describe "#split" do
147
+ it "returns a float" do
148
+ sw = Allotment::Stopwatch.new.start
149
+ sw.split.class.should eq Float
150
+ end
151
+
152
+ it "returns the correct time" do
153
+ sw = Allotment::Stopwatch.new.start
154
+ sleep 0.01
155
+ sw.split.round(2).should eq 0.01
156
+ sleep 0.01
157
+ sw.split.round(2).should eq 0.02
158
+ end
159
+
160
+ it "returns the correct time when stopped" do
161
+ sw = Allotment::Stopwatch.new.start
162
+ sleep 0.02
163
+ sw.split.round(2).should eq 0.02
164
+ sw.stop
165
+ sleep 0.01
166
+ sw.split.round(2).should eq 0.02
167
+ end
168
+
169
+ it "returns the time from the last start" do
170
+ sw = Allotment::Stopwatch.new.start
171
+ sleep 0.01
172
+ sw.split.round(2).should eq 0.01
173
+ sw.stop
174
+ sleep 0.01
175
+ sw.start
176
+ sleep 0.01
177
+ sw.split.round(2).should eq 0.01
178
+ end
179
+
180
+ it "keeps its stopwatch name" do
181
+ sw = Allotment::Stopwatch.new('stopwatch').start
182
+ sw.split
183
+ sw.name.should eq 'stopwatch'
184
+ end
185
+ end
186
+
187
+ describe "#lap" do
188
+ it "returns a float" do
189
+ sw = Allotment::Stopwatch.new.start
190
+ sw.lap.class.should eq Float
191
+ end
192
+
193
+ it "returns the correct time" do
194
+ sw = Allotment::Stopwatch.new.start
195
+ sleep 0.01
196
+ sw.lap.round(2).should eq 0.01
197
+ sleep 0.01
198
+ sw.lap.round(2).should eq 0.01
199
+ end
200
+
201
+ it "returns the correct time over stop/start" do
202
+ sw = Allotment::Stopwatch.new.start
203
+ sleep 0.01
204
+ sw.lap.round(2).should eq 0.01
205
+ sw.stop
206
+ sleep 0.01
207
+ sw.start
208
+ sleep 0.01
209
+ sw.lap.round(2).should eq 0.01
210
+ end
211
+
212
+ it "returns the correct time when stopped" do
213
+ sw = Allotment::Stopwatch.new.start
214
+ sleep 0.01
215
+ sw.lap.round(2).should eq 0.01
216
+ sleep 0.01
217
+ sw.stop
218
+ sleep 0.01
219
+ sw.lap.round(2).should eq 0.01
220
+ end
221
+
222
+ it "keeps its stopwatch name" do
223
+ sw = Allotment::Stopwatch.new('stopwatch').start
224
+ sw.lap
225
+ sw.name.should eq 'stopwatch'
226
+ end
227
+ end
228
+
229
+ describe "#time" do
230
+ it "displays the current time while running" do
231
+ sw = Allotment::Stopwatch.new('stopwatch').start
232
+ sleep 0.01
233
+ sw.time.round(2).should eq 0.01
234
+ sleep 0.01
235
+ sw.time.round(2).should eq 0.02
236
+ end
237
+
238
+ it "displays the current time while stopped" do
239
+ sw = Allotment::Stopwatch.new('stopwatch').start
240
+ sleep 0.01
241
+ sw.time.round(2).should eq 0.01
242
+ sw.stop
243
+ sleep 0.01
244
+ sw.time.round(2).should eq 0.01
245
+ end
246
+
247
+ it "returns the correct time over stop/start" do
248
+ sw = Allotment::Stopwatch.new.start
249
+ sleep 0.01
250
+ sw.stop
251
+ sleep 0.01
252
+ sw.start
253
+ sleep 0.01
254
+ sw.time.round(2).should eq 0.02
255
+ end
256
+
257
+ it "displays the current time when the stopwatch has been restarted" do
258
+ sw = Allotment::Stopwatch.new('stopwatch').start
259
+ sleep 0.01
260
+ sw.stop
261
+ sw.time.round(2).should eq 0.01
262
+ sleep 0.01
263
+ sw.start
264
+ sleep 0.01
265
+ sw.time.round(2).should eq 0.02
266
+ end
267
+ end
268
+ end