notification_center 0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +3 -0
- data/Gemfile.lock +33 -0
- data/README.md +81 -0
- data/Rakefile +10 -0
- data/lib/notification_center/configuration.rb +22 -0
- data/lib/notification_center/core_ext/module.rb +17 -0
- data/lib/notification_center/rspec_helpers.rb +11 -0
- data/lib/notification_center/version.rb +3 -0
- data/lib/notification_center.rb +42 -0
- data/notification_center.gemspec +22 -0
- data/spec/lib/notification_center_spec.rb +41 -0
- data/spec/spec_helper.rb +12 -0
- metadata +108 -0
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
notification_center (0.2)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: http://rubygems.org/
|
8
|
+
specs:
|
9
|
+
coderay (1.0.7)
|
10
|
+
diff-lcs (1.1.3)
|
11
|
+
method_source (0.7.1)
|
12
|
+
pry (0.9.9.6)
|
13
|
+
coderay (~> 1.0.5)
|
14
|
+
method_source (~> 0.7.1)
|
15
|
+
slop (>= 2.4.4, < 3)
|
16
|
+
rspec (2.10.0)
|
17
|
+
rspec-core (~> 2.10.0)
|
18
|
+
rspec-expectations (~> 2.10.0)
|
19
|
+
rspec-mocks (~> 2.10.0)
|
20
|
+
rspec-core (2.10.1)
|
21
|
+
rspec-expectations (2.10.0)
|
22
|
+
diff-lcs (~> 1.1.3)
|
23
|
+
rspec-mocks (2.10.1)
|
24
|
+
slop (2.4.4)
|
25
|
+
|
26
|
+
PLATFORMS
|
27
|
+
ruby
|
28
|
+
|
29
|
+
DEPENDENCIES
|
30
|
+
bundler
|
31
|
+
notification_center!
|
32
|
+
pry
|
33
|
+
rspec (~> 2.10.0)
|
data/README.md
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
## Notification/listener solution for ruby
|
2
|
+
|
3
|
+
## Features
|
4
|
+
|
5
|
+
* any number of listners
|
6
|
+
* only one event of one kind can be fired per request (cache)
|
7
|
+
* disable/enable events (nice feature for tests)
|
8
|
+
|
9
|
+
## Configuration for rails
|
10
|
+
|
11
|
+
In your Gemfile
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'notification_center', github: 'Houdini/notification_center'
|
15
|
+
```
|
16
|
+
|
17
|
+
config/initializers/notification_center.rb
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
NotificationCenter.configure do
|
21
|
+
enable_cache = true # only one event fired in one request scope, default is false
|
22
|
+
end
|
23
|
+
```
|
24
|
+
|
25
|
+
## Use
|
26
|
+
|
27
|
+
In any class or multiple classes:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
class SomeClass
|
31
|
+
observe :some_event
|
32
|
+
|
33
|
+
def some_event_handler # any number of args are possible
|
34
|
+
end
|
35
|
+
end
|
36
|
+
```
|
37
|
+
|
38
|
+
Anywhere in code:
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
NotificationCenter.post_notification :some_event
|
42
|
+
```
|
43
|
+
|
44
|
+
## Common practice
|
45
|
+
|
46
|
+
Create directory app/listeners and put listeners there, like user_listener.rb
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
class UserListener
|
50
|
+
observe :user_did_some_action
|
51
|
+
|
52
|
+
def user_did_some_action_handler
|
53
|
+
# some complex logic
|
54
|
+
end
|
55
|
+
end
|
56
|
+
```
|
57
|
+
|
58
|
+
## Important
|
59
|
+
|
60
|
+
Make sure, that your classes are preloaded!
|
61
|
+
So for app/listeners, put this code to your application.rb
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
Dir[Rails.root.join + 'app/listeners/*.rb'].map{|f| require f}
|
65
|
+
```
|
66
|
+
|
67
|
+
## For rspec
|
68
|
+
|
69
|
+
In your spec_helper.rb
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
require 'notification_center/rspec_helpers'
|
73
|
+
```
|
74
|
+
|
75
|
+
Then you can use notifications: false in describe, context, it. Like:
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
describe User, notifications: false do
|
79
|
+
|
80
|
+
it "should do smth", notifications: false do
|
81
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
module NotificationCenter
|
2
|
+
module Configuration
|
3
|
+
VALID_OPTIONS_KEYS = [:enable_cache, :enable_notifications, :method_suffix]
|
4
|
+
|
5
|
+
DEFAULT_ENABLE_CACHE = false
|
6
|
+
DEFAULT_ENABLE_NOTIFICATIONS = true
|
7
|
+
DEFAULT_METHOD_SUFFIX = 'handler'
|
8
|
+
|
9
|
+
attr_accessor *VALID_OPTIONS_KEYS
|
10
|
+
|
11
|
+
def self.extended base
|
12
|
+
base.reset
|
13
|
+
end
|
14
|
+
|
15
|
+
def reset
|
16
|
+
self.enable_cache = DEFAULT_ENABLE_CACHE
|
17
|
+
self.enable_notifications = DEFAULT_ENABLE_NOTIFICATIONS
|
18
|
+
self.method_suffix = DEFAULT_METHOD_SUFFIX
|
19
|
+
self
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module NotificationCenter
|
2
|
+
module CoreExt
|
3
|
+
module Module
|
4
|
+
def observe *events
|
5
|
+
stored_events = NotificationCenter.events.dup
|
6
|
+
for event in events
|
7
|
+
next if stored_events.has_key? event
|
8
|
+
stored_events[event] += Array self
|
9
|
+
stored_events[event] = stored_events[event].uniq
|
10
|
+
end
|
11
|
+
NotificationCenter.events = stored_events
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
::Module.__send__ :include, NotificationCenter::CoreExt::Module
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module NotificationCenter
|
2
|
+
module RspecHelpers
|
3
|
+
Rspec.configure do |config|
|
4
|
+
config.around(:each) do |spec|
|
5
|
+
NotificationCenter.enable_notifications = false if spec.metadata[:notifications] == false
|
6
|
+
spec.call
|
7
|
+
NotificationCenter.enable_notifications = true if spec.metadata[:notifications] == false
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'notification_center/core_ext/module'
|
2
|
+
require 'notification_center/configuration'
|
3
|
+
require 'set'
|
4
|
+
|
5
|
+
module NotificationCenter
|
6
|
+
extend Configuration
|
7
|
+
class << self
|
8
|
+
def events; @@events end
|
9
|
+
def events= hash; @@events = hash end
|
10
|
+
|
11
|
+
def flush_cache!; @cache = nil end
|
12
|
+
|
13
|
+
def _initialize_event_store
|
14
|
+
@@events = Hash.new Array.new
|
15
|
+
end
|
16
|
+
alias :forget_observers! :_initialize_event_store
|
17
|
+
|
18
|
+
def post_notification *args
|
19
|
+
event = args.shift
|
20
|
+
if enable_notifications
|
21
|
+
enable_cache ? _with_cache(event){ _notify! event, *args } : _notify!(event, *args)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def _notify! event, *args
|
26
|
+
event_handler = "#{event}_#{method_suffix}"
|
27
|
+
receivers = @@events[event]
|
28
|
+
for receiver in receivers
|
29
|
+
receiver.send event_handler, *args
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def _with_cache event
|
34
|
+
@cache ||= Set.new
|
35
|
+
unless @cache.include? event
|
36
|
+
yield
|
37
|
+
@cache << event
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
self._initialize_event_store
|
42
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "notification_center/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "notification_center"
|
7
|
+
s.version = NotificationCener::VERSION.dup
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Dmitrii Golub"]
|
10
|
+
s.email = ["dmitrii.golub@gmail.com"]
|
11
|
+
s.homepage = "http://github.com/Houdini/notification_center"
|
12
|
+
s.summary = %q{Observer pattern for ruby}
|
13
|
+
|
14
|
+
s.files = `git ls-files`.split("\n")
|
15
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
16
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
|
19
|
+
s.add_development_dependency("bundler")
|
20
|
+
s.add_development_dependency("rspec", "~> 2.10.0")
|
21
|
+
s.add_development_dependency("pry")
|
22
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
|
4
|
+
describe NotificationCenter do
|
5
|
+
describe '.post_notification' do
|
6
|
+
before { NotificationCenter.flush_cache! }
|
7
|
+
|
8
|
+
it "should fire event handler" do
|
9
|
+
EventListener = Class.new { observe :event }
|
10
|
+
EventListener.should_receive(:event_handler)
|
11
|
+
NotificationCenter.post_notification :event
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should not raise any exception when no event" do
|
15
|
+
NotificationCenter.post_notification :imaginary_event
|
16
|
+
end
|
17
|
+
|
18
|
+
context 'when cache disabled' do
|
19
|
+
before { NotificationCenter.enable_cache = false }
|
20
|
+
it "should not fire event twice" do
|
21
|
+
EventListener = Class.new { observe :event }
|
22
|
+
EventListener.should_receive(:event_handler).twice
|
23
|
+
|
24
|
+
NotificationCenter.post_notification :event
|
25
|
+
NotificationCenter.post_notification :event
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'when cache enabled' do
|
30
|
+
before { NotificationCenter.enable_cache = true }
|
31
|
+
|
32
|
+
it "should not fire event twice" do
|
33
|
+
EventListener = Class.new { observe :event }
|
34
|
+
EventListener.should_receive :event_handler
|
35
|
+
|
36
|
+
NotificationCenter.post_notification :event
|
37
|
+
NotificationCenter.post_notification :event
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
|
3
|
+
|
4
|
+
require 'pry'
|
5
|
+
require 'notification_center'
|
6
|
+
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.before do
|
9
|
+
NotificationCenter.forget_observers!
|
10
|
+
NotificationCenter.reset
|
11
|
+
end
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: notification_center
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.2'
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Dmitrii Golub
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-07-10 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 2.10.0
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 2.10.0
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: pry
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description:
|
63
|
+
email:
|
64
|
+
- dmitrii.golub@gmail.com
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- Gemfile
|
70
|
+
- Gemfile.lock
|
71
|
+
- README.md
|
72
|
+
- Rakefile
|
73
|
+
- lib/notification_center.rb
|
74
|
+
- lib/notification_center/configuration.rb
|
75
|
+
- lib/notification_center/core_ext/module.rb
|
76
|
+
- lib/notification_center/rspec_helpers.rb
|
77
|
+
- lib/notification_center/version.rb
|
78
|
+
- notification_center.gemspec
|
79
|
+
- spec/lib/notification_center_spec.rb
|
80
|
+
- spec/spec_helper.rb
|
81
|
+
homepage: http://github.com/Houdini/notification_center
|
82
|
+
licenses: []
|
83
|
+
post_install_message:
|
84
|
+
rdoc_options: []
|
85
|
+
require_paths:
|
86
|
+
- lib
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
+
none: false
|
89
|
+
requirements:
|
90
|
+
- - ! '>='
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
none: false
|
95
|
+
requirements:
|
96
|
+
- - ! '>='
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
requirements: []
|
100
|
+
rubyforge_project:
|
101
|
+
rubygems_version: 1.8.24
|
102
|
+
signing_key:
|
103
|
+
specification_version: 3
|
104
|
+
summary: Observer pattern for ruby
|
105
|
+
test_files:
|
106
|
+
- spec/lib/notification_center_spec.rb
|
107
|
+
- spec/spec_helper.rb
|
108
|
+
has_rdoc:
|