publish_subscribe 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f0ddee05e2ded219d1134378668b704b6c5c118f
4
+ data.tar.gz: ac06d2ffa79920a79a6e70b23b66470c958f4195
5
+ SHA512:
6
+ metadata.gz: bcd376826a63d405e5c21156994bc836609d135967fd603306a32de12a7e65fd5639bdb9ddd33d604d39f3dda27d4024817e4caa02216a8fea7a3c5820df22bf
7
+ data.tar.gz: 769485f9d6358ce04ee9b2bd076360dd77196356ccbab2754ffe00952ddbb48851ffa2cd2d2de50e6894c6a0fb8364668b8a235e79a0b1c97da1655daf9dd91e
data/.gitignore ADDED
@@ -0,0 +1,22 @@
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
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in publish_subscribe.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Cameron Martin
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,51 @@
1
+ # PublishSubscribe
2
+
3
+ Simple Publish/Subscribe pattern for ruby objects. It is like ruby's Observable module, but uses channels.
4
+
5
+ Adding subscriptions is not thread-safe, but if anyone wants this just open an issue and I'll add it.
6
+
7
+ It adds a public message `subscribe`, and two private methods, `publish` and `subscriptions`,
8
+ and an instance variable, `@_subscriptions`, to classes that mix in this module.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ gem 'publish_subscribe'
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install publish_subscribe
23
+
24
+ ## Usage
25
+
26
+ ```ruby
27
+ class Publisher
28
+ include PublishSubscribe
29
+
30
+ def publish_message
31
+ publish :channel, :argument
32
+ end
33
+ end
34
+
35
+ publisher = Publisher.new
36
+
37
+ publisher.subscribe :channel do |argument|
38
+ puts argument
39
+ end
40
+
41
+ publisher.publish_message
42
+ # :argument
43
+ ```
44
+
45
+ ## Contributing
46
+
47
+ 1. Fork it ( https://github.com/cameron-martin/publish_subscribe/fork )
48
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
49
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
50
+ 4. Push to the branch (`git push origin my-new-feature`)
51
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,33 @@
1
+ module PublishSubscribe
2
+
3
+ def subscribe(channel, &block)
4
+ subscriptions.add(channel, block)
5
+ end
6
+
7
+ private
8
+
9
+ def publish(channel, *args)
10
+ subscriptions.get(channel).each do |proc|
11
+ proc.call(*args)
12
+ end
13
+ end
14
+
15
+ def subscriptions
16
+ @_subscriptions ||= SubscriptionRegistry.new
17
+ end
18
+
19
+ class SubscriptionRegistry
20
+ def initialize
21
+ @subscriptions = {}
22
+ end
23
+
24
+ def add(channel, proc)
25
+ (@subscriptions[channel] ||= []) << proc
26
+ end
27
+
28
+ def get(channel)
29
+ @subscriptions[channel] || []
30
+ end
31
+ end
32
+
33
+ end
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'publish_subscribe'
7
+ spec.version = '0.0.1'
8
+ spec.authors = ['Cameron Martin']
9
+ spec.email = ['cameronmartin123@gmail.com']
10
+ spec.summary = %q{Simple Publish/Subscribe for ruby objects}
11
+ spec.homepage = 'https://github.com/cameron-martin/publish_subscribe'
12
+ spec.license = 'MIT'
13
+
14
+ spec.files = `git ls-files -z`.split("\x0")
15
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
16
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
+ spec.require_paths = ['lib']
18
+
19
+ spec.add_development_dependency 'bundler', '~> 1.6'
20
+ spec.add_development_dependency 'rake'
21
+ spec.add_development_dependency 'rspec', '~> 3'
22
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ describe PublishSubscribe do
4
+
5
+ class Publisher
6
+ include PublishSubscribe
7
+
8
+ def publish_message
9
+ publish :channel, :argument
10
+ end
11
+ end
12
+
13
+ let(:publisher) { Publisher.new }
14
+
15
+ it 'allows subscriptions' do
16
+ expect { publisher.subscribe(:channel) { } }.to_not raise_error
17
+ end
18
+
19
+ it 'sends messages to listeners' do
20
+ listener1 = ->() { }
21
+ listener2 = ->() { }
22
+
23
+ allow(listener1).to receive(:call)
24
+ allow(listener2).to receive(:call)
25
+
26
+ publisher.subscribe(:channel, &listener1)
27
+ publisher.subscribe(:channel, &listener2)
28
+ publisher.publish_message
29
+
30
+ expect(listener1).to have_received(:call).with(:argument)
31
+ expect(listener2).to have_received(:call).with(:argument)
32
+ end
33
+
34
+ end
@@ -0,0 +1 @@
1
+ require 'publish_subscribe'
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: publish_subscribe
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Cameron Martin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-17 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.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
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: '3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '3'
55
+ description:
56
+ email:
57
+ - cameronmartin123@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - lib/publish_subscribe.rb
68
+ - publish_subscribe.gemspec
69
+ - spec/publish_subscribe_spec.rb
70
+ - spec/spec_helper.rb
71
+ homepage: https://github.com/cameron-martin/publish_subscribe
72
+ licenses:
73
+ - MIT
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 2.2.1
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: Simple Publish/Subscribe for ruby objects
95
+ test_files:
96
+ - spec/publish_subscribe_spec.rb
97
+ - spec/spec_helper.rb
98
+ has_rdoc: