sleeper 0.0.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.rb +2 -0
- data/lib/sleeper/schedule.rb +79 -0
- data/lib/sleeper/timer.rb +43 -0
- metadata +50 -0
data/lib/sleeper.rb
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
module Sleeper
|
2
|
+
class Schedule
|
3
|
+
def initialize(values, recycle=false)
|
4
|
+
@values = values
|
5
|
+
@index = LoopableRange.new(0, values.length - 1, cyclic: recycle)
|
6
|
+
end
|
7
|
+
|
8
|
+
def next
|
9
|
+
@values[@index.succ]
|
10
|
+
end
|
11
|
+
|
12
|
+
def current
|
13
|
+
@values[index]
|
14
|
+
end
|
15
|
+
|
16
|
+
def last
|
17
|
+
index == 0 ? prev = 0 : prev = index - 1
|
18
|
+
|
19
|
+
@values[prev]
|
20
|
+
end
|
21
|
+
|
22
|
+
def index
|
23
|
+
@index.position
|
24
|
+
end
|
25
|
+
|
26
|
+
def reset
|
27
|
+
@index.reset
|
28
|
+
end
|
29
|
+
|
30
|
+
class LoopableRange
|
31
|
+
include Enumerable
|
32
|
+
|
33
|
+
attr_reader :position
|
34
|
+
|
35
|
+
def initialize(start, finish, opts={})
|
36
|
+
@start = start
|
37
|
+
@finish = finish
|
38
|
+
@cyclic = opts[:cyclic] || false
|
39
|
+
@position = opts[:position] || start
|
40
|
+
end
|
41
|
+
|
42
|
+
def succ
|
43
|
+
if cyclic?
|
44
|
+
@position = @position == @finish ? @start : @position += 1
|
45
|
+
else
|
46
|
+
@position = @position == @finish ? @finish : @position += 1
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def prev
|
51
|
+
if cyclic?
|
52
|
+
@position = @position == @start ? @finish : @position -= 1
|
53
|
+
else
|
54
|
+
@position = @position == @start ? @start : @position -= 1
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def cyclic?
|
59
|
+
@cyclic
|
60
|
+
end
|
61
|
+
|
62
|
+
def reset
|
63
|
+
@position = @start
|
64
|
+
end
|
65
|
+
|
66
|
+
# This isn't quite correct.
|
67
|
+
def each
|
68
|
+
@position = @start
|
69
|
+
|
70
|
+
while @position < @finish
|
71
|
+
yield @positon
|
72
|
+
self.succ
|
73
|
+
end
|
74
|
+
|
75
|
+
yield @position
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module Sleeper
|
2
|
+
# TODO: StateMachine?
|
3
|
+
class Timer
|
4
|
+
attr_reader :schedule
|
5
|
+
|
6
|
+
def initialize(schedule, cyclic=false)
|
7
|
+
@cyclic = cyclic
|
8
|
+
from_hash(schedule) if schedule.class == Hash
|
9
|
+
from_array(schedule) if schedule.class == Array
|
10
|
+
end
|
11
|
+
|
12
|
+
# TODO: Should raise exception if @schedule[key] is nil
|
13
|
+
# TODO: Need two separate method calls here for block or no block
|
14
|
+
def run(&block)
|
15
|
+
if block_given?
|
16
|
+
key = block.call
|
17
|
+
sleep(@schedule[key].current)
|
18
|
+
@schedule[key].next
|
19
|
+
else
|
20
|
+
sleep(@schedule.current)
|
21
|
+
@schedule.next
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def reset(key=nil)
|
26
|
+
key.nil? ? @schedule.reset : @schedule[key].reset
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def from_hash(schedule)
|
32
|
+
@schedule = {}
|
33
|
+
|
34
|
+
schedule.each_pair do |key, values|
|
35
|
+
@schedule[key] = Schedule.new values, @cyclic
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def from_array(schedule)
|
40
|
+
@schedule = Schedule.new schedule, @cyclic
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
metadata
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sleeper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Glen Holcomb
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-02-15 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: A small gem for variable sleep intervals
|
15
|
+
email:
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- !binary |-
|
21
|
+
bGliL3NsZWVwZXIvc2NoZWR1bGUucmI=
|
22
|
+
- !binary |-
|
23
|
+
bGliL3NsZWVwZXIvdGltZXIucmI=
|
24
|
+
- !binary |-
|
25
|
+
bGliL3NsZWVwZXIucmI=
|
26
|
+
homepage: ''
|
27
|
+
licenses: []
|
28
|
+
post_install_message:
|
29
|
+
rdoc_options: []
|
30
|
+
require_paths:
|
31
|
+
- lib
|
32
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
requirements: []
|
45
|
+
rubyforge_project:
|
46
|
+
rubygems_version: 1.8.10
|
47
|
+
signing_key:
|
48
|
+
specification_version: 3
|
49
|
+
summary: Schedules
|
50
|
+
test_files: []
|