Class: FloorManager::Queue

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/floormanager/queue.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Queue

Returns a new instance of Queue.



5
6
7
8
# File 'lib/floormanager/queue.rb', line 5

def initialize(*args)
	@queue = []
	args.each{|a| self << a} if (args = *args)
end

Instance Method Details

#[]Object

Get an item in the queue



27
28
29
# File 'lib/floormanager/queue.rb', line 27

def get_item(index)
	@queue[index]
end

#add(item) ⇒ Object Also known as: <<

Add an item to the queue



13
14
15
16
17
18
19
20
# File 'lib/floormanager/queue.rb', line 13

def add(item)
	@queue << {
		:item  => item,
		:value => nil,
		:state => States::PENDING,
		:index => @queue.length
	}
end

#checked_outObject

Get checked out items



76
77
78
# File 'lib/floormanager/queue.rb', line 76

def checked_out
	checked_out_items.map{|i| i[:item]}
end

#checkin(item, state = States::SUCCESS) ⇒ Object

Check in an item with a new value, optionally with a state (which defaults to SUCCESS)



47
48
49
50
51
52
53
# File 'lib/floormanager/queue.rb', line 47

def checkin(item, state=States::SUCCESS)
	if item.kind_of?(Hash) && @queue[item[:index]][:item] == item[:item]
		@queue[item[:index]] = item.merge({:state => state})
	else
		invalid_item!
	end
end

#checkoutObject

Check out an item from the queue



35
36
37
38
39
40
41
42
43
# File 'lib/floormanager/queue.rb', line 35

def checkout
	if pending?
		item = pending_items.first
		item[:state] = States::CHECKED_OUT
		item
	else
		nil
	end
end

#completedObject

Get completed items



81
82
83
# File 'lib/floormanager/queue.rb', line 81

def completed
	completed_items.map{|i| i[:item]}
end

#done?Boolean

Is the queue done?

Returns:

  • (Boolean)


61
62
63
# File 'lib/floormanager/queue.rb', line 61

def done?
	(pending.length == 0 && checked_out.length == 0) ? true : false
end

#eachObject

Returns each item with value, hash style



30
31
32
# File 'lib/floormanager/queue.rb', line 30

def each
	@queue.each{|item| yield item[:item], item[:value]}
end

#failedObject

Get failed items



91
92
93
# File 'lib/floormanager/queue.rb', line 91

def failed
	failed_items.map{|i| i[:item]}
end

#lengthObject

Total queue length



56
57
58
# File 'lib/floormanager/queue.rb', line 56

def length
	@queue.length
end

#pendingObject

Get pending items



71
72
73
# File 'lib/floormanager/queue.rb', line 71

def pending
	pending_items.map{|i| i[:item]}
end

#pending?Boolean

Does this queue have pending items?

Returns:

  • (Boolean)


66
67
68
# File 'lib/floormanager/queue.rb', line 66

def pending?
	(pending.length > 0) ? true : false
end

#successedObject

Get successed items



86
87
88
# File 'lib/floormanager/queue.rb', line 86

def successed
	successed_items.map{|i| i[:item]}
end