ActionTimer 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,5 +1,9 @@
1
1
  = CHANGELOG
2
2
 
3
+ == 0.1.1
4
+ * fix for float calculations
5
+ * downcased gem name
6
+
3
7
  == 0.1.0
4
8
  * remove excess exceptions
5
9
  * use logger directly
data/README.rdoc CHANGED
@@ -2,41 +2,110 @@
2
2
 
3
3
  ActionTimer is a helper for timed events. It allows for single and recurring actions to be executed in an efficient manner. It makes use of a single thread to keep time on registered actions and uses an ActionPool to execute actions. Simple and effective.
4
4
 
5
+
5
6
  === install (easy):
6
7
 
7
- gem install ActionTimer
8
+ gem install actiontimer
8
9
 
9
10
  === install (less easy):
10
11
 
11
12
  git clone http://github.com/spox/actiontimer.git
12
- cd actiontimer
13
- gem build actiontimer.gemspec
14
- gem install ./
13
+ cd actiontimer && gem build *.gemspec && gem install ./
15
14
 
16
- === Simple example of using the timer:
15
+ === install (less easy that's a little easier)
17
16
 
18
- require 'actiontimer'
17
+ {rip}[http://hellorip.com/about.html] makes it easy to install directly from a github repository.
19
18
 
20
- timer = ActionTimer::Timer.new
19
+ === Using the timer:
21
20
 
22
- timer.add(10){puts "#{Time.now}: This is timed every 10 seconds"}
23
- timer.add(15){puts "#{Time.now}: This is timed every 15 seconds"}
21
+ ==== Simple example:
22
+
23
+ require 'actiontimer'
24
+ timer = ActionTimer::Timer.new
25
+ timer.add(1){ puts "#{Time.now}: This is timed every 1 second." }
26
+ timer.add(2){ puts "#{Time.now}: This is timed every 2 seconds." }
24
27
  loop do
25
- puts "#{Time.now}: Main loop that sleeps for 5 seconds"
26
- sleep(5)
28
+ puts "#{Time.now}: Main loop sleeps for 3 seconds."
29
+ sleep(3)
27
30
  end
31
+
32
+ =>
33
+ 2010-01-05 17:52:46 -0800: Main loop sleeps for 3 seconds.
34
+ 2010-01-05 17:52:47 -0800: This is timed every 1 second.
35
+ 2010-01-05 17:52:48 -0800: This is timed every 1 second.
36
+ 2010-01-05 17:52:48 -0800: This is timed every 2 seconds.
37
+ 2010-01-05 17:52:49 -0800: Main loop sleeps for 3 seconds.
38
+ 2010-01-05 17:52:49 -0800: This is timed every 1 second.
39
+ 2010-01-05 17:52:50 -0800: This is timed every 1 second.
40
+ 2010-01-05 17:52:50 -0800: This is timed every 2 seconds.
41
+ 2010-01-05 17:52:51 -0800: This is timed every 1 second.
42
+ 2010-01-05 17:52:52 -0800: Main loop sleeps for 3 seconds.
43
+
44
+ ==== Other examples:
45
+
46
+ What if you want to sleep for less than a second? Well, sure we can do that:
47
+
48
+ require 'actiontimer'
49
+ result = 0
50
+ timer = ActionTimer::Timer.new
51
+ timer.add(0.1){ result += 1 }
52
+ sleep(1.01)
53
+ p result
54
+
55
+ => 10
56
+
57
+ How about passing data to your block:
58
+
59
+ require 'actiontimer'
60
+ data = :foobar
61
+ timer = ActionTimer::Timer.new
62
+ timer.add(0.01, false, data){|x| puts "Data: #{x}" }
63
+ data = :fubar
64
+ p data
65
+ sleep(0.011)
66
+ p data
67
+
68
+ =>
69
+ :fubar
70
+ Data: foobar
71
+ :fubar
72
+
73
+ Or maybe you don't want the timer to start right away:
74
+
75
+ require 'actiontimer'
76
+ timer = ActionTimer::Timer.new(:auto_start => false)
77
+ output = 0
78
+ timer.add(0.1){ output += 1 }
79
+ sleep(1)
80
+ p output
81
+ timer.start
82
+ sleep(1.01)
83
+ p output
84
+
85
+ =>
86
+ 0
87
+ 10
88
+
89
+ What if you want to add multiple actions at one time? We can do this:
90
+
91
+ require 'actiontimer'
92
+ timer = ActionTimer::Timer.new
93
+ result = 0
94
+ actions = []
95
+ actions << ActionTimer::Action.new(@timer, 0.1){ result += 1}
96
+ actions << ActionTimer::Action.new(@timer, 0.2){ result += 1}
97
+ actions << ActionTimer::Action.new(@timer, 0.3){ result += 1}
98
+ timer.mass_add(actions)
99
+ sleep(0.41)
100
+ p result
101
+
102
+ => 7
103
+
104
+ == Last remarks
105
+
106
+ If you find any bugs, please report them through {github}[http://github.com/spox/actiontimer/issues]. If you are in need of any help, you can generally find me on DALnet and Freenode.
107
+
108
+ == License
28
109
 
29
- ==== Output:
30
-
31
- 2009-04-13 17:41:39 -0700: Main loop that sleeps for 5 seconds
32
- 2009-04-13 17:41:44 -0700: Main loop that sleeps for 5 seconds
33
- 2009-04-13 17:41:49 -0700: This is timed every 10 seconds
34
- 2009-04-13 17:41:49 -0700: Main loop that sleeps for 5 seconds
35
- 2009-04-13 17:41:54 -0700: This is timed every 15 seconds
36
- 2009-04-13 17:41:54 -0700: Main loop that sleeps for 5 seconds
37
- 2009-04-13 17:41:59 -0700: This is timed every 10 seconds
38
- 2009-04-13 17:41:59 -0700: Main loop that sleeps for 5 seconds
39
- 2009-04-13 17:42:04 -0700: Main loop that sleeps for 5 seconds
40
- 2009-04-13 17:42:09 -0700: This is timed every 10 seconds
41
- 2009-04-13 17:42:09 -0700: This is timed every 15 seconds
42
- 2009-04-13 17:42:09 -0700: Main loop that sleeps for 5 seconds
110
+ ActionPool is licensed under the LGPLv3
111
+ Copyright (c) 2009 spox <spox@modspox.com>
data/actiontimer.gemspec CHANGED
@@ -2,7 +2,7 @@ spec = Gem::Specification.new do |s|
2
2
  s.name = 'ActionTimer'
3
3
  s.author = 'spox'
4
4
  s.email = 'spox@modspox.com'
5
- s.version = '0.1.0'
5
+ s.version = '0.1.1'
6
6
  s.summary = 'Simple timer for a complex world'
7
7
  s.platform = Gem::Platform::RUBY
8
8
  s.has_rdoc = true
@@ -14,4 +14,4 @@ spec = Gem::Specification.new do |s|
14
14
  s.required_ruby_version = '>= 1.8.6'
15
15
  s.homepage = 'http://github.com/spox/actiontimer'
16
16
  s.description = 'ActionTimer is a simple timer for recurring actions. It supports single and recurring actions with an easy to use API.'
17
- end
17
+ end
@@ -81,6 +81,7 @@ module ActionTimer
81
81
  else
82
82
  actual_sleep = 0
83
83
  end
84
+ actual_sleep = to_sleep if actual_sleep <= 0
84
85
  tick(actual_sleep)
85
86
  add_waiting_actions
86
87
  end
data/test ADDED
@@ -0,0 +1,11 @@
1
+ require 'actiontimer'
2
+ timer = ActionTimer::Timer.new
3
+ result = 0
4
+ actions = []
5
+ actions << ActionTimer::Action.new(@timer, 0.1){ result += 1}
6
+ actions << ActionTimer::Action.new(@timer, 0.2){ result += 1}
7
+ actions << ActionTimer::Action.new(@timer, 0.3){ result += 1}
8
+ timer.mass_add(actions)
9
+ sleep(0.41)
10
+ p result
11
+
data/tests/run_tests.rb CHANGED
@@ -1,3 +1,5 @@
1
+ $LOAD_PATH.unshift(File.expand_path("#{__FILE__}/../../lib"))
2
+
1
3
  require 'test/unit'
2
4
  require 'actiontimer'
3
5
 
@@ -25,6 +27,15 @@ class TimerTests < Test::Unit::TestCase
25
27
  @timer.stop
26
28
  assert(!@timer.running?)
27
29
  end
30
+
31
+ # Check that a value 0 < t < 1 works
32
+ # as expected
33
+ def test_float
34
+ result = 0
35
+ @timer.add(0.1){ result += 1 }
36
+ sleep(1.01)
37
+ assert_equal(10, result)
38
+ end
28
39
 
29
40
  # Check that a single iterative action is only
30
41
  # completed once
@@ -44,7 +55,7 @@ class TimerTests < Test::Unit::TestCase
44
55
  @timer.pause
45
56
  sleep(2)
46
57
  @timer.start
47
- sleep(2.1)
58
+ sleep(2)
48
59
  assert_equal(5, result)
49
60
  end
50
61
 
@@ -131,4 +142,4 @@ class TimerTests < Test::Unit::TestCase
131
142
  sleep(3.1)
132
143
  assert_equal(result, 3)
133
144
  end
134
- end
145
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ActionTimer
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
  - spox
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-11 00:00:00 -08:00
12
+ date: 2010-01-05 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -33,15 +33,16 @@ extra_rdoc_files:
33
33
  - LICENSE
34
34
  - CHANGELOG
35
35
  files:
36
- - actiontimer.gemspec
37
- - tests/run_tests.rb
38
- - lib/actiontimer.rb
36
+ - lib/actiontimer/Exceptions.rb
39
37
  - lib/actiontimer/Timer.rb
40
38
  - lib/actiontimer/Action.rb
41
- - lib/actiontimer/Exceptions.rb
39
+ - lib/actiontimer.rb
40
+ - test
41
+ - tests/run_tests.rb
42
42
  - CHANGELOG
43
43
  - LICENSE
44
44
  - README.rdoc
45
+ - actiontimer.gemspec
45
46
  has_rdoc: true
46
47
  homepage: http://github.com/spox/actiontimer
47
48
  licenses: []