message-pubsub 1.0.0
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.
- data/.gitignore +17 -0
- data/Gemfile +12 -0
- data/LICENSE +22 -0
- data/README.md +30 -0
- data/Rakefile +2 -0
- data/lib/message-pubsub.rb +7 -0
- data/lib/message/pub_sub.rb +42 -0
- data/lib/message/version.rb +5 -0
- data/message-pubsub.gemspec +17 -0
- data/spec/pub_sub_spec.rb +37 -0
- data/spec/spec_helper.rb +14 -0
- metadata +59 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2012 Prasath Venkatraman
|
|
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,30 @@
|
|
|
1
|
+
# Message::Pubsub
|
|
2
|
+
|
|
3
|
+
A simple Ruby implementation of publish subscribe design pattern.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
$ gem install message-pubsub
|
|
7
|
+
|
|
8
|
+
## Simple Example
|
|
9
|
+
|
|
10
|
+
class Foo
|
|
11
|
+
extend Message::Handler
|
|
12
|
+
subscribe :something, lambda { |message| receive_message(message) } #Subscibes to event something
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
class Bar
|
|
16
|
+
extend Message::Handler
|
|
17
|
+
subscribe :something, lambda { |message| receive_message(message) } #Subscibes to same event something
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
Message::PubSub.publish(:something, "foo bar")
|
|
21
|
+
|
|
22
|
+
## Contributing
|
|
23
|
+
|
|
24
|
+
1. Fork it
|
|
25
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
26
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
|
27
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
|
28
|
+
5. Create new Pull Request
|
|
29
|
+
|
|
30
|
+
|
data/Rakefile
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
module Message
|
|
2
|
+
module Handler
|
|
3
|
+
def subscribe(event, block = Proc.new {})
|
|
4
|
+
Message::PubSub.subscribe(self, event)
|
|
5
|
+
define_singleton_method(event) { |args| block.call(args) }
|
|
6
|
+
end
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
class PubSubError < StandardError;end
|
|
10
|
+
|
|
11
|
+
class PubSub
|
|
12
|
+
cattr_accessor :queue
|
|
13
|
+
self.queue = HashWithIndifferentAccess.new
|
|
14
|
+
|
|
15
|
+
class << self
|
|
16
|
+
def subscribe(subscriber, event)
|
|
17
|
+
queue[event] = Array.wrap(queue[event]).push(subscriber)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def publish(event, message)
|
|
21
|
+
queue[event].each { |subscriber| subscriber.send(event, message) }
|
|
22
|
+
rescue Exception => e
|
|
23
|
+
raise Message::PubSubError.new({:error => e})
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def subscribers
|
|
27
|
+
queue.keys
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def has_subscribers?
|
|
31
|
+
subscribers.size > 0
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
|
2
|
+
require File.expand_path('../lib/message/version', __FILE__)
|
|
3
|
+
|
|
4
|
+
Gem::Specification.new do |gem|
|
|
5
|
+
gem.authors = ["Prasath Venkatraman"]
|
|
6
|
+
gem.email = ["ask4prasath@gmail.com"]
|
|
7
|
+
gem.description = 'Ruby implementation of Publish subscribe design pattern'
|
|
8
|
+
gem.summary = 'Ruby implementation of Publish subscribe design pattern'
|
|
9
|
+
gem.homepage = ""
|
|
10
|
+
|
|
11
|
+
gem.files = `git ls-files`.split($\)
|
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
|
14
|
+
gem.name = "message-pubsub"
|
|
15
|
+
gem.require_paths = ["lib"]
|
|
16
|
+
gem.version = Message::PubSub::VERSION
|
|
17
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe 'Message::PubSub' do
|
|
4
|
+
|
|
5
|
+
it 'should have a default queue' do
|
|
6
|
+
Message::PubSub.queue.should eq(HashWithIndifferentAccess.new())
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
it 'should subscribe events' do
|
|
10
|
+
Message::PubSub.subscribe(TestSubscriber, :complete)
|
|
11
|
+
Message::PubSub.subscribers.should eq(['complete'])
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
it 'should return num of subscribers' do
|
|
15
|
+
Message::PubSub.subscribe(TestSubscriber, :complete)
|
|
16
|
+
Message::PubSub.has_subscribers?.should be_true
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
context 'Message pubsub Publish' do
|
|
20
|
+
before(:all) do
|
|
21
|
+
class Foo
|
|
22
|
+
extend Message::Handler
|
|
23
|
+
subscribe :something, lambda { |message| receive_message(message) }
|
|
24
|
+
|
|
25
|
+
def self.receive_message(message)
|
|
26
|
+
message
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it 'should receive message when it is subscribed' do
|
|
32
|
+
Foo.expects(:receive_message).with("foo bar")
|
|
33
|
+
Message::PubSub.publish(:something, "foo bar")
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
end
|
data/spec/spec_helper.rb
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
require 'rubygems'
|
|
2
|
+
require 'bundler/setup'
|
|
3
|
+
require 'active_support/all'
|
|
4
|
+
|
|
5
|
+
require File.expand_path('../../lib/message-pubsub', __FILE__)
|
|
6
|
+
require 'mocha'
|
|
7
|
+
|
|
8
|
+
RSpec.configure do |config|
|
|
9
|
+
config.color_enabled = true
|
|
10
|
+
config.mock_framework = :mocha
|
|
11
|
+
config.formatter = 'documentation'
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
class TestSubscriber; end
|
metadata
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: message-pubsub
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
prerelease:
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- Prasath Venkatraman
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2012-07-24 00:00:00.000000000 Z
|
|
13
|
+
dependencies: []
|
|
14
|
+
description: Ruby implementation of Publish subscribe design pattern
|
|
15
|
+
email:
|
|
16
|
+
- ask4prasath@gmail.com
|
|
17
|
+
executables: []
|
|
18
|
+
extensions: []
|
|
19
|
+
extra_rdoc_files: []
|
|
20
|
+
files:
|
|
21
|
+
- .gitignore
|
|
22
|
+
- Gemfile
|
|
23
|
+
- LICENSE
|
|
24
|
+
- README.md
|
|
25
|
+
- Rakefile
|
|
26
|
+
- lib/message-pubsub.rb
|
|
27
|
+
- lib/message/pub_sub.rb
|
|
28
|
+
- lib/message/version.rb
|
|
29
|
+
- message-pubsub.gemspec
|
|
30
|
+
- spec/pub_sub_spec.rb
|
|
31
|
+
- spec/spec_helper.rb
|
|
32
|
+
homepage: ''
|
|
33
|
+
licenses: []
|
|
34
|
+
post_install_message:
|
|
35
|
+
rdoc_options: []
|
|
36
|
+
require_paths:
|
|
37
|
+
- lib
|
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
39
|
+
none: false
|
|
40
|
+
requirements:
|
|
41
|
+
- - ! '>='
|
|
42
|
+
- !ruby/object:Gem::Version
|
|
43
|
+
version: '0'
|
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
45
|
+
none: false
|
|
46
|
+
requirements:
|
|
47
|
+
- - ! '>='
|
|
48
|
+
- !ruby/object:Gem::Version
|
|
49
|
+
version: '0'
|
|
50
|
+
requirements: []
|
|
51
|
+
rubyforge_project:
|
|
52
|
+
rubygems_version: 1.8.22
|
|
53
|
+
signing_key:
|
|
54
|
+
specification_version: 3
|
|
55
|
+
summary: Ruby implementation of Publish subscribe design pattern
|
|
56
|
+
test_files:
|
|
57
|
+
- spec/pub_sub_spec.rb
|
|
58
|
+
- spec/spec_helper.rb
|
|
59
|
+
has_rdoc:
|