resugan 0.1.13 → 0.1.14

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ba18c2c784e29ca7493bbb579f33a75c31b41ae6
4
- data.tar.gz: e47f70a4106daa799f18e3df9a157b2ba42ab50a
3
+ metadata.gz: 9529648f6c3c3ea15ec3bbd5de41528befc5b4ee
4
+ data.tar.gz: 54d84c99dad40bef7d122d853fecc369fcf12c1f
5
5
  SHA512:
6
- metadata.gz: c9a89df390d48392b0526686d6aaf863f87c6e53385ef6d8449ccd2acfa7a1a38b7da5e7045a2032eab8c13ee8e5685a8a2085286556bb5bb7e354cb087c308c
7
- data.tar.gz: 1d8be70ec1599f09e87b1380eaaf98ef195f8948c08c19ba5a994114a05e94487bf86f15f45ccce6f74ae98e72d348dec9039f6f40029e67e20a40c735f64350
6
+ metadata.gz: 14d17a89b022eeaba39cd2b719691464f71ff43d0a7f1bd9c9eba0e140c8f08ee5009b9305ec8f58fb211595c04e88795c856bf3b4afd590d17b72cfaf2e83ae
7
+ data.tar.gz: bdd07b56ed36bc09446ba3de39cbf995fae37a879879b065937e0872a138e28f5829fa6683ebb09cffcc331705a9fab19976a655a7c2d1df5320f420b2c151fb
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ = Version 0.1.14
2
+ * events will only be resolved at the top level for nested resugan blocks unless resugan! is used
3
+ * Config is set using Resugan::Kernel.config do |c| c..... end
4
+
1
5
  = Version 0.1.11
2
6
  * Allow listeners to attach to multiple namespaces to allow for DRY'er code
3
7
 
data/README.md CHANGED
@@ -147,6 +147,57 @@ listener that belongs to 2 namespaces
147
147
  hello! someone said hay!
148
148
  listener that belongs to 2 namespaces
149
149
  ```
150
+
151
+ ### Behavior of nested resugan blocks
152
+
153
+ Resugan will by default only resolve events at the outermost resugan context of
154
+ a namespace.
155
+
156
+ ```ruby
157
+ _listener :event1 do |array_of_params|
158
+ puts "hello! event 1"
159
+ end
160
+
161
+ _listener :event2, namespace: "group2" do |array_of_params|
162
+ puts "hello! event 1"
163
+ end
164
+
165
+ resugan {
166
+ _fire :event1
167
+
168
+ resugan {
169
+ _fire :event2
170
+ }
171
+ }
172
+ ```
173
+
174
+ If there are nested resugan blocks note that event dispatch will occur at the
175
+ outermost context. Output will be:
176
+
177
+ ```
178
+ hello! event 1
179
+ hello! event 2
180
+ ```
181
+
182
+ To force the innermost block to immediately resolve, use resugan! instead (or set Kernel.config.reuse_top_level_context = false globally):
183
+
184
+ ```ruby
185
+ resugan {
186
+ _fire :event1
187
+
188
+ resugan! {
189
+ _fire :event2
190
+ }
191
+ }
192
+ ```
193
+
194
+ Since the inner block will be resolved first, Output will be:
195
+
196
+ ```
197
+ hello! event 2
198
+ hello! event 1
199
+ ```
200
+
150
201
  ## Unique Listeners
151
202
 
152
203
  the _listener always creates a new listener for an event, so if it so happens that
@@ -207,7 +258,9 @@ This allows you to use various queue backends per namespace, like resugan-worker
207
258
  Sometimes you need to track where events are fired. You can do so by enabling line tracing:
208
259
 
209
260
  ```ruby
210
- Resugan::Kernel.enable_line_trace true
261
+ Resugan::Kernel.config do |c|
262
+ c.line_trace_enabled = true
263
+ end
211
264
  ```
212
265
 
213
266
  Line source should now be passed as params everytime you fire an event. You can also
@@ -247,6 +300,12 @@ Can also be used as a sample on how to extend resugan.
247
300
 
248
301
  https://github.com/jedld/resugan-worker
249
302
 
303
+ ### Testing
304
+
305
+ RSpec helpers are available:
306
+
307
+ https://github.com/jedld/resugan-rspec
308
+
250
309
  ## Similar Projects
251
310
 
252
311
  wisper (https://github.com/krisleech/wisper) - An excellent gem that focuses on a coupled pub-sub model. Though its global listeners somehow have the same effect though in a syntactically different way.
@@ -1,7 +1,7 @@
1
1
  module Resugan
2
2
  class Context
3
3
  def initialize(namespace = '')
4
- @namespace = namespace
4
+ @namespace = namespace.to_s
5
5
  @events = {}
6
6
  end
7
7
 
@@ -1,39 +1,53 @@
1
1
  module Resugan
2
+ class Config
3
+ attr_accessor :reuse_top_level_context, :warn_no_context_events, :line_trace_enabled, :default_dispatcher
4
+
5
+ def initialize
6
+ @reuse_top_level_context = true
7
+ @warn_no_context_events = false
8
+ @line_trace_enabled = false
9
+ @default_dispatcher = Resugan::Engine::InlineDispatcher
10
+ end
11
+ end
12
+
2
13
  class Kernel
3
- # show warning when a _fire was called and there was no context to consume it
4
- def self.warn_no_context_events(enable)
5
- @warn_no_context_events = enable
14
+ def self.config
15
+ @config ||= Resugan::Config.new
16
+ if block_given?
17
+ yield @config
18
+ end
19
+
20
+ @config
6
21
  end
7
22
 
8
- def self.warn_no_context_events?
9
- @warn_no_context_events || false
23
+ def self.reuse_top_level_context?
24
+ config.reuse_top_level_context
10
25
  end
11
26
 
12
- # flag to log the line source where a fire was executed
13
- def self.enable_line_trace(enable)
14
- @enable = enable
27
+ def self.warn_no_context_events?
28
+ config.warn_no_context_events
15
29
  end
16
30
 
17
31
  def self.line_trace_enabled?
18
- @enable || false
32
+ config.line_trace_enabled
19
33
  end
20
34
 
21
35
  def self.set_default_dispatcher(dispatcher)
22
- @default_dispatcher ||= dispatcher.new
36
+ config.default_dispatcher = dispatcher
23
37
  end
24
38
 
25
39
  def self.default_dispatcher
26
- @default_dispatcher || Resugan::Engine::InlineDispatcher.new
40
+ config.default_dispatcher
27
41
  end
28
42
 
29
43
  def self.dispatcher_for(namespace = '')
30
44
  @dispatchers = {} unless @dispatchers
31
- @dispatchers[namespace] || default_dispatcher
45
+ @dispatchers[namespace.to_s] || default_dispatcher.new
32
46
  end
33
47
 
34
48
  def self.register_dispatcher(dispatcher, namespace = '')
35
49
  @dispatchers = {} unless @dispatchers
36
- @dispatchers[namespace] = dispatcher.is_a?(Class) ? dispatcher.new : dispatcher
50
+ @dispatchers[namespace.to_s] = (dispatcher.is_a?(Class) ? dispatcher.new : dispatcher)
37
51
  end
38
52
 
39
53
  def self.register(event, &block)
@@ -1,12 +1,24 @@
1
1
  class Object
2
2
  def resugan(namespace = '', &block)
3
+ namespace ||= ''
3
4
  current_thread = Thread.current
4
5
  current_thread.push_resugan_context(namespace)
5
6
  begin
6
7
  block.call
7
8
  ensure
8
9
  context = current_thread.pop_resugan_context
9
- context.invoke
10
+ end
11
+ context
12
+ end
13
+
14
+ def resugan!(namespace = '', &block)
15
+ namespace ||= ''
16
+ current_thread = Thread.current
17
+ current_thread.push_resugan_context(namespace, true)
18
+ begin
19
+ block.call
20
+ ensure
21
+ context = current_thread.pop_resugan_context(true)
10
22
  end
11
23
  context
12
24
  end
@@ -1,18 +1,40 @@
1
1
  class Thread
2
- def push_resugan_context(namespace = '')
3
- if !@resugan_context
4
- @resugan_context_stack = []
2
+ def push_resugan_context(namespace = '', force_invoke = false)
3
+ @resugan_context_stack ||= []
4
+
5
+ namespace = namespace.to_s
6
+
7
+ if @resugan_context.nil? || !Resugan::Kernel.reuse_top_level_context? || force_invoke
8
+ @resugan_context = Resugan::Context.new(namespace)
9
+ elsif @resugan_context.namespace != namespace
10
+ @resugan_context = (@resugan_context_stack.reverse.find { |e| e.namespace == namespace }) || Resugan::Context.new(namespace)
5
11
  end
6
12
 
7
- @resugan_context = Resugan::Context.new(namespace)
8
13
  @resugan_context_stack << @resugan_context
9
14
  end
10
15
 
11
- def pop_resugan_context
12
- @resugan_context = @resugan_context_stack.pop
16
+ def pop_resugan_context(force_invoke = false)
17
+ _resugan_context = @resugan_context_stack.pop
18
+ @resugan_context = @resugan_context_stack.last
19
+
20
+ # depending on option, only invoke if top level
21
+ if !force_invoke && Resugan::Kernel.reuse_top_level_context?
22
+ _resugan_context.invoke if @resugan_context_stack.find { |e| e.namespace == _resugan_context.namespace }.nil?
23
+ elsif
24
+ _resugan_context.invoke
25
+ end
26
+
27
+ _resugan_context
13
28
  end
14
29
 
15
30
  def resugan_context
16
31
  @resugan_context
17
32
  end
33
+
34
+ private
35
+
36
+ def clear_context
37
+ @resugan_context_stack = []
38
+ @resugan_context
39
+ end
18
40
  end
@@ -1,3 +1,3 @@
1
1
  module Resugan
2
- VERSION = "0.1.13"
2
+ VERSION = "0.1.14"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: resugan
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.13
4
+ version: 0.1.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joseph Emmanuel Dayo
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-06-30 00:00:00.000000000 Z
11
+ date: 2016-07-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler