Project

circulate

0.0
No commit activity in last 3 years
No release in over 3 years
Allows any class to be a publisher of or subscriber to a notification
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

 Project Readme

Circulate

This gem lets you circulate messages around the ActiveSupport::Notifications pubsub system. It provides a syntax for publishing calls to instance methods and subscribing to them through class methods.

Installation

gem install circulate

Dependencies

The gem relies on active_support, but not the entire Rails stack. It also requires Ruby 2.0.0-p195+. Note that Ruby 2.0.0-p0 will not work because of a bug in Module.prepend.

Example

class ChicagoTribune
    include Circulate::Publisher

    publish :daily_edition

    def daily_edition
        puts "delivered to your door!"
    end
end

class Reader
    include Circulate::Subscriber

    subscribe :daily_edition

    def self.daily_edition(tribune)
        puts "Tribune edition #{tribune} received!"
    end
end

tribune = ChicagoTribune.new
tribune.daily_edition

Outputs...

delivered to your door!
Tribune edition #<ChicagoTribune:0x007fd78350d1d0> received!

Why do this?

So classes can be "poked" when a method is called on an instance. Sometimes you don't want or need to have an instance object sitting around waiting for a notification. Also, it removes you from having to keep track of notifications by String name, and you don't have to deal with the somewhat cumbersome ActiveSupport::Notifications syntax.

Testing

The gem also includes RSpec matchers for your test suite. To use them add require 'circulate/matchers' to your spec_helper. Then, in your tests, you can:

describe ChicagoTribune do
    it { should publish :daily_edition }
end

describe Reader do
    it { should subscribe_to :daily_edition }
end