Class: Zack::Target
- Inherits:
-
Object
- Object
- Zack::Target
- 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
Instance Attribute Summary collapse
-
#factory ⇒ Object
readonly
Returns the value of attribute factory.
-
#server ⇒ Object
readonly
Returns the value of attribute server.
Instance Method Summary collapse
-
#handle_request ⇒ Object
Handles one request.
-
#initialize(tube_name, opts = {}) ⇒ Target
constructor
Initializes #factory and #server.
-
#process_request(sym, args) ⇒ Object
Processes exactly one request, but doesn’t define how the request gets here.
-
#run(&block) ⇒ Object
Runs the server and keeps running until the world ends (or the process, whichever comes first).
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
#factory ⇒ Object (readonly)
Returns the value of attribute factory.
8 9 10 |
# File 'lib/zack/target.rb', line 8 def factory @factory end |
#server ⇒ Object (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_request ⇒ Object
Handles one request. This is specific to implementors.
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 |