shouter 0.1.0 → 0.1.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 +4 -4
- data/README.md +24 -2
- data/lib/shouter.rb +2 -0
- data/lib/shouter/guard.rb +11 -0
- data/lib/shouter/hook.rb +11 -0
- data/lib/shouter/listener.rb +28 -0
- data/lib/shouter/store.rb +2 -6
- data/lib/shouter/version.rb +1 -1
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e691c8ec76258396c97ae227875d55e77dbf8635
|
4
|
+
data.tar.gz: 820d5e90db24d53f64d0c25799bad6e67f7882b3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 42071e236df2752dab9146506f476b617f1ee74948247a1fc272c359c8b5e46e55998546a808beba4339545d907f0b87f6099dc3ec336a57e79eaf31d8fa7718
|
7
|
+
data.tar.gz: 7dc791ab3bbffd37dcf122343e49e44d694fde4ecbf38491893d388281c9d6c4f7cfa5db6480da73e5d6cc901a3ac9f4df164debdca10ee6650267dca0140526
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Shouter
|
2
|
-
[](https://travis-ci.org/wizardone/shouter)
|
3
|
+
[](https://codecov.io/gh/wizardone/shouter)
|
4
4
|
|
5
5
|
`Shouter` is a very simple and lightweight publish/subscription DSL for
|
6
6
|
Ruby applications.
|
@@ -71,6 +71,28 @@ A.publish(:my_scope, :on_change) do
|
|
71
71
|
end
|
72
72
|
```
|
73
73
|
|
74
|
+
To unsubscribe single or multiple objects you can call the `unsubscribe` method
|
75
|
+
```ruby
|
76
|
+
A.unsubscribe(Listener1, Listener2)
|
77
|
+
```
|
78
|
+
|
79
|
+
Since version `1.0.1`:
|
80
|
+
|
81
|
+
Alternatively you can pass the callback option when subscribing.
|
82
|
+
It accepts a callable object:
|
83
|
+
```ruby
|
84
|
+
subscribe(Listener.new, for: :my_scope, callback: ->() { perform_async })
|
85
|
+
```
|
86
|
+
|
87
|
+
You can now add a `guard` clause which will stop the execution of events
|
88
|
+
unless it returns `true`. Guard clauses accept callable objects
|
89
|
+
```ruby
|
90
|
+
subscribe(Listener.new, for: :my_scope, guard: Proc.new { some_listener.respond_to?(:my_method) })
|
91
|
+
```
|
92
|
+
|
93
|
+
|
94
|
+
The `clear` method removes all listeners from the store.
|
95
|
+
|
74
96
|
## Development
|
75
97
|
|
76
98
|
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.
|
data/lib/shouter.rb
CHANGED
data/lib/shouter/hook.rb
ADDED
data/lib/shouter/listener.rb
CHANGED
@@ -16,12 +16,40 @@ module Shouter
|
|
16
16
|
@options = options
|
17
17
|
end
|
18
18
|
|
19
|
+
def notify(event, args, &block)
|
20
|
+
return unless object.respond_to?(event)
|
21
|
+
if fire_guard!
|
22
|
+
object.public_send(event, *args)
|
23
|
+
fire_hook!(callback || block)
|
24
|
+
|
25
|
+
Store.unregister(object) if single?
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
19
29
|
def for?(scope)
|
20
30
|
options[:scope] == scope
|
21
31
|
end
|
22
32
|
|
33
|
+
private
|
34
|
+
|
35
|
+
def fire_hook!(callback)
|
36
|
+
Shouter::Hook.(callback)
|
37
|
+
end
|
38
|
+
|
39
|
+
def fire_guard!
|
40
|
+
Shouter::Guard.(guard)
|
41
|
+
end
|
42
|
+
|
43
|
+
def callback
|
44
|
+
options[:callback]
|
45
|
+
end
|
46
|
+
|
23
47
|
def single?
|
24
48
|
options[:single] == true
|
25
49
|
end
|
50
|
+
|
51
|
+
def guard
|
52
|
+
options[:guard]
|
53
|
+
end
|
26
54
|
end
|
27
55
|
end
|
data/lib/shouter/store.rb
CHANGED
@@ -33,15 +33,11 @@ module Shouter
|
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
|
-
def notify(scope, event, args)
|
36
|
+
def notify(scope, event, args, &block)
|
37
37
|
return if listeners.empty?
|
38
38
|
|
39
39
|
listeners.select { |listener| listener.for?(scope) }.each do |listener|
|
40
|
-
|
41
|
-
klass.public_send(event, *args) if klass.respond_to?(event)
|
42
|
-
# Serves as callback
|
43
|
-
yield if block_given?
|
44
|
-
unregister(klass) if listener.single?
|
40
|
+
listener.notify(event, args, &block)
|
45
41
|
end
|
46
42
|
end
|
47
43
|
|
data/lib/shouter/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: shouter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stefan Slaveykov
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-06-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -70,6 +70,8 @@ files:
|
|
70
70
|
- bin/console
|
71
71
|
- bin/setup
|
72
72
|
- lib/shouter.rb
|
73
|
+
- lib/shouter/guard.rb
|
74
|
+
- lib/shouter/hook.rb
|
73
75
|
- lib/shouter/listener.rb
|
74
76
|
- lib/shouter/store.rb
|
75
77
|
- lib/shouter/version.rb
|
@@ -94,7 +96,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
94
96
|
version: '0'
|
95
97
|
requirements: []
|
96
98
|
rubyforge_project:
|
97
|
-
rubygems_version: 2.
|
99
|
+
rubygems_version: 2.6.8
|
98
100
|
signing_key:
|
99
101
|
specification_version: 4
|
100
102
|
summary: Small publish/subscription based system in Ruby
|