Project

spine-hub

0.0
No commit activity in last 3 years
No release in over 3 years
Spine hub for Ruby applications.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

 Project Readme

Spine::Hub

Gem Version Dependency Status Test Coverage Code Climate security Inline docs Codeship Status for rspine/hub

Provides Publish/Subscribe pattern.

Installation

To install it, add the gem to your Gemfile:

gem 'spine-hub'

Then run bundle. If you're not using Bundler, just gem install spine-hub.

Usage

class Service
  include Spine::Hub::Publisher

  def action
    publish(:action_executing)
    # ...
    publish(:action_executed, 'OK')
  end
end

class Listener
  include Spine::Hub::Subscriber

  def on_action_executing
    puts 'Executing action'
  end
end

service = Service.new
service.subscribe(Listener.new)
service.on(:action_executed) do |status|
  puts "Action executed: #{ status }"
end
service.action

There is also Spine::Hub::Repeater, which repeats subscribed events. If you need to get more control over subscriber event handling, implement notify method:

class MySubscriber
  def notify(event, *arguments)
    # Handle events
  end
end

When global subscribers are needed, extend your publisher with Spine::Hub::Subcriptions::Global module. It also works with Repeater module.

class Service
  include Spine::Hub::Publisher
  include Spine::Hub::Subscriptions::Global

  def action
    # ...
  end
end

For registering to global scope:

Spine::Hub.subscribe(MySubscriber.new)
Spine::Hub.on(:my_event) do |argument|
  # ...
end