Class: Tavern::Hub
- Inherits:
-
Object
- Object
- Tavern::Hub
- Defined in:
- lib/tavern/hub.rb
Overview
Implements a simplified Pub / Sub hub for in-application notifications. Used inside smeghead as a general replacement for observers and a way for items to hook into events.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#subscriptions ⇒ Object
readonly
Returns the value of attribute subscriptions.
Instance Method Summary collapse
-
#initialize ⇒ Hub
constructor
Initializes the given hub with an empty set of subscriptions.
- #primary=(value) ⇒ Object
- #primary? ⇒ Boolean
-
#publish(path, context = {}) ⇒ true, false
Publishes a message to the given path and with a given hash context.
-
#subscribe(path, object = nil, &blk) ⇒ Object
Subscribes to a given path string and either a proc callback or any object responding to #call.
-
#unsubscribe(subscription) ⇒ Subscription
Deletes the given subscription from this pub sub hub.
Constructor Details
#initialize ⇒ Hub
Initializes the given hub with an empty set of subscriptions.
42 43 44 45 |
# File 'lib/tavern/hub.rb', line 42 def initialize @subscriptions = Subscriptions.new @primary = false end |
Instance Attribute Details
#subscriptions ⇒ Object (readonly)
Returns the value of attribute subscriptions.
39 40 41 |
# File 'lib/tavern/hub.rb', line 39 def subscriptions @subscriptions end |
Instance Method Details
#primary=(value) ⇒ Object
95 96 97 98 99 100 101 |
# File 'lib/tavern/hub.rb', line 95 def primary=(value) value = !!value if value != @primary @primary = value ActiveSupport.run_load_hooks :tavern_hub, self if @primary end end |
#primary? ⇒ Boolean
91 92 93 |
# File 'lib/tavern/hub.rb', line 91 def primary? !!@primary end |
#publish(path, context = {}) ⇒ true, false
Publishes a message to the given path and with a given hash context.
84 85 86 87 88 89 |
# File 'lib/tavern/hub.rb', line 84 def publish(path, context = {}) path_parts = path.split(":") context = merge_path_context path_parts, context # Actually handle publishing the subscription subscriptions.call(context.merge(:path_parts => path_parts, :full_path => path)) != false end |
#subscribe(path, object = nil, &blk) ⇒ Object
Subscribes to a given path string and either a proc callback or any object responding to #call.
58 59 60 61 62 63 64 65 66 |
# File 'lib/tavern/hub.rb', line 58 def subscribe(path, object = nil, &blk) if object and not object.respond_to?(:call) raise ArgumentError, "you provided an object as an argument but it doesn't respond to #call" end subscription = Subscription.new(path, (object || blk)) level = subscriptions.sublevel_at subscription.to_subscribe_keys level.add subscription subscription end |
#unsubscribe(subscription) ⇒ Subscription
Deletes the given subscription from this pub sub hub.
71 72 73 74 75 76 |
# File 'lib/tavern/hub.rb', line 71 def unsubscribe(subscription) return if subscription.blank? level = subscriptions.sublevel_at subscription.to_subscribe_keys level.delete subscription subscription end |