basic_subscriber 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.ruby-version +1 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +21 -0
- data/README.md +131 -0
- data/Rakefile +6 -0
- data/basic_subscriber.gemspec +36 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/basic_subscriber.rb +7 -0
- data/lib/basic_subscriber/subscriber.rb +82 -0
- data/lib/basic_subscriber/subscription.rb +22 -0
- data/lib/basic_subscriber/version.rb +4 -0
- metadata +102 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2fc4c82ad1c7bbf0bcf58a9ebffd0251811b2207
|
4
|
+
data.tar.gz: 0cb0d9d386f4e97f0fffc8797caef2e38b35cf3a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0b0c8e7688a120c83cd8e93fd2b3db396271206c6784c8abf98f217ea851415a7db8c9d90022522d9edfe9f62ea85138e9f2b04158a6c66caf23cb3c98540040
|
7
|
+
data.tar.gz: 5f5f7f6e38bc3caf971156d2f24a722dfa5ebbb75d392143c46be2f77ea8281ec81fcad3021367019f87bf91ff993a5f3876ce72e6e3b04ddaa2314af6c50d50
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.3.1
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at corlinus@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Yuriy Esaulov
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
# BasicSubscriber
|
2
|
+
|
3
|
+
Basic PubSub realization.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'basic_subscriber'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install basic_subscriber
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
### Subscriber class
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
class MySubscription < BasicSubscriber::Subscriber
|
27
|
+
# register event subscription
|
28
|
+
on :event do
|
29
|
+
# do something...
|
30
|
+
end
|
31
|
+
|
32
|
+
# register anoter event subscription
|
33
|
+
on :event do
|
34
|
+
# do something else...
|
35
|
+
end
|
36
|
+
|
37
|
+
# register subscription to multiple events
|
38
|
+
on :event, :another_event do
|
39
|
+
# do something...
|
40
|
+
end
|
41
|
+
|
42
|
+
# register event in scope
|
43
|
+
scope :my_scope do
|
44
|
+
on :scoped_event do
|
45
|
+
# trigger this event as :'scoped_event.my_scope'
|
46
|
+
end
|
47
|
+
|
48
|
+
# it's also possible to create nested scopes
|
49
|
+
scope :another_scope do
|
50
|
+
on :event do
|
51
|
+
# this is event :'event.another_scope.my_scope'
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
# set scope for all events below
|
57
|
+
scope :my_another_scope
|
58
|
+
|
59
|
+
on :event do
|
60
|
+
# this is event :'event.my_another_scope'
|
61
|
+
end
|
62
|
+
|
63
|
+
on :my_event do
|
64
|
+
# this event is also in :my_another_scope
|
65
|
+
end
|
66
|
+
|
67
|
+
# nested scope will be created if `scope` called twice without block
|
68
|
+
scope :my_scope
|
69
|
+
|
70
|
+
on :event do
|
71
|
+
# this is :'event.my_scope.my_another_scope'
|
72
|
+
end
|
73
|
+
end
|
74
|
+
```
|
75
|
+
|
76
|
+
### Subscription class
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
class MySubscription < BasicSubscriber::Subscription
|
80
|
+
subscribe MySubscriber
|
81
|
+
end
|
82
|
+
```
|
83
|
+
|
84
|
+
To subscribe another subscriptions pass multiple classes to `subscribe` or call it several times
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
subscribe MySubscriber, MyAnotherSubscriber
|
88
|
+
subscribe SomeSubscriber
|
89
|
+
```
|
90
|
+
|
91
|
+
### Trigger event
|
92
|
+
|
93
|
+
To trigger event use
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
MySubscription.trigger :'event.my_scope', some: :data
|
97
|
+
```
|
98
|
+
|
99
|
+
### In subscriber instance
|
100
|
+
|
101
|
+
There are methods available In all event subscriptions
|
102
|
+
|
103
|
+
```ruby
|
104
|
+
# returns hash passed on event trigger
|
105
|
+
payload # => {some: :data}
|
106
|
+
|
107
|
+
# returns full event name
|
108
|
+
event_name # => :'event.my_scope'
|
109
|
+
|
110
|
+
# returns first event name part
|
111
|
+
unscoped_event_name # => :event
|
112
|
+
|
113
|
+
# returns event scope
|
114
|
+
scope_name # => :my_scope
|
115
|
+
```
|
116
|
+
|
117
|
+
## Development
|
118
|
+
|
119
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
120
|
+
|
121
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
122
|
+
|
123
|
+
## Contributing
|
124
|
+
|
125
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/corlinus/basic_subscriber. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
126
|
+
|
127
|
+
|
128
|
+
## License
|
129
|
+
|
130
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
131
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'basic_subscriber/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "basic_subscriber"
|
8
|
+
spec.version = BasicSubscriber::VERSION
|
9
|
+
spec.authors = ["Yuriy Esaulov"]
|
10
|
+
spec.email = ["corlinus@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Basic event subscriber implementation.}
|
13
|
+
spec.description = %q{Basic event subscriber implementation.}
|
14
|
+
spec.homepage = "https://github.com/corlinus/basic_subscriber"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
#if spec.respond_to?(:metadata)
|
20
|
+
# spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
|
21
|
+
#else
|
22
|
+
# raise "RubyGems 2.0 or newer is required to protect against " \
|
23
|
+
# "public gem pushes."
|
24
|
+
#end
|
25
|
+
|
26
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
27
|
+
f.match(%r{^(test|spec|features)/})
|
28
|
+
end
|
29
|
+
spec.bindir = "exe"
|
30
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
31
|
+
spec.require_paths = ["lib"]
|
32
|
+
|
33
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
34
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
35
|
+
spec.add_development_dependency "rspec", "~> 3.5"
|
36
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "basic_subscriber"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module BasicSubscriber
|
3
|
+
class Subscriber
|
4
|
+
NAME_SEPARATOR = '.'
|
5
|
+
attr_reader :event_name, :payload
|
6
|
+
|
7
|
+
class << self
|
8
|
+
def trigger(event_name, **payload)
|
9
|
+
event_name = event_name.to_sym
|
10
|
+
|
11
|
+
subscriber = new event_name, payload
|
12
|
+
subscriptions[event_name].each { |block| subscriber.instance_exec(&block) }
|
13
|
+
nil
|
14
|
+
end
|
15
|
+
|
16
|
+
def on(*event_names, &block)
|
17
|
+
event_names.each { |name| subscriptions[scoped_name(name)].push block }
|
18
|
+
end
|
19
|
+
|
20
|
+
def scope(scope_name)
|
21
|
+
scope_name = scope_name.to_sym
|
22
|
+
if block_given?
|
23
|
+
previous_scope = current_scope.dup
|
24
|
+
subscope scope_name
|
25
|
+
yield
|
26
|
+
self.current_scope = previous_scope
|
27
|
+
else
|
28
|
+
parent_scope = current_scope[1..-1]
|
29
|
+
self.current_scope = parent_scope if parent_scope&.any?
|
30
|
+
subscope scope_name
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def known_events
|
35
|
+
subscriptions.keys
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def subscriptions
|
41
|
+
@subscriptions ||= Hash.new { |h, k| h[k] = [] }
|
42
|
+
end
|
43
|
+
|
44
|
+
def scoped_name(name)
|
45
|
+
([name] + current_scope).join(NAME_SEPARATOR).to_sym
|
46
|
+
end
|
47
|
+
|
48
|
+
def current_scope
|
49
|
+
@current_scope ||= []
|
50
|
+
end
|
51
|
+
|
52
|
+
attr_writer :current_scope
|
53
|
+
|
54
|
+
def subscope(name)
|
55
|
+
@current_scope.unshift name
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def initialize(event_name, **payload)
|
60
|
+
@event_name = event_name
|
61
|
+
@payload = payload
|
62
|
+
end
|
63
|
+
|
64
|
+
def unscoped_event_name
|
65
|
+
@unscoped_event_name ||= split_event_name.first.to_sym
|
66
|
+
end
|
67
|
+
|
68
|
+
def scope_name
|
69
|
+
return @scope_name if defined? @scope_name
|
70
|
+
@scope_name = begin
|
71
|
+
scope_name = split_event_name[2]
|
72
|
+
scope_name == '' ? nil : scope_name.to_sym
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
private
|
77
|
+
|
78
|
+
def split_event_name
|
79
|
+
@split_event_name ||= event_name.to_s.partition NAME_SEPARATOR
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module BasicSubscriber
|
3
|
+
class Subscription
|
4
|
+
class << self
|
5
|
+
def subscribe(*subscriptions)
|
6
|
+
subscriptions.each { |s| s.known_events.each { |e| mounts[e].push(s).uniq! } }
|
7
|
+
end
|
8
|
+
|
9
|
+
def trigger(event_name, **payload)
|
10
|
+
event_name = event_name.to_sym
|
11
|
+
mounts[event_name].each { |subscriber| subscriber.trigger event_name, payload }
|
12
|
+
nil
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def mounts
|
18
|
+
@mounts ||= Hash.new { |h, k| h[k] = [] }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: basic_subscriber
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yuriy Esaulov
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-04-13 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.13'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.13'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.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.5'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.5'
|
55
|
+
description: Basic event subscriber implementation.
|
56
|
+
email:
|
57
|
+
- corlinus@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".ruby-version"
|
65
|
+
- ".travis.yml"
|
66
|
+
- CODE_OF_CONDUCT.md
|
67
|
+
- Gemfile
|
68
|
+
- LICENSE.txt
|
69
|
+
- README.md
|
70
|
+
- Rakefile
|
71
|
+
- basic_subscriber.gemspec
|
72
|
+
- bin/console
|
73
|
+
- bin/setup
|
74
|
+
- lib/basic_subscriber.rb
|
75
|
+
- lib/basic_subscriber/subscriber.rb
|
76
|
+
- lib/basic_subscriber/subscription.rb
|
77
|
+
- lib/basic_subscriber/version.rb
|
78
|
+
homepage: https://github.com/corlinus/basic_subscriber
|
79
|
+
licenses:
|
80
|
+
- MIT
|
81
|
+
metadata: {}
|
82
|
+
post_install_message:
|
83
|
+
rdoc_options: []
|
84
|
+
require_paths:
|
85
|
+
- lib
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
requirements: []
|
97
|
+
rubyforge_project:
|
98
|
+
rubygems_version: 2.5.1
|
99
|
+
signing_key:
|
100
|
+
specification_version: 4
|
101
|
+
summary: Basic event subscriber implementation.
|
102
|
+
test_files: []
|