token_of_fire 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 04dee45d965496063c510a9d07689a2d006df740
4
+ data.tar.gz: 954c1fd598d8393048c3642d80f46697f9d5b3fe
5
+ SHA512:
6
+ metadata.gz: d86ace5e9fe0dac9ff30e1656130e0abede6380a33a439474a850d65f00d3505a6853ce198ceafdd1d25ea5aad4d0c0c174fe3a6b6141542019bedc52f1dd2e5
7
+ data.tar.gz: 66882e4779a1ec6d9b5554c8bd0bddb61bc269255a4fab15781726ccaf497011d96c96ec64aa8e724faa5839ab98dec96c3aef0f6390d488db8c2ebfeb934aa5
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in token_of_fire.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 TODO: Write your name
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,29 @@
1
+ # TokenOfFire
2
+
3
+ Celluloid based Event bus
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'token_of_fire'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install token_of_fire
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( http://github.com/<my-github-username>/token_of_fire/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,67 @@
1
+ require 'celluloid'
2
+
3
+ module TokenOfFire
4
+ Event = Struct.new(:name, :scope, :payload)
5
+
6
+ class EventBus
7
+ include Celluloid
8
+ exclusive :subscribe
9
+
10
+ def initialize
11
+ @subscriptions = TokenOfFire::Subscriptions.new
12
+ self
13
+ end
14
+
15
+ def scope(filter={})
16
+ TokenOfFire::Scope.new(Celluloid::Actor.current, filter, true)
17
+ end
18
+
19
+ def subscribe(event_name, scope, handler, handler_method)
20
+ uuid = @subscriptions.subscribe(event_name, scope, handler, handler_method)
21
+ # $stdout.puts "-- Subscribe event_name: #{event_name} #{uuid}"
22
+ uuid
23
+ end
24
+
25
+ def unsubscribe(uuid)
26
+ # $stdout.puts "-- Unsubscribe uuid:#{uuid}"
27
+ @subscriptions.unsubscribe(uuid)
28
+ end
29
+
30
+ def subscriptions
31
+ @subscriptions.get_subscriptions
32
+ end
33
+
34
+ def trigger(event)
35
+ subscriptions = @subscriptions.get_subscriptions(event.name, event.scope)
36
+ if subscriptions and subscriptions.size > 0
37
+ subscriptions.each_value do |subscription|
38
+ subscription[:handler].send(subscription[:method_name], event.payload)
39
+ end
40
+ else
41
+ # $stdout.puts "No subscriptions found for: scope: #{event.scope}, event_name: #{event.name}"
42
+ end
43
+ end
44
+
45
+ def perform(event_name, scope, payload, sync_type)
46
+ event = make_event(event_name, scope, payload)
47
+ case sync_type
48
+ when :async
49
+ async.trigger(event)
50
+ when :sync
51
+ trigger(event)
52
+ end
53
+ end
54
+
55
+ def fire(event_name, scope, payload)
56
+ perform(event_name, scope, payload, :async)
57
+ end
58
+
59
+ def fire_sync(event_name, scope, payload)
60
+ perform(event_name, scope, payload, :sync)
61
+ end
62
+
63
+ def make_event(event_name, scope, payload)
64
+ TokenOfFire::Event.new(event_name, scope, payload)
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,46 @@
1
+ module TokenOfFire
2
+ class Scope
3
+ def initialize(event_bus, filter={}, single)
4
+ @event_bus = event_bus
5
+ raise RuntimeError, "Provide hash please" unless filter.is_a? Hash
6
+ @filter = filter
7
+ @subscriptions = []
8
+ end
9
+
10
+ def attach_subscriptions(list)
11
+ list.each do |t|
12
+ attach(t[0], t[1], t[2])
13
+ end
14
+ end
15
+
16
+ def attach(event_name, handler, method_name)
17
+ @subscriptions.push({
18
+ :event_name => event_name,
19
+ :handler => handler,
20
+ :method_name => method_name
21
+ })
22
+ @subscriptions
23
+ end
24
+
25
+ def subscriptions
26
+ @subscriptions
27
+ end
28
+ # alias attach_subscription()
29
+
30
+ # def dettach(:name_of_event)
31
+ # @scope_a.dettach(:name_of_event)
32
+
33
+ def token
34
+ TokenOfFire::Token.new(@event_bus, self)
35
+ # starts with all subscriptions previously attached to a scope
36
+ end
37
+
38
+ def unique_token
39
+ TokenOfFire::Token.new(@event_bus, self, true)
40
+ end
41
+
42
+ def filter
43
+ @filter
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,55 @@
1
+ require 'set'
2
+
3
+ module TokenOfFire
4
+ class Subscriptions
5
+
6
+ def initialize(subscriptions={})
7
+ @subscriptions = subscriptions
8
+
9
+ @subscriptions_by_event_name = {}
10
+ end
11
+
12
+ def subscriptions
13
+ @subscriptions
14
+ end
15
+
16
+ def subscribe(event_name, scope, handler, method_name)
17
+ gen_uuid = SecureRandom.uuid
18
+ @subscriptions[gen_uuid] ||= {}
19
+ @subscriptions[gen_uuid] = {
20
+ :event_name => event_name,
21
+ :scope => scope,
22
+ :handler => handler,
23
+ :method_name => method_name
24
+ }
25
+ # $stdout.puts "subscription added: #{@subscriptions[gen_uuid]}"
26
+
27
+ @subscriptions_by_event_name[event_name] ||= []
28
+ @subscriptions_by_event_name[event_name] << gen_uuid
29
+
30
+ gen_uuid
31
+ end
32
+
33
+ def unsubscribe(uuid)
34
+ @subscriptions.reject! { |k| k == uuid }
35
+ @subscriptions_by_event_name.each do |s_by_event_name|
36
+ s_by_event_name.reject! { |v| v == uuid }
37
+ end
38
+ end
39
+
40
+ def get_subscriptions(event_name=nil, scope=nil)
41
+ # $stdout.puts "---\nSearching for: #{event_name}"
42
+ # $stdout.puts " scope: #{scope}"
43
+ uuids = @subscriptions_by_event_name[event_name]
44
+ return [] if not uuids
45
+ @subscriptions.select { |k| uuids.include? k }.select { |k, v|
46
+ # $stdout.puts " check match: #{v[:scope]}"
47
+ s1 = Set.new(scope)
48
+ s2 = Set.new(v[:scope])
49
+ value = s1.subset? s2
50
+ # $stdout.puts " #{value}"
51
+ value
52
+ }
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,40 @@
1
+ module TokenOfFire
2
+ class Token
3
+ def initialize(event_bus, scope, unique=false)
4
+ @event_bus = event_bus
5
+ @scope = scope
6
+ @uuid = SecureRandom.uuid
7
+ @unique = unique
8
+ subscribe_to_all
9
+ end
10
+
11
+ def filter
12
+ filter = @scope.filter.dup
13
+ filter.merge!(:uuid => @uuid) if @unique
14
+ filter
15
+ end
16
+
17
+ def fire(event_name, payload)
18
+ @event_bus.fire(event_name, filter, payload)
19
+ end
20
+
21
+ def fire_sync
22
+ @event_bus.fire_sync(event_name, filter, payload)
23
+ end
24
+
25
+ def release
26
+ @subscription_uuids.each do |uuid|
27
+ @event_bus.unsubscribe(uuid)
28
+ end
29
+ end
30
+
31
+ private
32
+ def subscribe_to_all
33
+ @subscription_uuids = @scope.subscriptions.collect do |subscription|
34
+ @event_bus.subscribe(
35
+ subscription[:event_name], filter, subscription[:handler], subscription[:method_name]
36
+ )
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,3 @@
1
+ module TokenOfFire
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,11 @@
1
+ require "token_of_fire/event_bus"
2
+ require "token_of_fire/scope"
3
+ require "token_of_fire/subscriptions"
4
+ require "token_of_fire/token"
5
+ require "token_of_fire/version"
6
+
7
+ module TokenOfFire
8
+ def self.bus
9
+ TokenOfFire::EventBus.new
10
+ end
11
+ end
@@ -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 'token_of_fire/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "token_of_fire"
8
+ spec.version = TokenOfFire::VERSION
9
+ spec.authors = ["Fernando Dario Alonso"]
10
+ spec.email = ["krakatoa1987@gmail.com"]
11
+ spec.summary = %q{Celluloid based Events manager}
12
+ spec.description = %q{Celluloid based Events manager (experimental)}
13
+ spec.homepage = ""
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_runtime_dependency "celluloid"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.5"
24
+ spec.add_development_dependency "rake"
25
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: token_of_fire
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Fernando Dario Alonso
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: celluloid
15
+ version_requirements: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ requirement: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - '>='
23
+ - !ruby/object:Gem::Version
24
+ version: '0'
25
+ prerelease: false
26
+ type: :runtime
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.5'
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ~>
37
+ - !ruby/object:Gem::Version
38
+ version: '1.5'
39
+ prerelease: false
40
+ type: :development
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ version_requirements: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirement: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ prerelease: false
54
+ type: :development
55
+ description: Celluloid based Events manager (experimental)
56
+ email:
57
+ - krakatoa1987@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - lib/token_of_fire.rb
69
+ - lib/token_of_fire/event_bus.rb
70
+ - lib/token_of_fire/scope.rb
71
+ - lib/token_of_fire/subscriptions.rb
72
+ - lib/token_of_fire/token.rb
73
+ - lib/token_of_fire/version.rb
74
+ - token_of_fire.gemspec
75
+ homepage: ''
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.1.9
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Celluloid based Events manager
99
+ test_files: []