resugan 0.1.11 → 0.1.12

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: 8436d6c33951f98da4d83996218eabaca1425f07
4
- data.tar.gz: 3bdc108d04b3ae9a7140212ead07f9e0d3aceef4
3
+ metadata.gz: 9654ca7f5f98456f020eb91f5bc89498957b6fe9
4
+ data.tar.gz: 5ee0ae2b994d3bec9077d0025f3683c1aba9c3f0
5
5
  SHA512:
6
- metadata.gz: bb6cea9e23d0d5012c8bca0deaced58cbe04c55fa3845ba6beb6872326d9590a335adc2491300dd1e5a89b060e9d29eec43ee426158a10c457fc57c8568cafc4
7
- data.tar.gz: 171ec8152a6b3a49598eed4c351836f720adea8c9ba323a6eeefd2a53f3edd4217491fc6bc3fa167f332a30a1ba0cdd8f81f959e63334c277c7f6ef73a793667
6
+ metadata.gz: 52f12f972eb824baa776710c7fd0b98f805ab790cc491be0403db7881a9ca90affd80623f7266ae27da09a0b20d95421c2614615295813beaf698770374b17f3
7
+ data.tar.gz: 7e57c11239e0aecfb94162094f2260c5f998cf997e21f2f600e8eb8af0310cfca7fdb2807370a94aefd5f5e444d202e34c507dd9ea51f1562d0b9f20bc8eaf57
data/README.md CHANGED
@@ -6,7 +6,7 @@ Simple, powerful and unobstrusive event driven architecture framework for ruby.
6
6
  a base framework in order to build a more powerful event based system on top of it. Events cuts across multiple objects and allows you to cleanly separate business logic to other cross cutting concerns like analytics and logging. Multiple events are consolidated allowing you to efficiently batch related operations together.
7
7
 
8
8
  Also allows for a customizable backend which enables the use of various evented queuing mechanisms
9
- like redis queue, amazon sqs with minimal changes to your code that generates the events.
9
+ like redis queue, amazon SQS with minimal changes to your code that generates the events.
10
10
 
11
11
  ## Installation
12
12
 
@@ -26,19 +26,23 @@ Or install it yourself as:
26
26
 
27
27
  ## Basic Usage
28
28
 
29
- Register listeners:
29
+ Register listeners, using :
30
30
 
31
31
  ```ruby
32
- _listener :event1 do |params|
32
+ _listener :event1 do |array_of_params|
33
33
  puts "hello! event 2 has been called!"
34
34
  end
35
35
 
36
- _listener :hay do |params|
36
+ _listener :hay do |array_of_params|
37
37
  puts "hello! someone said hay!"
38
38
  end
39
39
  ```
40
40
 
41
- Generate events and wrap them in a resugan block:
41
+ Listeners are basically code that listens to an event, in this case :event1 and :hay.
42
+ an array of params equal to the number of times that specific event was captured
43
+ will be passed. So if :event1 was called twice, array_of_params will contain 2 elements.
44
+
45
+ Generate events using _fire and wrap them in a resugan block:
42
46
 
43
47
  ```ruby
44
48
  resugan {
@@ -52,7 +56,13 @@ resugan {
52
56
  }
53
57
  ```
54
58
 
55
- Note that events don't have to be fired at the top level of the block, even objects used inside the block can invoke fire to generate an event.
59
+ The _fire method is available inside all objects, however the events won't be
60
+ collected unless within the context of a resugan block. The idea is that you
61
+ can prepackage a library that fires those events but won't actually
62
+ get used until someone specifically listens for it.
63
+
64
+ Note that events don't have to be fired at the top level of the block, even objects
65
+ used inside the block can invoke fire to generate an event.
56
66
 
57
67
  The two events should fire and should print:
58
68
 
@@ -101,18 +111,17 @@ Please see spec/resugan_spec.rb for more examples and details.
101
111
 
102
112
  Resugan supports namespaces, allowing you to group listeners and trigger them separately
103
113
 
104
-
105
114
  ```ruby
106
- _listener :event1, namespace: "group1" do |params|
115
+ _listener :event1, namespace: "group1" do |array_of_params|
107
116
  puts "hello! event 2 has been called!"
108
117
  end
109
118
 
110
- _listener :event1, namespace: "group2" do |params|
119
+ _listener :event1, namespace: "group2" do |array_of_params|
111
120
  puts "hello! someone said hay!"
112
121
  end
113
122
 
114
- _listener :log, namespace: %w(group1 group2) do |params|
115
- params.each {
123
+ _listener :log, namespace: %w(group1 group2) do |array_of_params|
124
+ array_of_params.each {
116
125
  puts "listener that belongs to 2 namespaces"
117
126
  }
118
127
  end
@@ -138,6 +147,18 @@ listener that belongs to 2 namespaces
138
147
  hello! someone said hay!
139
148
  listener that belongs to 2 namespaces
140
149
  ```
150
+ ## Unique Listeners
151
+
152
+ the _listener always creates a new listener for an event, so if it so happens that
153
+ the code that creates those listeners gets executed again it will create another one.
154
+ if you want to make sure that listener only gets executed once you can pass an id
155
+ option:
156
+
157
+ ```ruby
158
+ listener :event1, id: 'no_other_listener_like_this' do |array|
159
+ # some code that gets executed
160
+ end
161
+ ```
141
162
 
142
163
  ## Customizing the Event dispatcher
143
164
 
@@ -182,15 +203,27 @@ Line source should now be passed as params everytime you fire an event. You can
182
203
  view it by dumping a resugan context.
183
204
 
184
205
  ```ruby
185
- puts resugan {
206
+ puts(resugan {
186
207
  _fire :event1
187
- }.dump
208
+ }.dump)
188
209
  ```
189
210
 
190
211
  ```ruby
191
212
  {:event1=>[{:params=>{:_source=>"/Users/jedld/workspace/resugan/spec/resugan_spec.rb:144:in `block (5 levels) in <top (required)>'"}}]}
192
213
  ```
193
214
 
215
+ ## Using Resugan::Engine::MarshalledInlineDispatcher
216
+
217
+ By default, resugan uses the Resugan::Engine::InlineDispatcher as the default dispatcher for
218
+ all namespaces. For performance reasons, params passed to the _fire method are passed as is, but there are
219
+ times when you want to simulate params that are passed using JSON.parse as is the case
220
+ when using a custom dispatcher that uses redis (see resugan-worker). In this case you may set MarshalledInlineDispatcher
221
+ as the default dispatcher for test and development environment instead (e.g. rails):
222
+
223
+ ```ruby
224
+ Resugan::Kernel.set_default_dispatcher(Resugan::Engine::MarshalledInlineDispatcher) if Rails.env.development? || Rails.env.test?
225
+ ```
226
+
194
227
  ## Related Projects
195
228
 
196
229
  Below are projects that extend resugan.
@@ -203,6 +236,10 @@ Can also be used as a sample on how to extend resugan.
203
236
 
204
237
  https://github.com/jedld/resugan-worker
205
238
 
239
+ ## Similar Projects
240
+
241
+ wisper (https://github.com/krisleech/wisper) - An excellent gem that focuses on a pub-sub model. Though its global listeners somehow have the same effect though in a syntactically different way than resugan.
242
+
206
243
  ## Development
207
244
 
208
245
  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/resugan.rb CHANGED
@@ -5,6 +5,8 @@ require "resugan/thread"
5
5
  require "resugan/context"
6
6
  require "resugan/object_helpers"
7
7
  require "resugan/engine/inline_dispatcher"
8
+ require "resugan/engine/marshalled_inline_dispatcher"
9
+ require "json"
8
10
 
9
11
  module Resugan
10
12
  end
@@ -0,0 +1,17 @@
1
+ module Resugan
2
+ module Engine
3
+ class MarshalledInlineDispatcher
4
+ def dispatch(namespace, events)
5
+ marshalled_events = []
6
+ events.each do |k, v|
7
+ marshalled_events << { event: k, args: v }.to_json
8
+ end
9
+
10
+ marshalled_events.each do |event|
11
+ unmarshalled_event = JSON.parse(event)
12
+ Resugan::Kernel.invoke(namespace, unmarshalled_event['event'], unmarshalled_event['args'])
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -33,7 +33,7 @@ module Resugan
33
33
 
34
34
  def self.register_dispatcher(dispatcher, namespace = '')
35
35
  @dispatchers = {} unless @dispatchers
36
- @dispatchers[namespace] = dispatcher
36
+ @dispatchers[namespace] = dispatcher.is_a?(Class) ? dispatcher.new : dispatcher
37
37
  end
38
38
 
39
39
  def self.register(event, &block)
@@ -64,8 +64,6 @@ module Resugan
64
64
 
65
65
  def self.invoke(namespace, event, payload = [])
66
66
  event = "#{namespace}_#{event}".to_sym
67
- puts "invoke"
68
- p @_listener
69
67
  if @_listener && @_listener[event]
70
68
  @_listener[event].each do |_listener|
71
69
  _listener.call(payload.map { |p| p[:params] || p['params'] })
@@ -1,3 +1,3 @@
1
1
  module Resugan
2
- VERSION = "0.1.11"
2
+ VERSION = "0.1.12"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: resugan
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.11
4
+ version: 0.1.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joseph Emmanuel Dayo
@@ -87,6 +87,7 @@ files:
87
87
  - lib/resugan.rb
88
88
  - lib/resugan/context.rb
89
89
  - lib/resugan/engine/inline_dispatcher.rb
90
+ - lib/resugan/engine/marshalled_inline_dispatcher.rb
90
91
  - lib/resugan/kernel.rb
91
92
  - lib/resugan/object.rb
92
93
  - lib/resugan/object_helpers.rb