Class: SubPub::Register

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/sub_pub/register.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRegister

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

#enabledObject

Returns the value of attribute enabled.



7
8
9
# File 'lib/sub_pub/register.rb', line 7

def enabled
  @enabled
end

#scopeObject

Returns the value of attribute scope.



7
8
9
# File 'lib/sub_pub/register.rb', line 7

def scope
  @scope
end

#subscriptionsObject

Returns the value of attribute subscriptions.



7
8
9
# File 'lib/sub_pub/register.rb', line 7

def subscriptions
  @subscriptions
end

Class Method Details

.disableObject



33
34
35
# File 'lib/sub_pub/register.rb', line 33

def disable
  instance.enabled = false
end

.disabled?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/sub_pub/register.rb', line 45

def disabled?
  !instance.enabled
end

.enableObject



29
30
31
# File 'lib/sub_pub/register.rb', line 29

def enable
  instance.enabled = true
end

.enabled?Boolean

Returns:

  • (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

  options = {
    scoped_topic: ScopedTopic.new(topic, instance.scope),
    action: block
  }

  Subscription.subscribe(options).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_allObject



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_stateObject



16
17
18
# File 'lib/sub_pub/register.rb', line 16

def default_enabled_state
  true
end

#default_scopeObject



20
21
22
# File 'lib/sub_pub/register.rb', line 20

def default_scope
  "sub_pub"
end