Class: Pub::Bartender
Overview
A bartender.
In other words, a producer in our processing queue.
Instance Method Summary collapse
-
#initialize(pub_name) ⇒ Bartender
constructor
Creates a new bartender.
-
#serve(beer, &block) ⇒ Object
Serves a beer to a thirsty patron.
-
#take_orders(count = 1) ⇒ Object
Takes one or more orders from the queue.
Constructor Details
#initialize(pub_name) ⇒ Bartender
Creates a new bartender.
Takes the name of the pub.
11 12 13 |
# File 'lib/pub/bartender.rb', line 11 def initialize(pub_name) @pub_name = pub_name end |
Instance Method Details
#serve(beer, &block) ⇒ Object
Serves a beer to a thirsty patron.
Takes a block which should return a glass of beer.
See below for example usage.
20 21 22 23 |
# File 'lib/pub/bartender.rb', line 20 def serve(beer, &block) counter.lrem(@pub_name, 0, beer) counter.publish(order_for(beer), block.call) end |
#take_orders(count = 1) ⇒ Object
Takes one or more orders from the queue.
orders = .take_orders(3)
orders.each do |order|
.serve(order) do
"A pint of #{order}"
end
end
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/pub/bartender.rb', line 33 def take_orders(count = 1) orders = Array.new count.times do order = counter.lpop(@pub_name) || break orders << order end orders end |