resugan 0.1.4 → 0.1.5

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: 16c94f13ca5e42a153322de77d1823e1989bea02
4
- data.tar.gz: 9a9d847bc40016e0e112ed811ae7b8ed1eef4f41
3
+ metadata.gz: 4c7270a33c2bdc1644287e242de2ff11d60cedcd
4
+ data.tar.gz: dd89cd1a9e3548a6214cc75ac5dc6a8d518f63a9
5
5
  SHA512:
6
- metadata.gz: 7a5aad719ad1290e86ff7943dfdc9fd85de5af47a236f2f9cf0d3854ea8dc131df6f02f5083e0a909e4cf57128b06ead7bb2bba7ce9162206ba2896e36c89c22
7
- data.tar.gz: a7e03d9ce263b7ef90a8cfe7682d8efeadd3b1322f15d9b6f2fd5ab83ec265ec89db6807c8f566fa6a342f1c67aa918190f649d2e63703ea1cf151734bc6ee59
6
+ metadata.gz: 43c806a87a2b83c278d4d23aa93a3a375009b16eb1514affab58953f77189245576edb7076879c7049fcf2bf8e497180c719c6b84e2a7ee0e192316be62e889e
7
+ data.tar.gz: 41211f23f39cd001bff7db9f3a098d22404de7b3d1ccd537bf0945a5fcb8ec48681493638fca840388931b5e480aa36b96af22c80ad0fa33fed211cf613e91a0
data/README.md CHANGED
@@ -24,11 +24,11 @@ Or install it yourself as:
24
24
  Register listeners:
25
25
 
26
26
  ```ruby
27
- listener :event1 do |params|
27
+ _listener :event1 do |params|
28
28
  puts "hello! event 2 has been called!"
29
29
  end
30
30
 
31
- listener :hay do |params|
31
+ _listener :hay do |params|
32
32
  puts "hello! someone said hay!"
33
33
  end
34
34
  ```
@@ -39,9 +39,9 @@ Generate events and wrap them in a resugan block:
39
39
  resugan {
40
40
  puts "I am now going to generate an event"
41
41
 
42
- fire :event2
42
+ _fire :event2
43
43
 
44
- fire :hay
44
+ _fire :hay
45
45
  }
46
46
  ```
47
47
 
@@ -49,7 +49,7 @@ Note that events don't have to be fired at the top level of the block, even obje
49
49
 
50
50
  The two events should fire and should print:
51
51
 
52
- ```
52
+ ```ruby
53
53
  hello! event 2 has been called!
54
54
  hello! someone said hay!
55
55
  ```
@@ -65,24 +65,53 @@ Resugan supports namespaces, allowing you to group listeners and trigger them se
65
65
 
66
66
 
67
67
  ```ruby
68
- listener :event1, namespace: "group1" do |params|
68
+ _listener :event1, namespace: "group1" do |params|
69
69
  puts "hello! event 2 has been called!"
70
70
  end
71
71
 
72
- listener :event1, namespace: "group2" do |params|
72
+ _listener :event1, namespace: "group2" do |params|
73
73
  puts "hello! someone said hay!"
74
74
  end
75
75
 
76
76
  resugan "group1" do
77
- fire :event1
77
+ _fire :event1
78
78
  end
79
79
 
80
80
  resugan "group2" do
81
- fire :event1
81
+ _fire :event1
82
+ end
83
+ ```
84
+
85
+ Behavior is as expected. Events under group1 will only be handled by listeners under group1 and so on.
86
+
87
+ ## Customizing the Event dispatcher
88
+
89
+ The way events are consumed is entirely customizable. You can register your own event dispatcher:
90
+
91
+ ```ruby
92
+ class MyCustomerDispatcher
93
+ def dispatch(namespace, events)
94
+ events.each do |k,v|
95
+ Resugan::Kernel.invoke(namespace, k, v)
96
+ end
82
97
  end
98
+ end
83
99
  ```
84
100
 
85
- Behavior is as is expected. events under group1 will only be handled by listeners under group1 and so on.
101
+ You need to implement your own dispatch method, captured events are passed as
102
+ parameters.
103
+
104
+ You can then set it as the default dispatcher:
105
+
106
+ ```ruby
107
+ Resugan::Kernel.set_default_dispatcher(MyCustomerDispatcher)
108
+ ```
109
+
110
+ Or assign it to a specific namespace:
111
+
112
+ ```ruby
113
+ Resugan::Kernel.register_dispatcher(MyCustomerDispatcher, 'CustomGroup')
114
+ ```
86
115
 
87
116
  ## Development
88
117
 
@@ -24,16 +24,16 @@ module Resugan
24
24
 
25
25
  def self.register_with_namespace(namespace, event, listener_id = nil, block)
26
26
  @listener_ids = {} unless @listener_ids
27
- @listener = {} unless @listener
27
+ @_listener = {} unless @_listener
28
28
 
29
29
  return self if listener_id && @listener_ids["#{namespace}_#{listener_id}"]
30
30
 
31
31
  event = "#{namespace}_#{event}".to_sym
32
32
 
33
- unless @listener[event]
34
- @listener[event] = [block]
33
+ unless @_listener[event]
34
+ @_listener[event] = [block]
35
35
  else
36
- @listener[event] << block
36
+ @_listener[event] << block
37
37
  end
38
38
 
39
39
  @listener_ids["#{namespace}_#{listener_id}"] = block if listener_id
@@ -43,20 +43,20 @@ module Resugan
43
43
 
44
44
  def self.invoke(namespace, event, payload = [])
45
45
  event = "#{namespace}_#{event}".to_sym
46
- if @listener[event]
47
- @listener[event].each do |listener|
48
- listener.call(payload.map { |p| p[:params] })
46
+ if @_listener[event]
47
+ @_listener[event].each do |_listener|
48
+ _listener.call(payload.map { |p| p[:params] })
49
49
  end
50
50
  end
51
51
  end
52
52
 
53
53
  def self.listeners
54
- @listener
54
+ @_listener
55
55
  end
56
56
 
57
57
  def self.clear
58
58
  @listener_ids.clear if @listener_ids
59
- @listener.clear if @listener
59
+ @_listener.clear if @_listener
60
60
  end
61
61
  end
62
62
  end
@@ -9,14 +9,14 @@ class Object
9
9
  context.invoke
10
10
  end
11
11
 
12
- def fire(event, params = {})
12
+ def _fire(event, params = {})
13
13
  current_thread = Thread.current
14
14
  if current_thread.resugan_context
15
15
  current_thread.resugan_context.register(event, params)
16
16
  end
17
17
  end
18
18
 
19
- def listener(event, options = {}, &block)
19
+ def _listener(event, options = {}, &block)
20
20
  Resugan::Kernel.register_with_namespace(options[:namespace], event, options[:id], ->(params) {
21
21
  block.call(params)
22
22
  })
@@ -1,3 +1,3 @@
1
1
  module Resugan
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
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.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joseph Emmanuel Dayo