rbminivents 0.1.1 → 0.2.0

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
  SHA256:
3
- metadata.gz: 516cebd771ef42bc0233d276a1e9d7cafd2f129b70e6f96c6a1e124537a200a8
4
- data.tar.gz: ab39feb59826b236c658261550d3d026abab22f2cd608130fac42f626a83e9fa
3
+ metadata.gz: '069468a2b42d7e60881c9ee410f04f1934260b404515f0800cb6aa71764ffe01'
4
+ data.tar.gz: 4b156f5ba2ffec43e0935b6d8b8052e43ea5427f885ab8a86657582ae7cccb9d
5
5
  SHA512:
6
- metadata.gz: f88f86014ab9421eea8f98a5af2d385bd3e6927b119de5b22067c73b66359058e932bffb45b5caf20563fab042aef56253c271637bd8c268c2b9f16833b388d2
7
- data.tar.gz: c3ca2963c64c069b89efa6c2b10f0425184276f3c314a04d95d7e6988474f8654dc8e62d441ec9f7aaabf064c2183564db61add707cfc2f8680c7231f4576753
6
+ metadata.gz: 6cc90acb5e271b621b3fe4cf3ba1d167d915d2a287c40004c91d77636b5b0348ea821fe9401d8341724877d2ab83ba5e8b2fb913f55d9e61b435232abcd052b1
7
+ data.tar.gz: 3be1006231fec1b47e6b02912a25b1ca320a1c08391e21ded687911648d3eca469e23a5942ce3c0f1921400ff640772e74ab799e6589235252ab7b239e1c7121
data/README.md CHANGED
@@ -1,8 +1,9 @@
1
1
  # rbminivents
2
- This gem allows you to create and emit your events very easily and minimalistic.
2
+ This gem allows you to easily use minimalistic events in Ruby.
3
3
 
4
4
  ## Description
5
- This gem was made under the influence of the tiny JS library [minivents](https://github.com/allouis/minivents) by [allouis](https://github.com/allouis) that I liked. It seemed to me that it would be nice to transfer this to Ruby.
5
+ This gem was made under the influence of the tiny JS library [minivents](https://github.com/allouis/minivents) by [allouis](https://github.com/allouis) that I liked.
6
+ It seemed to me that it would be quite nice to move something like this to Ruby.
6
7
 
7
8
  ## Getting started
8
9
 
@@ -25,12 +26,11 @@ bundle install
25
26
  ```
26
27
 
27
28
  ## API
28
- This project is similar to the original one in many ways, but some features are not implemented
29
- So,
29
+ This project is similar to the original one in many ways, but some features are not implemented. You can use standard ruby mixins for fully functionality. So,
30
30
 
31
- `on`: Listen to event
31
+ `on`: Listen to event. Returns handle
32
32
 
33
- `off`: Stop listening to event
33
+ `off`: Stop listening to event
34
34
 
35
35
  `emit`: Emit event
36
36
 
@@ -64,9 +64,6 @@ sandbox.on(:event) do |name|
64
64
  end
65
65
 
66
66
  sandbox.emit(:event, 'Max') #=> Hello, Max!
67
- # OR
68
- # when there are > 1 arguments, pass them through the array
69
- sandbox.emit(:event, ['Max']) #=> Hello, Max!
70
67
  ```
71
68
 
72
69
  ```ruby
@@ -83,11 +80,42 @@ end
83
80
 
84
81
  # max, [1, 2, 3], 5 + 7 = 12
85
82
  sandbox.emit(:event,
86
- [
87
- {name: 'max'},
88
- [1, 2, 3],
89
- 7,
90
- false
91
- ]
83
+ {name: 'max'},
84
+ [1, 2, 3],
85
+ 7,
86
+ false
92
87
  )
93
88
  ```
89
+
90
+ ### Using Off
91
+ ```ruby
92
+ require 'rbminivents'
93
+
94
+ sandbox = RbMinivents::Events.new
95
+
96
+ handler1 = sandbox.on(:event) do
97
+ puts "First Hello!"
98
+ end
99
+
100
+ handler2 = sandbox.on(:event) do
101
+ puts "Second Hello!"
102
+ end
103
+
104
+ # remove just this callback
105
+ sandbox.emit(:event) # => First Hello!\nSecond Hello!
106
+
107
+ sandbox.off(:event, handler1)
108
+ sandbox.emit(:event) # => Second Hello!
109
+
110
+ sandbox.off(:event, handler2)
111
+ sandbox.emit(:event) # => nothing...
112
+
113
+ # re-add listener to show can remove all at once
114
+ sandbox.on(:event) do
115
+ puts "Hello!"
116
+ end
117
+
118
+ # remove all
119
+ sandbox.off(:event)
120
+ sandbox.emit(:event) # => nothing again
121
+ ```
data/lib/rbminivents.rb CHANGED
@@ -2,38 +2,26 @@ module RbMinivents
2
2
  class Events
3
3
 
4
4
  def initialize
5
- @events = Hash.new
5
+ @handlers = {}
6
6
  end
7
7
 
8
8
  # On: listen to events
9
- def on(type, &prc)
10
- sym = type.to_s.to_sym
11
-
12
- @events[sym] = prc if block_given?
13
- self
9
+ def on(name, &handler)
10
+ @handlers[name] ||= []
11
+ @handlers[name] << handler
12
+ handler
14
13
  end
15
14
 
16
- # Off: stop listening to event
17
- def off(type)
18
- sym = type.to_s.to_sym
19
-
20
- if @events.has_key?(sym)
21
- @events.delete(sym)
22
- end
23
-
24
- self
15
+ # Off: stop listening to event / specific callback
16
+ def off(name, handler)
17
+ @handlers[name] ||= []
18
+ @handlers[name].delete(handler)
25
19
  end
26
20
 
27
21
  # Emit: send event, callbacks will be triggered
28
- def emit(type, args = nil)
29
- sym = type.to_s.to_sym
30
-
31
- if @events.has_key?(sym)
32
- @events[sym].call(*args)
33
- @events.delete(sym)
34
- end
35
-
36
- self
22
+ def emit(name, *args)
23
+ @handlers[name] ||= []
24
+ @handlers[name].each { |handler| handler.call(*args) }
37
25
  end
38
26
  end
39
27
  end
@@ -1,3 +1,3 @@
1
1
  module RbMinivents
2
- VERSION = '0.1.1'
2
+ VERSION = '0.2.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbminivents
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Max Barsukov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-04-12 00:00:00.000000000 Z
11
+ date: 2021-05-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop