pubsub 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/Gemfile +10 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +19 -0
- data/Rakefile +54 -0
- data/VERSION +1 -0
- data/lib/pubsub.rb +61 -0
- data/lib/pubsub/pubsub.rb +57 -0
- data/lib/pubsub/string.rb +18 -0
- data/test/helper.rb +18 -0
- data/test/test_pubsub.rb +7 -0
- metadata +129 -0
data/.document
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Max Kazarin
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
= pubsub
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Contributing to pubsub
|
6
|
+
|
7
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
8
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
9
|
+
* Fork the project
|
10
|
+
* Start a feature/bugfix branch
|
11
|
+
* Commit and push until you are happy with your contribution
|
12
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
13
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2011 Max Kazarin. See LICENSE.txt for
|
18
|
+
further details.
|
19
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "pubsub"
|
18
|
+
gem.homepage = "http://github.com/maxkazar/pubsub"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{PubSub pattern}
|
21
|
+
gem.description = %Q{PubSub pattern}
|
22
|
+
gem.email = "maxkazargm@gmail.com"
|
23
|
+
gem.authors = ["Max Kazarin"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
gem.add_dependency "ruby_events", ">=0"
|
26
|
+
end
|
27
|
+
Jeweler::RubygemsDotOrgTasks.new
|
28
|
+
|
29
|
+
require 'rake/testtask'
|
30
|
+
Rake::TestTask.new(:test) do |test|
|
31
|
+
test.libs << 'lib' << 'test'
|
32
|
+
test.pattern = 'test/**/test_*.rb'
|
33
|
+
test.verbose = true
|
34
|
+
end
|
35
|
+
|
36
|
+
require 'rcov/rcovtask'
|
37
|
+
Rcov::RcovTask.new do |test|
|
38
|
+
test.libs << 'test'
|
39
|
+
test.pattern = 'test/**/test_*.rb'
|
40
|
+
test.verbose = true
|
41
|
+
test.rcov_opts << '--exclude "gems/*"'
|
42
|
+
end
|
43
|
+
|
44
|
+
task :default => :test
|
45
|
+
|
46
|
+
require 'rake/rdoctask'
|
47
|
+
Rake::RDocTask.new do |rdoc|
|
48
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
49
|
+
|
50
|
+
rdoc.rdoc_dir = 'rdoc'
|
51
|
+
rdoc.title = "pubsub #{version}"
|
52
|
+
rdoc.rdoc_files.include('README*')
|
53
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
54
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.0
|
data/lib/pubsub.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
require "ruby_events"
|
2
|
+
require File.join(File.dirname(__FILE__), "pubsub/pubsub.rb")
|
3
|
+
|
4
|
+
module DigitalPro
|
5
|
+
module Events
|
6
|
+
module PubSub
|
7
|
+
def subscribe(channel, procs = nil, &block)
|
8
|
+
logger.info "Subscribe #{self.class.name} to #{channel}"
|
9
|
+
|
10
|
+
PubSub.events.listen channel, procs, &block
|
11
|
+
|
12
|
+
procs_collected = []
|
13
|
+
if procs.respond_to?(:each) && procs.respond_to?(:to_a)
|
14
|
+
procs_collected += procs.to_a
|
15
|
+
elsif procs
|
16
|
+
procs_collected << procs
|
17
|
+
end
|
18
|
+
procs_collected << block if block
|
19
|
+
|
20
|
+
channel_handlers[channel] ||= []
|
21
|
+
channel_handlers[channel] += procs_collected
|
22
|
+
end
|
23
|
+
|
24
|
+
def unsubscribe(channel=nil, proc = nil)
|
25
|
+
# unsubscribe from all object channels
|
26
|
+
unless channel
|
27
|
+
channel_handlers.each_key{ |channel_name| unsubscribe channel_name}
|
28
|
+
return
|
29
|
+
end
|
30
|
+
|
31
|
+
if proc
|
32
|
+
logger.info "Unsubscribe #{self.class.name} to #{channel}"
|
33
|
+
PubSub.events.remove channel, proc
|
34
|
+
channel_handlers[channel].delete_if {|stored_event| stored_event == proc} if channel_handlers.include? channel
|
35
|
+
else
|
36
|
+
channel_handlers[channel].each { |proc_handler| unsubscribe channel, proc_handler} if channel_handlers.include? channel
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def publish(channel, *args)
|
41
|
+
logger.info "Publish #{self.class.name} to #{channel} with #{args.to_s}"
|
42
|
+
|
43
|
+
channel.sub_before "/" do |sub_channel|
|
44
|
+
PubSub.events.fire sub_channel, *args
|
45
|
+
end
|
46
|
+
PubSub.events.fire "*", *args
|
47
|
+
end
|
48
|
+
|
49
|
+
protected
|
50
|
+
|
51
|
+
def channel_handlers
|
52
|
+
@channel_handlers ||= {}
|
53
|
+
end
|
54
|
+
|
55
|
+
def PubSub.events
|
56
|
+
@@events ||= RubyEvents::Events.new(self)
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require "ruby_events"
|
2
|
+
require File.join(File.dirname(__FILE__), "string.rb")
|
3
|
+
|
4
|
+
module PubSub
|
5
|
+
def subscribe(channel, procs = nil, &block)
|
6
|
+
logger.info "Subscribe #{self.class.name} to #{channel}"
|
7
|
+
|
8
|
+
PubSub.events.listen channel, procs, &block
|
9
|
+
|
10
|
+
procs_collected = []
|
11
|
+
if procs.respond_to?(:each) && procs.respond_to?(:to_a)
|
12
|
+
procs_collected += procs.to_a
|
13
|
+
elsif procs
|
14
|
+
procs_collected << procs
|
15
|
+
end
|
16
|
+
procs_collected << block if block
|
17
|
+
|
18
|
+
channel_handlers[channel] ||= []
|
19
|
+
channel_handlers[channel] += procs_collected
|
20
|
+
end
|
21
|
+
|
22
|
+
def unsubscribe(channel=nil, proc = nil)
|
23
|
+
# unsubscribe from all object channels
|
24
|
+
unless channel
|
25
|
+
channel_handlers.each_key{ |channel_name| unsubscribe channel_name}
|
26
|
+
return
|
27
|
+
end
|
28
|
+
|
29
|
+
if proc
|
30
|
+
logger.info "Unsubscribe #{self.class.name} to #{channel}"
|
31
|
+
PubSub.events.remove channel, proc
|
32
|
+
channel_handlers[channel].delete_if {|stored_event| stored_event == proc} if channel_handlers.include? channel
|
33
|
+
else
|
34
|
+
channel_handlers[channel].each { |proc_handler| unsubscribe channel, proc_handler} if channel_handlers.include? channel
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def publish(channel, *args)
|
39
|
+
logger.info "Publish #{self.class.name} to #{channel} with #{args.to_s}"
|
40
|
+
|
41
|
+
channel.sub_before "/" do |sub_channel|
|
42
|
+
PubSub.events.fire sub_channel, *args
|
43
|
+
end
|
44
|
+
PubSub.events.fire "*", *args
|
45
|
+
end
|
46
|
+
|
47
|
+
protected
|
48
|
+
|
49
|
+
def channel_handlers
|
50
|
+
@channel_handlers ||= {}
|
51
|
+
end
|
52
|
+
|
53
|
+
def PubSub.events
|
54
|
+
@@events ||= RubyEvents::Events.new(self)
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
class String
|
2
|
+
#Return substring from start to last occurrence separator. In the block form, return all substring
|
3
|
+
# "test/test1/test2".sub_before("/") {|item| puts item}
|
4
|
+
# test/test1/test2
|
5
|
+
# test/test1
|
6
|
+
# test
|
7
|
+
def sub_before(separator)
|
8
|
+
result = self
|
9
|
+
index = size
|
10
|
+
begin
|
11
|
+
result = result[0, index]
|
12
|
+
next if result.empty?
|
13
|
+
break unless block_given?
|
14
|
+
yield result
|
15
|
+
end while index = result.rindex('/')
|
16
|
+
result
|
17
|
+
end
|
18
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'test/unit'
|
11
|
+
require 'shoulda'
|
12
|
+
|
13
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
14
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
15
|
+
require 'pubsub'
|
16
|
+
|
17
|
+
class Test::Unit::TestCase
|
18
|
+
end
|
data/test/test_pubsub.rb
ADDED
metadata
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pubsub
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Max Kazarin
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-07-15 00:00:00.000000000 +04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: ruby_events
|
17
|
+
requirement: &11460780 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '0'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *11460780
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: shoulda
|
28
|
+
requirement: &11458140 !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *11458140
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: bundler
|
39
|
+
requirement: &11419840 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ~>
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: 1.0.0
|
45
|
+
type: :development
|
46
|
+
prerelease: false
|
47
|
+
version_requirements: *11419840
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: jeweler
|
50
|
+
requirement: &11418620 !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ~>
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: 1.6.4
|
56
|
+
type: :development
|
57
|
+
prerelease: false
|
58
|
+
version_requirements: *11418620
|
59
|
+
- !ruby/object:Gem::Dependency
|
60
|
+
name: rcov
|
61
|
+
requirement: &11417000 !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ! '>='
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
type: :development
|
68
|
+
prerelease: false
|
69
|
+
version_requirements: *11417000
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: ruby_events
|
72
|
+
requirement: &11414840 !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
type: :runtime
|
79
|
+
prerelease: false
|
80
|
+
version_requirements: *11414840
|
81
|
+
description: PubSub pattern
|
82
|
+
email: maxkazargm@gmail.com
|
83
|
+
executables: []
|
84
|
+
extensions: []
|
85
|
+
extra_rdoc_files:
|
86
|
+
- LICENSE.txt
|
87
|
+
- README.rdoc
|
88
|
+
files:
|
89
|
+
- .document
|
90
|
+
- Gemfile
|
91
|
+
- LICENSE.txt
|
92
|
+
- README.rdoc
|
93
|
+
- Rakefile
|
94
|
+
- VERSION
|
95
|
+
- lib/pubsub.rb
|
96
|
+
- lib/pubsub/pubsub.rb
|
97
|
+
- lib/pubsub/string.rb
|
98
|
+
- test/helper.rb
|
99
|
+
- test/test_pubsub.rb
|
100
|
+
has_rdoc: true
|
101
|
+
homepage: http://github.com/maxkazar/pubsub
|
102
|
+
licenses:
|
103
|
+
- MIT
|
104
|
+
post_install_message:
|
105
|
+
rdoc_options: []
|
106
|
+
require_paths:
|
107
|
+
- lib
|
108
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
109
|
+
none: false
|
110
|
+
requirements:
|
111
|
+
- - ! '>='
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
segments:
|
115
|
+
- 0
|
116
|
+
hash: 3917047595112901718
|
117
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
118
|
+
none: false
|
119
|
+
requirements:
|
120
|
+
- - ! '>='
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0'
|
123
|
+
requirements: []
|
124
|
+
rubyforge_project:
|
125
|
+
rubygems_version: 1.6.2
|
126
|
+
signing_key:
|
127
|
+
specification_version: 3
|
128
|
+
summary: PubSub pattern
|
129
|
+
test_files: []
|