Class: Zack::Target

Inherits:
Object
  • Object
show all
Defined in:
lib/zack/target.rb

Overview

Abstract base class for everything that is an RPC target. This implements some common mechanisms like a run loop, exception handling and argument handling.

Direct Known Subclasses

Server

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tube_name, opts = {}) ⇒ Target

Initializes #factory and #server.



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/zack/target.rb', line 13

def initialize(tube_name, opts={})
  @server = opts[:server] || 'beanstalk:11300'

  if opts.has_key? :factory
    @factory = opts[:factory]
  elsif opts.has_key? :simple
    klass = opts[:simple]
    @factory = lambda { klass.new }
  else
    raise ArgumentError, "Either :factory or :simple argument must be given." 
  end
end

Instance Attribute Details

#factoryObject (readonly)

Returns the value of attribute factory.



8
9
10
# File 'lib/zack/target.rb', line 8

def factory
  @factory
end

#serverObject (readonly)

Returns the value of attribute server.



9
10
11
# File 'lib/zack/target.rb', line 9

def server
  @server
end

Instance Method Details

#handle_requestObject

Handles one request. This is specific to implementors.

Raises:

  • (NotImplementedError)


37
38
39
40
# File 'lib/zack/target.rb', line 37

def handle_request
  raise NotImplementedError, 
    "Abstract base class doesn't implement #handle_request."
end

#process_request(sym, args) ⇒ Object

Processes exactly one request, but doesn’t define how the request gets here.



29
30
31
32
33
# File 'lib/zack/target.rb', line 29

def process_request(sym, args)
  instance = factory.call
  
  instance.send(sym, *args)
end

#run(&block) ⇒ Object

Runs the server and keeps running until the world ends (or the process, whichever comes first).



45
46
47
48
49
50
51
# File 'lib/zack/target.rb', line 45

def run(&block)
  loop do
    exception_handling(block) do
      handle_request
    end
  end
end