Class: SubPub::Register
- Inherits:
-
Object
- Object
- SubPub::Register
- Includes:
- Singleton
- Defined in:
- lib/sub_pub/register.rb
Instance Attribute Summary collapse
-
#enabled ⇒ Object
Returns the value of attribute enabled.
-
#scope ⇒ Object
Returns the value of attribute scope.
-
#subscriptions ⇒ Object
Returns the value of attribute subscriptions.
Class Method Summary collapse
- .disable ⇒ Object
- .disabled? ⇒ Boolean
- .enable ⇒ Object
- .enabled? ⇒ Boolean
- .publish(topic, payload = {}, &block) ⇒ Object
- .scope=(new_scope) ⇒ Object
- .scoped(topic) ⇒ Object
- .subscribe(*args, &block) ⇒ Object
- .unsubscribe(subscription) ⇒ Object
- .unsubscribe_all ⇒ Object
Instance Method Summary collapse
- #default_enabled_state ⇒ Object
- #default_scope ⇒ Object
-
#initialize ⇒ Register
constructor
A new instance of Register.
Constructor Details
#initialize ⇒ Register
Returns a new instance of Register.
9 10 11 12 13 14 |
# File 'lib/sub_pub/register.rb', line 9 def initialize @enabled = default_enabled_state @scope = default_scope @subscriptions = [] super end |
Instance Attribute Details
#enabled ⇒ Object
Returns the value of attribute enabled.
7 8 9 |
# File 'lib/sub_pub/register.rb', line 7 def enabled @enabled end |
#scope ⇒ Object
Returns the value of attribute scope.
7 8 9 |
# File 'lib/sub_pub/register.rb', line 7 def scope @scope end |
#subscriptions ⇒ Object
Returns the value of attribute subscriptions.
7 8 9 |
# File 'lib/sub_pub/register.rb', line 7 def subscriptions @subscriptions end |
Class Method Details
.disable ⇒ Object
33 34 35 |
# File 'lib/sub_pub/register.rb', line 33 def disable instance.enabled = false end |
.disabled? ⇒ Boolean
45 46 47 |
# File 'lib/sub_pub/register.rb', line 45 def disabled? !instance.enabled end |
.enable ⇒ Object
29 30 31 |
# File 'lib/sub_pub/register.rb', line 29 def enable instance.enabled = true end |
.enabled? ⇒ Boolean
37 38 39 40 41 42 43 |
# File 'lib/sub_pub/register.rb', line 37 def enabled? if instance.enabled.nil? instance.enabled = true end instance.enabled end |
.publish(topic, payload = {}, &block) ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/sub_pub/register.rb', line 49 def publish(topic, payload = {}, &block) return if disabled? full_topic = scoped(topic).full_topic ActiveSupport::Notifications.publish(full_topic, payload, &block) end |
.scope=(new_scope) ⇒ Object
25 26 27 |
# File 'lib/sub_pub/register.rb', line 25 def scope=(new_scope) instance.scope = new_scope end |
.scoped(topic) ⇒ Object
57 58 59 |
# File 'lib/sub_pub/register.rb', line 57 def scoped(topic) ScopedTopic.new(topic, instance.scope) end |
.subscribe(*args, &block) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/sub_pub/register.rb', line 61 def subscribe(*args, &block) topic = args.first = { scoped_topic: ScopedTopic.new(topic, instance.scope), action: block } Subscription.subscribe().tap do |subscription| instance.subscriptions << subscription end end |
.unsubscribe(subscription) ⇒ Object
74 75 76 77 |
# File 'lib/sub_pub/register.rb', line 74 def unsubscribe(subscription) subscription.unsubscribe instance.subscriptions.delete(subscription) end |
.unsubscribe_all ⇒ Object
79 80 81 82 83 84 85 |
# File 'lib/sub_pub/register.rb', line 79 def unsubscribe_all instance.subscriptions.each do |subscription| subscription.unsubscribe end instance.subscriptions = [] end |
Instance Method Details
#default_enabled_state ⇒ Object
16 17 18 |
# File 'lib/sub_pub/register.rb', line 16 def default_enabled_state true end |
#default_scope ⇒ Object
20 21 22 |
# File 'lib/sub_pub/register.rb', line 20 def default_scope "sub_pub" end |