Class: Pub

Inherits:
Object
  • Object
show all
Defined in:
lib/pub.rb,
lib/pub/patron.rb,
lib/pub/helpers.rb,
lib/pub/version.rb,
lib/pub/bartender.rb

Overview

A Redis-backed pub with a non-blocking bar counter.

Or, putting aside the metaphor for a moment:

A processing queue where consumers, instead of simply queuing jobs and getting on with their lives, queue and wait for a response without blocking the Ruby process.

Each pub instance is a distinct queue.

Defined Under Namespace

Modules: Helpers Classes: Bartender, Patron

Constant Summary collapse

VERSION =
"0.1.0"

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Pub

Enters a pub.

Takes the name of the pub and an optional block.

Pub.new('Ye Olde Rubies') do |pub|

  patron = pub.new_patron

  patron.order('Guinness') do |beer|
    JSON.parse(beer).drink
  end

end


45
46
47
48
# File 'lib/pub.rb', line 45

def initialize(name)
  @name = name
  EM.synchrony { yield self } if block_given?
end

Class Attribute Details

.beer_tapObject

A device that dispenses beer.



23
24
25
# File 'lib/pub.rb', line 23

def beer_tap
  @beer_tap
end

Instance Attribute Details

#nameObject (readonly)

The name of the pub.



19
20
21
# File 'lib/pub.rb', line 19

def name
  @name
end

Class Method Details

.counterObject

The bar counter.



26
27
28
# File 'lib/pub.rb', line 26

def counter
  Redis.new(url: beer_tap)
end

Instance Method Details

#closeObject

Closes the pub for the night.



51
52
53
# File 'lib/pub.rb', line 51

def close
  EM.stop
end

#new_bartenderObject

A new bartender.



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

def new_bartender
  Bartender.new(name)
end

#new_patron(timeout = 5) ⇒ Object

A new patron.



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

def new_patron(timeout = 5)
  Patron.new(name, timeout)
end