🚀 DAY 5 OF LAUNCH WEEK: Introducing Socket Firewall Enterprise.Learn more →
Socket
Book a DemoInstallSign in
Socket

notification_center

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

notification_center

bundlerRubygems
Version
0.3.2
Version published
Maintainers
1
Created
Source

Notification/listener solution for ruby

Features

  • any number of listners
  • only one event of one kind can be fired per request (cache)
  • disable/enable events (nice feature for tests)

Configuration for rails

In your Gemfile

gem 'notification_center'

Cache

To enable cache, in your:

config/initializers/notification_center.rb

NotificationCenter.enable_cache = true # only one event fired in one request scope, default is false

Dont forget to flush cache wach request, for this in your config/application.rb in config section:

config.middleware.use NotificationCenter::Cache

Use

In any class or multiple classes:

class SomeClass
  observe :some_event

  def some_event_handler # any number of args are possible
  end
end

Anywhere in code:

NotificationCenter.post_notification :some_event

Common practice

Create directory app/listeners and put listeners there, like user_listener.rb

class UserListener
  observe :user_did_some_action
  
  def user_did_some_action_handler
    # some complex logic
  end
end

Important

Make sure, that your classes are preloaded! So for app/listeners, put this code to your application.rb

Dir[Rails.root.join + 'app/listeners/*.rb'].map{|f| require f}

For rspec

In your spec_helper.rb

require 'notification_center/rspec_helpers'

Then you can use notifications: false in describe, context, it. Like:

describe User, notifications: false do

it "should do smth", notifications: false do

FAQs

Package last updated on 25 Jul 2012

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts