signals 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +38 -0
- data/Rakefile +1 -0
- data/lib/signals/block_listener.rb +14 -0
- data/lib/signals/publisher.rb +36 -0
- data/lib/signals/version.rb +3 -0
- data/lib/signals.rb +7 -0
- data/signals.gemspec +25 -0
- data/spec/signals/block_listener_spec.rb +9 -0
- data/spec/signals/publisher_spec.rb +72 -0
- data/spec/spec_helper.rb +6 -0
- metadata +105 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 79695d044f17ec3916824ea55ba5551f24fcd4a8
|
4
|
+
data.tar.gz: f1894ecf6cdb059adcf22e9c685b349fca6a7bea
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b02380267c91e36fa2908332cae607fdf463f8d3c0c94755003b98b2de10e5379ff5f59970ce88e0420f083419022a72db0e9776157272ec8ea3ff2802226853
|
7
|
+
data.tar.gz: e78b36011fd2e6195e110e4765424cd5cb44e04286a9e4a1e0aae3eab7d4251981100b227b564e2578da04363bfa2d2684e7841d349914329df5bdd270d1082a
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
signals
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.0.0
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Matthew Johnston
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# Signals
|
2
|
+
|
3
|
+
A light weight publish / subscribe. It is similar to how the gem Wisper works
|
4
|
+
but without extra functionality. This library assumes nothing and concurrency is
|
5
|
+
not a priortiy.
|
6
|
+
|
7
|
+
## Usage
|
8
|
+
|
9
|
+
```rb
|
10
|
+
class Coach
|
11
|
+
include Signals::Publisher
|
12
|
+
|
13
|
+
def run_play
|
14
|
+
broadcast(:v_formation, self)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class Player
|
19
|
+
def v_formation(coach)
|
20
|
+
puts "I'm in position"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
coach = Coach.new
|
25
|
+
player = Player.new
|
26
|
+
|
27
|
+
coach.subscribe(player)
|
28
|
+
|
29
|
+
coach.run_play
|
30
|
+
```
|
31
|
+
|
32
|
+
## Contributing
|
33
|
+
|
34
|
+
1. Fork it
|
35
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
36
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
37
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
38
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Signals
|
2
|
+
|
3
|
+
# This is a Listener that once instantiated, will define a method that is
|
4
|
+
# named what is passed as the action.
|
5
|
+
class BlockListener
|
6
|
+
# @param [String] action
|
7
|
+
def initialize(action, &block)
|
8
|
+
(class << self; self; end;).instance_eval do
|
9
|
+
define_method(action, block)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Signals
|
2
|
+
|
3
|
+
module Publisher
|
4
|
+
# Broadcasts an action to all of the subscribed listeners
|
5
|
+
# @return [Signals::Publisher]
|
6
|
+
def broadcast(action, *args)
|
7
|
+
listeners.each do |listener|
|
8
|
+
if listener.respond_to?(action)
|
9
|
+
listener.send(action, *args)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
self
|
13
|
+
end
|
14
|
+
|
15
|
+
def on(event, &block)
|
16
|
+
listeners << BlockListener.new(event, &block)
|
17
|
+
self
|
18
|
+
end
|
19
|
+
|
20
|
+
# Subscribe a listener to the publisher
|
21
|
+
# @return [Signals::Publisher]
|
22
|
+
def subscribe(listener)
|
23
|
+
listeners << listener
|
24
|
+
self
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
# All of the listeners subscribed to a publisher
|
30
|
+
# @return [Set] a unique set of listeners
|
31
|
+
def listeners
|
32
|
+
@listeners ||= Set.new
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
data/lib/signals.rb
ADDED
data/signals.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'signals/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "signals"
|
8
|
+
spec.version = Signals::VERSION
|
9
|
+
spec.authors = ["Matthew Johnston"]
|
10
|
+
spec.email = ["warmwaffles@gmail.com"]
|
11
|
+
spec.description = %q{A lightweight publish / subscribe library}
|
12
|
+
spec.summary = %q{A lightweight publish / subscribe library}
|
13
|
+
spec.homepage = "https://github.com/warmwaffles/signals"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Signals::Publisher do
|
4
|
+
class DummyPublisher
|
5
|
+
include Signals::Publisher
|
6
|
+
end
|
7
|
+
|
8
|
+
class DummySimpleListener
|
9
|
+
def simple_method
|
10
|
+
true
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class DummyComplexListener
|
15
|
+
def complex_method(a, b)
|
16
|
+
true
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe '#broadcast' do
|
21
|
+
it 'should broadcast to no listeners' do
|
22
|
+
publisher = DummyPublisher.new
|
23
|
+
expect { publisher.broadcast(:simple_method) }.to_not raise_error
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'should broadcast to a simple listener' do
|
27
|
+
publisher = DummyPublisher.new
|
28
|
+
publisher.stub(:listeners).and_return([DummySimpleListener.new])
|
29
|
+
expect { publisher.broadcast(:simple_method) }.to_not raise_error
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should broadcast to a complex listener' do
|
33
|
+
publisher = DummyPublisher.new
|
34
|
+
publisher.stub(:listeners).and_return([DummyComplexListener.new])
|
35
|
+
expect { publisher.broadcast(:complex_method, 1, 2) }.to_not raise_error
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'should broadcast an unsupported event to a listener' do
|
39
|
+
publisher = DummyPublisher.new
|
40
|
+
publisher.stub(:listeners).and_return([DummySimpleListener.new])
|
41
|
+
expect { publisher.broadcast(:does_not_exist) }.to_not raise_error
|
42
|
+
end
|
43
|
+
|
44
|
+
context 'when providing too few arguments' do
|
45
|
+
it 'should raise an argument error' do
|
46
|
+
publisher = DummyPublisher.new
|
47
|
+
publisher.stub(:listeners).and_return([DummyComplexListener.new])
|
48
|
+
expect { publisher.broadcast(:complex_method, 1) }.to raise_error(/wrong number of arguments/)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context 'when providing too many arguments' do
|
53
|
+
it 'should raise an argument error' do
|
54
|
+
publisher = DummyPublisher.new
|
55
|
+
publisher.stub(:listeners).and_return([DummySimpleListener.new])
|
56
|
+
expect { publisher.broadcast(:simple_method, 1) }.to raise_error(/wrong number of arguments/)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe '#subscribe' do
|
62
|
+
it 'should subscribe an object' do
|
63
|
+
publisher = DummyPublisher.new
|
64
|
+
expect { publisher.subscribe(Object.new) }.to_not raise_error
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
describe '#on' do
|
69
|
+
pending
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: signals
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Matthew Johnston
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-07-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: A lightweight publish / subscribe library
|
56
|
+
email:
|
57
|
+
- warmwaffles@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- .rspec
|
64
|
+
- .ruby-gemset
|
65
|
+
- .ruby-version
|
66
|
+
- Gemfile
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- lib/signals.rb
|
71
|
+
- lib/signals/block_listener.rb
|
72
|
+
- lib/signals/publisher.rb
|
73
|
+
- lib/signals/version.rb
|
74
|
+
- signals.gemspec
|
75
|
+
- spec/signals/block_listener_spec.rb
|
76
|
+
- spec/signals/publisher_spec.rb
|
77
|
+
- spec/spec_helper.rb
|
78
|
+
homepage: https://github.com/warmwaffles/signals
|
79
|
+
licenses:
|
80
|
+
- MIT
|
81
|
+
metadata: {}
|
82
|
+
post_install_message:
|
83
|
+
rdoc_options: []
|
84
|
+
require_paths:
|
85
|
+
- lib
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - '>='
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - '>='
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
requirements: []
|
97
|
+
rubyforge_project:
|
98
|
+
rubygems_version: 2.0.3
|
99
|
+
signing_key:
|
100
|
+
specification_version: 4
|
101
|
+
summary: A lightweight publish / subscribe library
|
102
|
+
test_files:
|
103
|
+
- spec/signals/block_listener_spec.rb
|
104
|
+
- spec/signals/publisher_spec.rb
|
105
|
+
- spec/spec_helper.rb
|