shouter 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f01d5e611309eff3f12fc296e56ba81bbf46a71c
4
- data.tar.gz: 312e00888703e49abc114f2840a8503c3162bdef
3
+ metadata.gz: e691c8ec76258396c97ae227875d55e77dbf8635
4
+ data.tar.gz: 820d5e90db24d53f64d0c25799bad6e67f7882b3
5
5
  SHA512:
6
- metadata.gz: 636653c15d3730feb4d45f944e4f0d3f161cb5c6bc6d5edabf78247b69e77f9539222df61ed074b225797eadb4f84b3783aee7d6033836b833ec0d0ac8da48f9
7
- data.tar.gz: a55ead88d51bef5703734ee883834c6ca9bde9c9a1d3367292c3414e125c1f0dbb60a9c7d1bb7e5f4eaf50a2e07d0698e6ddc87b3374868d262112ad3a07d591
6
+ metadata.gz: 42071e236df2752dab9146506f476b617f1ee74948247a1fc272c359c8b5e46e55998546a808beba4339545d907f0b87f6099dc3ec336a57e79eaf31d8fa7718
7
+ data.tar.gz: 7dc791ab3bbffd37dcf122343e49e44d694fde4ecbf38491893d388281c9d6c4f7cfa5db6480da73e5d6cc901a3ac9f4df164debdca10ee6650267dca0140526
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Shouter
2
- [![Build Status](https://travis-ci.org/wizardone/bouncer.svg?branch=master)](https://travis-ci.org/wizardone/bouncer)
3
- [![codecov](https://codecov.io/gh/wizardone/bouncer/branch/master/graph/badge.svg)](https://codecov.io/gh/wizardone/bouncer)
2
+ [![Build Status](https://travis-ci.org/wizardone/shouter.svg?branch=master)](https://travis-ci.org/wizardone/shouter)
3
+ [![codecov](https://codecov.io/gh/wizardone/shouter/branch/master/graph/badge.svg)](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.
@@ -1,4 +1,6 @@
1
1
  require 'shouter/version'
2
+ require 'shouter/hook'
3
+ require 'shouter/guard'
2
4
  require 'shouter/listener'
3
5
  require 'shouter/store'
4
6
 
@@ -0,0 +1,11 @@
1
+ module Shouter
2
+ class Guard
3
+
4
+ class << self
5
+ def call(guard)
6
+ return true unless guard
7
+ guard.call
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module Shouter
2
+ class Hook
3
+
4
+ class << self
5
+ def call(callback)
6
+ # TODO: remove redundant ifs
7
+ callback.call if callback.is_a?(Proc)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -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
@@ -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
- klass = listener.object
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
 
@@ -1,3 +1,3 @@
1
1
  module Shouter
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
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.0
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-01-10 00:00:00.000000000 Z
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.5.1
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