eotb 0.5.9 → 0.5.10

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.
Files changed (4) hide show
  1. data/VERSION +1 -1
  2. data/eotb.gemspec +3 -2
  3. data/lib/timer.rb +67 -0
  4. metadata +5 -4
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.9
1
+ 0.5.10
data/eotb.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{eotb}
8
- s.version = "0.5.9"
8
+ s.version = "0.5.10"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Ragnarson"]
12
- s.date = %q{2010-09-15}
12
+ s.date = %q{2010-09-19}
13
13
  s.description = %q{Rails plugin which allow you easily track and observe your apps}
14
14
  s.email = %q{bartlomiej.kozal@ragnarson.com}
15
15
  s.extra_rdoc_files = [
@@ -32,6 +32,7 @@ Gem::Specification.new do |s|
32
32
  "lib/eotb.rb",
33
33
  "lib/generators/eotb/USAGE",
34
34
  "lib/generators/eotb/eotb_generator.rb",
35
+ "lib/timer.rb",
35
36
  "spec/eotb_spec.rb",
36
37
  "spec/spec_helper.rb"
37
38
  ]
data/lib/timer.rb ADDED
@@ -0,0 +1,67 @@
1
+ # Ruby license. Copyright (C)2004-2008 Joel VanderWerf.
2
+ # Contact mailto:vjoel@users.sourceforge.net.
3
+ #
4
+ # A lightweight, non-drifting, self-correcting timer. Average error is bounded
5
+ # as long as, on average, there is enough time to complete the work done, and
6
+ # the timer is checked often enough. It is lightweight in the sense that no
7
+ # threads are created. Can be used either as an internal iterator (Timer.every)
8
+ # or as an external iterator (Timer.new). Obviously, the GC can cause a
9
+ # temporary slippage.
10
+
11
+ class Timer
12
+ # Yields to the supplied block every +period+ seconds. The value yielded is
13
+ # the total elapsed time (an instance of +Time+). If +expire+ is given, then
14
+ # #every returns after that amount of elapsed time.
15
+ def Timer.every(period, expire = nil)
16
+ target = time_start = Time.now
17
+ loop do
18
+ elapsed = Time.now - time_start
19
+ break if expire and elapsed > expire
20
+ yield elapsed
21
+ target += period
22
+ error = target - Time.now
23
+ sleep error if error > 0
24
+ end
25
+ end
26
+
27
+ # Make a Timer that can be checked when needed, using #wait or #if_ready. The
28
+ # advantage over Timer.every is that the timer can be checked on separate
29
+ # passes through a loop.
30
+ def initialize(period = 1)
31
+ @period = period
32
+ restart
33
+ end
34
+
35
+ attr_accessor :period
36
+
37
+ # Call this to restart the timer after a period of inactivity (e.g., the user
38
+ # hits the pause button, and then hits the go button).
39
+ def restart
40
+ @target = @time_start = Time.now
41
+ end
42
+
43
+ # Time on timer since instantiation or last #restart.
44
+ def elapsed
45
+ Time.now - @time_start
46
+ end
47
+
48
+ # Wait for the next cycle, if time remains in the current cycle. Otherwise,
49
+ # return immediately to caller.
50
+ def wait(per = nil)
51
+ @target += per || @period
52
+ error = @target - Time.now
53
+ sleep error if error > 0
54
+ true
55
+ end
56
+
57
+ # Yield to the block if no time remains in cycle. Otherwise, return
58
+ # immediately to caller
59
+ def if_ready
60
+ error = @target + @period - Time.now
61
+ if error <= 0
62
+ @target += @period
63
+ elapsed = Time.now - @time_start
64
+ yield elapsed
65
+ end
66
+ end
67
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eotb
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 31
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 5
9
- - 9
10
- version: 0.5.9
9
+ - 10
10
+ version: 0.5.10
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ragnarson
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-09-15 00:00:00 +02:00
18
+ date: 2010-09-19 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -107,6 +107,7 @@ files:
107
107
  - lib/eotb.rb
108
108
  - lib/generators/eotb/USAGE
109
109
  - lib/generators/eotb/eotb_generator.rb
110
+ - lib/timer.rb
110
111
  - spec/eotb_spec.rb
111
112
  - spec/spec_helper.rb
112
113
  has_rdoc: true