sleeper 0.0.0 → 0.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.
- data/lib/sleeper/timer.rb +32 -13
- metadata +3 -3
data/lib/sleeper/timer.rb
CHANGED
@@ -1,29 +1,48 @@
|
|
1
1
|
module Sleeper
|
2
2
|
# TODO: StateMachine?
|
3
|
+
# TODO: Need to be able to add and remove schedules?
|
3
4
|
class Timer
|
4
|
-
attr_reader :schedule
|
5
|
+
attr_reader :schedule, :default
|
5
6
|
|
6
|
-
def initialize(schedule,
|
7
|
-
@cyclic = cyclic
|
7
|
+
def initialize(schedule, opts={})
|
8
|
+
@cyclic = opts[:cyclic] || false
|
9
|
+
@default = opts[:default] || nil
|
8
10
|
from_hash(schedule) if schedule.class == Hash
|
9
11
|
from_array(schedule) if schedule.class == Array
|
10
12
|
end
|
11
13
|
|
12
|
-
# TODO: Should raise exception if @schedule[key] is nil
|
13
|
-
# TODO: Need two separate method calls here for block or no block
|
14
14
|
def run(&block)
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
15
|
+
block_given? ? schedule = @schedule[block.call] : schedule = @schedule
|
16
|
+
|
17
|
+
# TODO: If schedule nil then use default or raise exception?
|
18
|
+
follow_schedule(schedule)
|
19
|
+
end
|
20
|
+
|
21
|
+
def follow_schedule(schedule)
|
22
|
+
period = schedule.current
|
23
|
+
|
24
|
+
sleep(period)
|
25
|
+
schedule.next
|
26
|
+
|
27
|
+
period
|
28
|
+
end
|
29
|
+
|
30
|
+
# TODO: Hash reset should be separate method and should handle entire hash or specific keys.
|
31
|
+
def reset(key=nil)
|
32
|
+
return @schedule[key].reset unless key.nil?
|
33
|
+
|
34
|
+
if @schedule.class == Hash
|
35
|
+
@schedule.values.each do |schedule|
|
36
|
+
schedule.reset
|
37
|
+
end
|
19
38
|
else
|
20
|
-
|
21
|
-
@schedule.next
|
39
|
+
@schedule.reset
|
22
40
|
end
|
41
|
+
#key.nil? ? @schedule.reset : @schedule[key].reset
|
23
42
|
end
|
24
43
|
|
25
|
-
def
|
26
|
-
|
44
|
+
def default=(value)
|
45
|
+
value.nil? ? @default = nil : @default = Schedule.new(Array(value), @cyclic)
|
27
46
|
end
|
28
47
|
|
29
48
|
private
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sleeper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-02-
|
12
|
+
date: 2012-02-20 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: A small gem for variable sleep intervals
|
15
15
|
email:
|
@@ -23,7 +23,7 @@ files:
|
|
23
23
|
bGliL3NsZWVwZXIvdGltZXIucmI=
|
24
24
|
- !binary |-
|
25
25
|
bGliL3NsZWVwZXIucmI=
|
26
|
-
homepage:
|
26
|
+
homepage: https://github.com/LeakyBucket/Sleeper
|
27
27
|
licenses: []
|
28
28
|
post_install_message:
|
29
29
|
rdoc_options: []
|