floormanager 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +4 -0
- data/VERSION +1 -1
- data/floormanager.gemspec +3 -1
- data/lib/floormanager/queue.rb +13 -19
- data/lib/floormanager/result.rb +28 -0
- data/lib/floormanager/states.rb +8 -0
- data/lib/floormanager/workers.rb +15 -2
- data/lib/floormanager.rb +13 -1
- metadata +4 -2
data/README.rdoc
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/floormanager.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{floormanager}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.2.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Inge J\303\270rgensen"]
|
@@ -26,6 +26,8 @@ Gem::Specification.new do |s|
|
|
26
26
|
"floormanager.gemspec",
|
27
27
|
"lib/floormanager.rb",
|
28
28
|
"lib/floormanager/queue.rb",
|
29
|
+
"lib/floormanager/result.rb",
|
30
|
+
"lib/floormanager/states.rb",
|
29
31
|
"lib/floormanager/workers.rb",
|
30
32
|
"spec/floormanager_spec.rb",
|
31
33
|
"spec/spec.opts",
|
data/lib/floormanager/queue.rb
CHANGED
@@ -2,12 +2,6 @@ module FloorManager
|
|
2
2
|
class Queue
|
3
3
|
include Enumerable
|
4
4
|
|
5
|
-
# States
|
6
|
-
PENDING = 0
|
7
|
-
CHECKED_OUT = 1
|
8
|
-
FAILED = 2
|
9
|
-
SUCCESS = 3
|
10
|
-
|
11
5
|
def initialize(*args)
|
12
6
|
@queue = []
|
13
7
|
args.each{|a| self << a} if (args = *args)
|
@@ -20,7 +14,7 @@ module FloorManager
|
|
20
14
|
@queue << {
|
21
15
|
:item => item,
|
22
16
|
:value => nil,
|
23
|
-
:state => PENDING
|
17
|
+
:state => States::PENDING
|
24
18
|
}
|
25
19
|
end
|
26
20
|
alias :<< :add
|
@@ -45,7 +39,7 @@ module FloorManager
|
|
45
39
|
def checkout
|
46
40
|
if pending?
|
47
41
|
item = pending_items.first
|
48
|
-
item[:state] = CHECKED_OUT
|
42
|
+
item[:state] = States::CHECKED_OUT
|
49
43
|
item[:item]
|
50
44
|
else
|
51
45
|
nil
|
@@ -54,7 +48,7 @@ module FloorManager
|
|
54
48
|
|
55
49
|
# Check in an item with a new value, optionally with a state
|
56
50
|
# (which defaults to SUCCESS)
|
57
|
-
def checkin(item, value, state=SUCCESS)
|
51
|
+
def checkin(item, value, state=States::SUCCESS)
|
58
52
|
if keys.include?(item)
|
59
53
|
item = get_item(item)
|
60
54
|
item[:value] = value
|
@@ -111,27 +105,27 @@ module FloorManager
|
|
111
105
|
|
112
106
|
# Is this item processed?
|
113
107
|
def processed?(key)
|
114
|
-
state(key) > PENDING
|
108
|
+
state(key) > States::PENDING
|
115
109
|
end
|
116
110
|
|
117
111
|
# Is this item checked out?
|
118
112
|
def checked_out?(key)
|
119
|
-
state(key) == CHECKED_OUT
|
113
|
+
state(key) == States::CHECKED_OUT
|
120
114
|
end
|
121
115
|
|
122
116
|
# Did this item fail?
|
123
117
|
def failed?(key)
|
124
|
-
state(key) == FAILED
|
118
|
+
state(key) == States::FAILED
|
125
119
|
end
|
126
120
|
|
127
121
|
# Did this item succeed?
|
128
122
|
def success?(key)
|
129
|
-
state(key) == SUCCESS
|
123
|
+
state(key) == States::SUCCESS
|
130
124
|
end
|
131
125
|
|
132
126
|
# Is this item completed?
|
133
127
|
def completed?(key)
|
134
|
-
state(key) >= FAILED
|
128
|
+
state(key) >= States::FAILED
|
135
129
|
end
|
136
130
|
|
137
131
|
private
|
@@ -149,27 +143,27 @@ module FloorManager
|
|
149
143
|
|
150
144
|
# Get all pending items
|
151
145
|
def pending_items
|
152
|
-
@queue.select{|i| i[:state] == PENDING}
|
146
|
+
@queue.select{|i| i[:state] == States::PENDING}
|
153
147
|
end
|
154
148
|
|
155
149
|
# Get all items which are checked out
|
156
150
|
def checked_out_items
|
157
|
-
@queue.select{|i| i[:state] == CHECKED_OUT}
|
151
|
+
@queue.select{|i| i[:state] == States::CHECKED_OUT}
|
158
152
|
end
|
159
153
|
|
160
154
|
# Get all completed items (both failed and successed)
|
161
155
|
def completed_items
|
162
|
-
@queue.select{|i| i[:state] >= FAILED}
|
156
|
+
@queue.select{|i| i[:state] >= States::FAILED}
|
163
157
|
end
|
164
158
|
|
165
159
|
# Get all failed items
|
166
160
|
def failed_items
|
167
|
-
@queue.select{|i| i[:state] == FAILED}
|
161
|
+
@queue.select{|i| i[:state] == States::FAILED}
|
168
162
|
end
|
169
163
|
|
170
164
|
# Get all successed items
|
171
165
|
def successed_items
|
172
|
-
@queue.select{|i| i[:state] == SUCCESS}
|
166
|
+
@queue.select{|i| i[:state] == States::SUCCESS}
|
173
167
|
end
|
174
168
|
|
175
169
|
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module FloorManager
|
2
|
+
class Result < FloorManager::BasicObject
|
3
|
+
attr_accessor :result, :state
|
4
|
+
attr_reader :delegate
|
5
|
+
|
6
|
+
def initialize(delegate, result, state=States::SUCCESS)
|
7
|
+
@delegate = delegate
|
8
|
+
@result = result
|
9
|
+
@state = state
|
10
|
+
end
|
11
|
+
|
12
|
+
def method_missing(name, *args, &block)
|
13
|
+
@delegate.send(name, *args, &block)
|
14
|
+
end
|
15
|
+
|
16
|
+
def state
|
17
|
+
@state
|
18
|
+
end
|
19
|
+
|
20
|
+
def failed?
|
21
|
+
@state == States::FAILED
|
22
|
+
end
|
23
|
+
|
24
|
+
def success?
|
25
|
+
@state == States::SUCCESS
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/lib/floormanager/workers.rb
CHANGED
@@ -14,8 +14,9 @@ module FloorManager
|
|
14
14
|
Thread.new do
|
15
15
|
until queue.done?
|
16
16
|
if item = checkout
|
17
|
-
|
18
|
-
|
17
|
+
result = yield(item)
|
18
|
+
result = Result.new(result, result, (result.state rescue States::SUCCESS))
|
19
|
+
checkin(item, result, result.state)
|
19
20
|
else
|
20
21
|
Thread.pass
|
21
22
|
end
|
@@ -38,5 +39,17 @@ module FloorManager
|
|
38
39
|
@mutex.synchronize{yield}
|
39
40
|
end
|
40
41
|
alias :exclusively :synchronize
|
42
|
+
|
43
|
+
def result(result, state=States::SUCCESS)
|
44
|
+
Result.new(result, result, state)
|
45
|
+
end
|
46
|
+
|
47
|
+
def success(result)
|
48
|
+
result(result, States::SUCCESS)
|
49
|
+
end
|
50
|
+
|
51
|
+
def fail(result)
|
52
|
+
result(result, States::FAILED)
|
53
|
+
end
|
41
54
|
end
|
42
55
|
end
|
data/lib/floormanager.rb
CHANGED
@@ -1,3 +1,15 @@
|
|
1
|
+
module FloorManager
|
2
|
+
if defined?(::BasicObject)
|
3
|
+
BasicObject = ::BasicObject #:nodoc:
|
4
|
+
else
|
5
|
+
class BasicObject #:nodoc:
|
6
|
+
instance_methods.each{|m| undef_method m unless m =~ /^__|instance_eval/}
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
1
11
|
require 'thread'
|
12
|
+
require 'floormanager/states'
|
2
13
|
require 'floormanager/queue'
|
3
|
-
require 'floormanager/workers'
|
14
|
+
require 'floormanager/workers'
|
15
|
+
require 'floormanager/result'
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
7
|
+
- 2
|
8
8
|
- 0
|
9
|
-
version: 0.
|
9
|
+
version: 0.2.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- "Inge J\xC3\xB8rgensen"
|
@@ -50,6 +50,8 @@ files:
|
|
50
50
|
- floormanager.gemspec
|
51
51
|
- lib/floormanager.rb
|
52
52
|
- lib/floormanager/queue.rb
|
53
|
+
- lib/floormanager/result.rb
|
54
|
+
- lib/floormanager/states.rb
|
53
55
|
- lib/floormanager/workers.rb
|
54
56
|
- spec/floormanager_spec.rb
|
55
57
|
- spec/spec.opts
|