wisper 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'guard'
6
+ gem 'guard-rspec'
7
+ gem 'rb-fsevent', '~>0.9.1'
8
+
9
+ gem 'flay'
data/Guardfile ADDED
@@ -0,0 +1,8 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec' do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+ end
data/README.md ADDED
@@ -0,0 +1,100 @@
1
+ # Wisper
2
+
3
+ Simple pub/sub for Ruby objects
4
+
5
+ While this is not dependent on Rails in any way it was extracted from a Rails
6
+ project and is used as an alternative to ActiveRecord callbacks and Observers.
7
+
8
+ The problem with callbacks and Observers is that they always happen. How many
9
+ times have you wanted to do `User.create` without firing off a welcome email?
10
+
11
+ It is also super useful for integrating web socket notifications, statistics
12
+ and activity streams in to your controller layer without coupling them to your
13
+ models.
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ gem 'wisper'
20
+
21
+ ## Usage
22
+
23
+ ```
24
+ class CreateThing
25
+ include Wisper
26
+
27
+ def execute(attributes)
28
+ thing = Thing.new(attributes)
29
+ if thing.valid?
30
+ thing.save!
31
+ broadcast(:create_thing_successful, thing)
32
+ else
33
+ broadcast(:create_thing_failed, thing)
34
+ end
35
+ end
36
+ end
37
+
38
+ class ThingsController < ApplicationController
39
+ def create
40
+ command = CreateThing.new
41
+
42
+ command.add_listener(PusherListener.new)
43
+ command.add_listener(ActivityListener.new)
44
+ command.add_listener(StatisticsListener.new)
45
+
46
+ command.respond_to(:create_thing_successful) do |thing|
47
+ redirect_to thing
48
+ end
49
+
50
+ command.respond_to(:create_thing_failed) do |thing|
51
+ @thing = thing
52
+ render :action => :new
53
+ end
54
+
55
+ command.execute(params[:thing])
56
+ end
57
+ end
58
+
59
+ class PusherListener
60
+ def create_thing_successful(thing)
61
+ # ...
62
+ end
63
+ end
64
+
65
+ class ActivityListener
66
+ def create_thing_successful(thing)
67
+ # ...
68
+ end
69
+ end
70
+
71
+ class StatisticsListener
72
+ def create_thing_successful(thing)
73
+ # ...
74
+ end
75
+ end
76
+ ```
77
+
78
+ ## License
79
+
80
+ (The MIT License)
81
+
82
+ Copyright (c) 2013 Kris Leech
83
+
84
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
85
+ this software and associated documentation files (the 'Software'), to deal in
86
+ the Software without restriction, including without limitation the rights to
87
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
88
+ of the Software, and to permit persons to whom the Software is furnished to do
89
+ so, subject to the following conditions:
90
+
91
+ The above copyright notice and this permission notice shall be included in all
92
+ copies or substantial portions of the Software.
93
+
94
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
95
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
96
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
97
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
98
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
99
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
100
+ SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/lib/wisper.rb ADDED
@@ -0,0 +1,65 @@
1
+ require "wisper/version"
2
+
3
+ module Wisper
4
+ include ActiveSupport::Concern
5
+
6
+ def listeners
7
+ @listeners ||= Set.new
8
+ end
9
+
10
+ def add_listener(listener, options = {})
11
+ listeners << ObjectRegistration.new(listener, options)
12
+ end
13
+
14
+ def add_block_listener(options = {}, &block)
15
+ listeners << BlockRegistration.new(block, options)
16
+ end
17
+
18
+ # sugar
19
+ def respond_to(event, &block)
20
+ listeners << BlockRegistration.new(block, :on => event)
21
+ end
22
+
23
+ class BlockRegistration
24
+ attr_reader :on, :listener
25
+
26
+ def initialize(block, options)
27
+ @listener = block
28
+ @on = Array(options.fetch(:on) { 'all' }).map(&:to_s)
29
+ end
30
+
31
+ def broadcast(event, *args)
32
+ if on.include?(event) || on.include?('all')
33
+ listener.call(*args)
34
+ end
35
+ end
36
+ end
37
+
38
+ class ObjectRegistration
39
+ attr_reader :on, :listener
40
+
41
+ def initialize(listener, options)
42
+ @listener = listener
43
+ @method = options[:method]
44
+ @on = Array(options.fetch(:on) { 'all' }).map(&:to_s)
45
+ end
46
+
47
+ def broadcast(event, *args)
48
+ if (on.include?(event) || on.include?('all')) && listener.respond_to?(event)
49
+ listener.public_send(event, *args)
50
+ end
51
+ end
52
+ end
53
+
54
+ private
55
+
56
+ def broadcast(event, *args)
57
+ listeners.each do | listener |
58
+ listener.broadcast(clean_event(event), *args)
59
+ end
60
+ end
61
+
62
+ def clean_event(event)
63
+ event.to_s.gsub('-', '_')
64
+ end
65
+ end
@@ -0,0 +1,3 @@
1
+ module Wisper
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ # Example
4
+ class MyCommand
5
+ include Wisper
6
+
7
+ def execute(be_successful)
8
+ if be_successful
9
+ broadcast('success', 'hello')
10
+ else
11
+ broadcast('failure', 'world')
12
+ end
13
+ end
14
+ end
15
+
16
+ describe Wisper do
17
+
18
+ it 'subscribes object to all published events' do
19
+ listener = double('listener')
20
+ listener.should_receive(:success).with('hello')
21
+
22
+ command = MyCommand.new
23
+
24
+ command.add_listener(listener)
25
+
26
+ command.execute(true)
27
+ end
28
+
29
+ it 'subscribes block to all published events' do
30
+ insider = double('Insider')
31
+ insider.should_receive(:render).with('hello')
32
+
33
+ command = MyCommand.new
34
+
35
+ command.add_block_listener do |message|
36
+ insider.render(message)
37
+ end
38
+
39
+ command.execute(true)
40
+ end
41
+ end
@@ -0,0 +1,66 @@
1
+ require 'spec_helper'
2
+
3
+ describe Wisper do
4
+ let(:listener) { double('listener') }
5
+ let(:publisher) { Object.new.extend(Wisper) }
6
+
7
+ describe '.add_listener' do
8
+ it 'subscribes listener to all published events' do
9
+ listener.should_receive(:this_happened)
10
+ listener.should_receive(:so_did_this)
11
+
12
+ publisher.add_listener(listener)
13
+
14
+ publisher.send(:broadcast, 'this_happened')
15
+ publisher.send(:broadcast, 'so_did_this')
16
+ end
17
+
18
+ it 'subscribes listener to selected events' do
19
+ listener.should_receive(:this_happened)
20
+ listener.stub(:so_did_this)
21
+ listener.should_not_receive(:so_did_this)
22
+
23
+ listener.respond_to?(:so_did_this).should be_true
24
+
25
+ publisher.add_listener(listener, :on => 'this_happened')
26
+
27
+ publisher.send(:broadcast, 'this_happened')
28
+ publisher.send(:broadcast, 'so_did_this')
29
+ end
30
+
31
+ it 'does not receive events it can not respond to' do
32
+ listener.should_not_receive(:so_did_this)
33
+ listener.stub(:respond_to?, false)
34
+
35
+ listener.respond_to?(:so_did_this).should be_false
36
+
37
+ publisher.add_listener(listener, :on => 'so_did_this')
38
+
39
+ publisher.send(:broadcast, 'so_did_this')
40
+ end
41
+ end
42
+
43
+ describe 'Block listeners' do
44
+ it 'subscribes block to all events' do
45
+ insider = double('insider')
46
+ insider.should_receive(:it_happened)
47
+
48
+ publisher.add_block_listener do
49
+ insider.it_happened
50
+ end
51
+
52
+ publisher.send(:broadcast, 'something_happened')
53
+ end
54
+
55
+ it 'subscribes block to selected events' do
56
+ insider = double('insider')
57
+ insider.should_not_receive(:it_happened)
58
+
59
+ publisher.add_block_listener(:on => 'this_thing_happened') do
60
+ insider.it_happened
61
+ end
62
+
63
+ publisher.send(:broadcast, 'something_happened')
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,12 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
3
+
4
+ require 'active_support'
5
+ require 'wisper'
6
+
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+ config.order = 'random'
12
+ end
data/wisper.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'wisper/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "wisper"
8
+ gem.version = Wisper::VERSION
9
+ gem.authors = ["Kris Leech"]
10
+ gem.email = ["kris.leech@gmail.com"]
11
+ gem.description = %q{pub/sub for Ruby objects}
12
+ gem.summary = %q{pub/sub for Ruby objects}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency 'active_support'
21
+
22
+ gem.add_development_dependency 'rake'
23
+ gem.add_development_dependency 'rspec'
24
+ gem.add_development_dependency 'simplecov'
25
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wisper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kris Leech
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: active_support
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
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: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '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: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
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
+ - !ruby/object:Gem::Dependency
63
+ name: simplecov
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: pub/sub for Ruby objects
79
+ email:
80
+ - kris.leech@gmail.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - .rspec
87
+ - Gemfile
88
+ - Guardfile
89
+ - README.md
90
+ - Rakefile
91
+ - lib/wisper.rb
92
+ - lib/wisper/version.rb
93
+ - spec/lib/integration_spec.rb
94
+ - spec/lib/wisper_spec.rb
95
+ - spec/spec_helper.rb
96
+ - wisper.gemspec
97
+ homepage: ''
98
+ licenses: []
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ! '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ segments:
110
+ - 0
111
+ hash: -961177691427387453
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ segments:
119
+ - 0
120
+ hash: -961177691427387453
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 1.8.23
124
+ signing_key:
125
+ specification_version: 3
126
+ summary: pub/sub for Ruby objects
127
+ test_files:
128
+ - spec/lib/integration_spec.rb
129
+ - spec/lib/wisper_spec.rb
130
+ - spec/spec_helper.rb