event_aggregator 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +61 -0
- data/Rakefile +1 -0
- data/event_aggregator.gemspec +23 -0
- data/lib/event_aggregator/aggregator.rb +27 -0
- data/lib/event_aggregator/listener.rb +105 -0
- data/lib/event_aggregator/message.rb +15 -0
- data/lib/event_aggregator/version.rb +3 -0
- data/lib/event_aggregator.rb +8 -0
- metadata +89 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Stephan Eriksen
|
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,61 @@
|
|
1
|
+
# EventAggregator
|
2
|
+
|
3
|
+
The 'event_aggregator' gem is a gem for using the event aggregator pattern in Ruby.
|
4
|
+
|
5
|
+
An event aggregator is essentially a message passing service that aims at decoupeling object communication and that lets
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'event_aggregator'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install event_aggregator
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
#!/usr/bin/ruby
|
24
|
+
|
25
|
+
require "rubygems"
|
26
|
+
require "event_aggregator"
|
27
|
+
|
28
|
+
class Foo
|
29
|
+
include EventAggregator::Listener
|
30
|
+
def initialize()
|
31
|
+
message_type_to_recieve_add( "foo", lambda{|data| puts "bar" } )
|
32
|
+
|
33
|
+
message_type_to_recieve_add( "foo2", method(:handle_message) )
|
34
|
+
end
|
35
|
+
|
36
|
+
def handle_message(data)
|
37
|
+
puts data
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
f = Foo.new
|
42
|
+
|
43
|
+
EventAggregator::Message.new("foo", "data").publish
|
44
|
+
#=> bar
|
45
|
+
EventAggregator::Message.new("foo2", "data").publish
|
46
|
+
#=> data
|
47
|
+
EventAggregator::Message.new("foo3", "data").publish
|
48
|
+
#=>
|
49
|
+
|
50
|
+
## Contributing
|
51
|
+
|
52
|
+
1. Fork it
|
53
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
54
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
55
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
56
|
+
5. Create new Pull Request
|
57
|
+
|
58
|
+
## Todo:
|
59
|
+
|
60
|
+
- Enable threaded message passing for higher performance.
|
61
|
+
- Improving the readme and documentation in the gem.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'event_aggregator/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "event_aggregator"
|
8
|
+
spec.version = EventAggregator::VERSION
|
9
|
+
spec.authors = ["Stephan Eriksen"]
|
10
|
+
spec.email = ["stephan.n.eriksen@gmail.com"]
|
11
|
+
spec.description = %q{A simple Ruby event aggregator.}
|
12
|
+
spec.summary = %q{Event aggregator for Ruby.}
|
13
|
+
spec.homepage = "https://github.com/stephan-nordnes-eriksen/event_aggregator"
|
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
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module EventAggregator
|
2
|
+
class Aggregator
|
3
|
+
@@listeners = Hash.new([])
|
4
|
+
|
5
|
+
@@message_types = Hash.new
|
6
|
+
|
7
|
+
def self.register( listener, message_type )
|
8
|
+
@@listeners[message_type] << listener unless @@listeners[message_type].include?(listener)
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.unregister( listener, message_type )
|
12
|
+
@@listeners[message_type].delete listener
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.message_publish ( message )
|
16
|
+
return "Not a valid message" unless message.is_a? EventAggregator::Message
|
17
|
+
|
18
|
+
@@listeners[message.message_type].each do |l|
|
19
|
+
l.recieve_message message
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.register_message_type(message_type)
|
24
|
+
@@message_types[message_types] = [] unless @@message_types[message_types]
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,105 @@
|
|
1
|
+
module EventAggregator
|
2
|
+
# Public: A module you can include or extend to recieve messages from
|
3
|
+
# the event Aggregator system.
|
4
|
+
#
|
5
|
+
# Examples
|
6
|
+
#
|
7
|
+
# class Foo
|
8
|
+
# Include Listener
|
9
|
+
# ...
|
10
|
+
# def initialize()
|
11
|
+
# ...
|
12
|
+
# message_type_to_recieve_add( "foo", lambda{ puts "bar" } )
|
13
|
+
# end
|
14
|
+
# ...
|
15
|
+
# end
|
16
|
+
#
|
17
|
+
module Listener
|
18
|
+
|
19
|
+
#event_listener_listens_to = Hash.new() #This actually sets Listener.event_listener_listens_to = Hash.new, not the instance
|
20
|
+
|
21
|
+
|
22
|
+
# Public: This is the callback method when the module is extended.
|
23
|
+
# Using this to setup the last few things before the event listener can start.
|
24
|
+
#
|
25
|
+
# base - The class object for the class extening the Listener module
|
26
|
+
#
|
27
|
+
# Returns nil
|
28
|
+
def self.extended(base)
|
29
|
+
# Initialize module.
|
30
|
+
# add_auto_register_procedure(base) #Depricated now, but it is utterly awesome that you can do this.
|
31
|
+
end
|
32
|
+
|
33
|
+
# Public: This is the callback method when the module is included.
|
34
|
+
# Using this to setup the last few things before the event listener can start.
|
35
|
+
#
|
36
|
+
# base - The class object for the class including the Listener module
|
37
|
+
#
|
38
|
+
# Returns nil
|
39
|
+
def self.included(base)
|
40
|
+
# Initialize module.
|
41
|
+
# add_auto_register_procedure(base) #Depricated now, but it is utterly awesome that you can do this.
|
42
|
+
end
|
43
|
+
|
44
|
+
|
45
|
+
# Public: DEPRICATED: Adding extra initialize to the class so that we make sure new objects are added to the EventAggregators registry.
|
46
|
+
# This whole hack-deal is possibly not nescessary. Can be omited with a simple "register" when you add a new "recieve message_type"
|
47
|
+
#
|
48
|
+
# base - The class object for the class including the Listener module
|
49
|
+
#
|
50
|
+
# Returns nil
|
51
|
+
def self.add_auto_register_procedure(base)
|
52
|
+
base.class_eval do
|
53
|
+
# back up method's name
|
54
|
+
alias_method :old_initialize, :initialize
|
55
|
+
|
56
|
+
# replace the old method with a new version which adds the Aggregator registry
|
57
|
+
def initialize(*args)
|
58
|
+
Aggregator.register self
|
59
|
+
old_initialize(*args)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def recieve_message( message )
|
65
|
+
m = event_listener_listens_to[message.message_type]
|
66
|
+
|
67
|
+
m.call(message.data) if m.respond_to? :call #Should not need the check here, however who knows what kind of conurrency issues we might have.
|
68
|
+
#This will probably become hotpath, so having the extra check can be problematic.
|
69
|
+
end
|
70
|
+
|
71
|
+
private
|
72
|
+
def event_listener_listens_to
|
73
|
+
@event_listener_listens_to ||= Hash.new
|
74
|
+
end
|
75
|
+
# public: Use to add message types you want to recieve. Overwirte existing callback when existing message type is given.
|
76
|
+
#
|
77
|
+
# message_type - A string indicating the message type you want to recieve from the event aggregrator. Can actually be anything.
|
78
|
+
# callback - The method that will be invoked every time this message type is recieved. Must have: callback.respond_to? :call #=> true
|
79
|
+
#
|
80
|
+
# Examples
|
81
|
+
#
|
82
|
+
# message_type_to_recieve_add("foo", method(:my_class_method))
|
83
|
+
# message_type_to_recieve_add("foo", lambda { puts "foo" })
|
84
|
+
# message_type_to_recieve_add("foo", Proc.new { puts "foo" })
|
85
|
+
#
|
86
|
+
def message_type_to_recieve_add( message_type, callback )
|
87
|
+
event_listener_listens_to[message_type] = callback #unless event_listener_listens_to[message_type] #It makes more sence to overwrite in the case it already exists.
|
88
|
+
Aggregator.register( self, message_type )
|
89
|
+
end
|
90
|
+
|
91
|
+
# Public: Used to remove a certain type of message from your listening types. Messages of this specific type will no longer
|
92
|
+
# invoke any callbacks.
|
93
|
+
#
|
94
|
+
# message_type -A string indicating the message type you no longer want to recieve.
|
95
|
+
#
|
96
|
+
# Examples
|
97
|
+
#
|
98
|
+
# message_type_to_recieve_remove("foo")
|
99
|
+
#
|
100
|
+
def message_type_to_recieve_remove( message_type )
|
101
|
+
event_listener_listens_to[message_type] = nil
|
102
|
+
Aggregator.unregister(self, message_type)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module EventAggregator
|
2
|
+
class Message
|
3
|
+
attr_accessor :message_type, :data
|
4
|
+
@message_type = nil
|
5
|
+
@data = nil
|
6
|
+
def initialize(message_type, data)
|
7
|
+
@message_type = message_type
|
8
|
+
@data = data
|
9
|
+
end
|
10
|
+
|
11
|
+
def publish
|
12
|
+
Aggregator.message_publish( self )
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: event_aggregator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Stephan Eriksen
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-12-09 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
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
|
+
description: A simple Ruby event aggregator.
|
47
|
+
email:
|
48
|
+
- stephan.n.eriksen@gmail.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- Gemfile
|
55
|
+
- LICENSE.txt
|
56
|
+
- README.md
|
57
|
+
- Rakefile
|
58
|
+
- event_aggregator.gemspec
|
59
|
+
- lib/event_aggregator.rb
|
60
|
+
- lib/event_aggregator/aggregator.rb
|
61
|
+
- lib/event_aggregator/listener.rb
|
62
|
+
- lib/event_aggregator/message.rb
|
63
|
+
- lib/event_aggregator/version.rb
|
64
|
+
homepage: https://github.com/stephan-nordnes-eriksen/event_aggregator
|
65
|
+
licenses:
|
66
|
+
- MIT
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
requirements: []
|
84
|
+
rubyforge_project:
|
85
|
+
rubygems_version: 1.8.25
|
86
|
+
signing_key:
|
87
|
+
specification_version: 3
|
88
|
+
summary: Event aggregator for Ruby.
|
89
|
+
test_files: []
|