Class: Tavern::Hub

Inherits:
Object
  • Object
show all
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

MockHub

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHub

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

#subscriptionsObject (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

Returns:

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

Examples:

Publishing a message

hub.publish 'hello:world', :hello => 'world'

Parameters:

  • path (String)

    the pubsub path

  • context (Hash{Symbol => Object}) (defaults to: {})

    the message context

Returns:

  • (true, false)

    whether or not all callbacks executed successfully.



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.

Examples:

Subscribing with a block

hub.subscribe 'hello:world' do |ctx|
  puts "Context is #{ctx.inspect}"
end

Subscribing with an object

hub.subscribe 'hello:world', MyCallableClass

Parameters:

  • path (String)

    the subscription path

  • object (#call) (defaults to: nil)

    if present, the callback to invoke

  • blk (Proc)

    the block to use for the callback (if the object is nil)



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.

Parameters:

  • subscription (Subscription)

    the subscription to delete

Returns:



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