Module: Gilmour::Base::Registrar
- Defined in:
- lib/gilmour/base.rb
Overview
Registration module ########### This module helps act as a Resistrar for subclasses
Constant Summary collapse
- DEFAULT_SUBSCRIBER_PATH =
'subscribers'- @@subscribers =
rubocop:disable all
{}
- @@registered_services =
[]
Instance Attribute Summary collapse
-
#backend ⇒ Object
Returns the value of attribute backend.
-
#subscribers_path ⇒ Object
Returns the value of attribute subscribers_path.
Instance Method Summary collapse
-
#inherited(child) ⇒ Object
:nodoc:.
-
#listen_to(topic, opts = {}, &handler) ⇒ Object
(also: #add_listener)
This is the underlying layer of communication.
-
#load_all(dir = nil) ⇒ Object
- Loads all ruby source files inside dir as subscribers Should only be used inside the parent container class Params:
dir -
relative path of directory to load subscribers from.
- Loads all ruby source files inside dir as subscribers Should only be used inside the parent container class Params:
-
#load_subscriber(path) ⇒ Object
:nodoc:.
-
#registered_subscribers ⇒ Object
Returns the subscriber classes registered.
-
#reply_to(topic, opts = {}, &handler) ⇒ Object
Add a reply listener.
-
#slot(topic, opts = {}, &handler) ⇒ Object
Add a slot listener.
-
#subscribers(topic = nil) ⇒ Object
- Returns the list of subscribers for topic or all subscribers if it is nil Params:
topic -
The topic for which to return the subscribers.
- Returns the list of subscribers for topic or all subscribers if it is nil Params:
Instance Attribute Details
#backend ⇒ Object
Returns the value of attribute backend.
41 42 43 |
# File 'lib/gilmour/base.rb', line 41 def backend @backend end |
#subscribers_path ⇒ Object
Returns the value of attribute subscribers_path.
40 41 42 |
# File 'lib/gilmour/base.rb', line 40 def subscribers_path @subscribers_path end |
Instance Method Details
#inherited(child) ⇒ Object
:nodoc:
46 47 48 |
# File 'lib/gilmour/base.rb', line 46 def inherited(child) #:nodoc: @@registered_services << child end |
#listen_to(topic, opts = {}, &handler) ⇒ Object Also known as: add_listener
This is the underlying layer of communication. Use if you know what you are doing. Use “reply_to” and “slot” instead.
Adds a listener for the given topic
topic-
The topic to listen to
opts-
Hash of optional arguments. Supported options are:
- exclusive
-
If true, this listener is added to a group of listeners
with the same name as the name of the class in which this method is called. A message sent to the topic will be processed by at most one listener from a group
- timeout
-
Maximum duration (seconds) that a subscriber has to
finish the task. If the execution exceeds the timeout, gilmour responds with status data: nil
70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/gilmour/base.rb', line 70 def listen_to(topic, opts={}, &handler) opt_defaults = { exclusive: false, timeout: 600, fork: false }.merge(opts) #Make sure these are not overriden by opts. opt_defaults[:handler] = handler opt_defaults[:subscriber] = self @@subscribers[topic] ||= [] @@subscribers[topic] << opt_defaults end |
#load_all(dir = nil) ⇒ Object
Loads all ruby source files inside dir as subscribers Should only be used inside the parent container class Params:
dir-
relative path of directory to load subscribers from
118 119 120 121 |
# File 'lib/gilmour/base.rb', line 118 def load_all(dir = nil) dir ||= (subscribers_path || DEFAULT_SUBSCRIBER_PATH) Dir["#{dir}/*.rb"].each { |f| require f } end |
#load_subscriber(path) ⇒ Object
:nodoc:
123 124 125 |
# File 'lib/gilmour/base.rb', line 123 def load_subscriber(path) #:nodoc: require path end |
#registered_subscribers ⇒ Object
Returns the subscriber classes registered
52 53 54 |
# File 'lib/gilmour/base.rb', line 52 def registered_subscribers @@registered_services end |
#reply_to(topic, opts = {}, &handler) ⇒ Object
Add a reply listener
87 88 89 90 91 92 |
# File 'lib/gilmour/base.rb', line 87 def reply_to(topic, opts={}, &handler) defopts = opts.merge({ type: :reply }) listen_to(topic, defopts, &handler) end |
#slot(topic, opts = {}, &handler) ⇒ Object
Add a slot listener
95 96 97 98 99 100 |
# File 'lib/gilmour/base.rb', line 95 def slot(topic, opts={}, &handler) defopts = opts.merge({ type: :slot }) listen_to(topic, defopts, &handler) end |
#subscribers(topic = nil) ⇒ Object
Returns the list of subscribers for topic or all subscribers if it is nil Params:
topic-
The topic for which to return the subscribers. All subscribers are
returned if this is not provided
106 107 108 109 110 111 112 |
# File 'lib/gilmour/base.rb', line 106 def subscribers(topic = nil) if topic @@subscribers[topic] else @@subscribers end end |