Class: Tavern::MockHub

Inherits:
Hub
  • Object
show all
Defined in:
lib/tavern/mock_hub.rb

Overview

A simple hub you can use to completely disable the pub / sub process but still record what happens.

Defined Under Namespace

Classes: Publication

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Hub

#primary=, #primary?

Constructor Details

#initializeMockHub

Returns a new instance of MockHub.



13
14
15
16
# File 'lib/tavern/mock_hub.rb', line 13

def initialize
  super
  @subscriptions, @unsubscriptions, @publications = [], [], []
end

Instance Attribute Details

#publicationsObject (readonly)

Returns the value of attribute publications.



11
12
13
# File 'lib/tavern/mock_hub.rb', line 11

def publications
  @publications
end

#subscriptionsObject (readonly)

Returns the value of attribute subscriptions.



11
12
13
# File 'lib/tavern/mock_hub.rb', line 11

def subscriptions
  @subscriptions
end

#unsubscriptionsObject (readonly)

Returns the value of attribute unsubscriptions.



11
12
13
# File 'lib/tavern/mock_hub.rb', line 11

def unsubscriptions
  @unsubscriptions
end

Instance Method Details

#publish(path, context = {}) ⇒ Object

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



49
50
51
# File 'lib/tavern/mock_hub.rb', line 49

def publish(path, context = {})
  publications << Publication.new(path, context)
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)



29
30
31
32
33
# File 'lib/tavern/mock_hub.rb', line 29

def subscribe(path, object = nil, &blk)
  subscription = Subscription.new(path, (object || blk))
  subscriptions << subscription
  subscription
end

#unsubscribe(subscription) ⇒ Subscription

Deletes the given subscription from this pub sub hub.

Parameters:

  • subscription (Subscription)

    the subscription to delete

Returns:



38
39
40
41
42
# File 'lib/tavern/mock_hub.rb', line 38

def unsubscribe(subscription)
  return if subscription.blank?
  subscriptions.delete subscription
  subscription
end