microevent 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d5c0e175d5a47fc6de82d73f5d6133b8fbfdbcb8
4
+ data.tar.gz: 126873406a1970cf956d6de3755dd078c0e2a814
5
+ SHA512:
6
+ metadata.gz: c212bf4db92b625537a8b4d5670050afb766237d93267ef0989b3a663442c594057d37db5ecdaef775d478011e471a71eeae455771611661fd9dfa0741f8cafb
7
+ data.tar.gz: 8db662bbc7f8a8f4d69030f5ab167adcece813a009953623ecdd09040290114805acbfe060d6487beacf227e40d0bab98ed21fb972fd95d201546779bd692d86
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ Gemfile.lock
data/.travis.yml ADDED
@@ -0,0 +1,17 @@
1
+ language: ruby
2
+
3
+ script: ruby spec/*
4
+
5
+ rvm:
6
+ - 2.2.1
7
+ - 2.2.0
8
+ - 2.1.5
9
+ - 2.1.4
10
+ - 2.1.3
11
+ - 2.1.2
12
+ - 2.1.1
13
+ - 2.1.0
14
+ - 2.0.0
15
+ - rbx-2
16
+ - jruby-head
17
+
data/CHANGELOG.md ADDED
@@ -0,0 +1,4 @@
1
+ ### 1.0.0
2
+
3
+ * Release
4
+
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,14 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ microevent (1.0.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+
10
+ PLATFORMS
11
+ ruby
12
+
13
+ DEPENDENCIES
14
+ microevent!
data/MIT-LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2015 Jan Lelis
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # MicroEvent.rb [![[travis]](https://travis-ci.org/janlelis/microevent.rb.png)](https://travis-ci.org/janlelis/microevent.rb)
2
+
3
+ MicroEvent.rb is a event emitter library which provides the observer pattern to Ruby objects. It is inspired by [MicroEvent.js](https://github.com/jeromeetienne/microevent.js), implemented in less than [20 lines of Ruby](https://github.com/janlelis/microevent.rb/blob/master/lib/microevent.rb).
4
+
5
+
6
+ ## Setup
7
+
8
+ Add to your `Gemfile`
9
+
10
+ ```ruby
11
+ gem 'microevent'
12
+ ```
13
+
14
+ or copy the [source file](https://github.com/janlelis/microevent.rb/blob/master/lib/microevent.rb) into your project.
15
+
16
+ ## How to Use It
17
+
18
+ Suppose you got a class `Klass`, and you wish it to support the observer partern, do
19
+
20
+ ```ruby
21
+ class Klass
22
+ include MicroEvent
23
+ end
24
+ ```
25
+
26
+ That's it. Now all instances of this class can `#bind`, `#unbind` and `trigger`:
27
+
28
+ ```ruby
29
+ fn = proc{ puts "Go" }
30
+ object = Klass.new
31
+ object.bind :slot, &fn
32
+ object.trigger :slot # => Go
33
+ ```
34
+
35
+ You could also use it on class/singleton level:
36
+
37
+ ```ruby
38
+ class Klass
39
+ extend MicroEvent
40
+ end
41
+
42
+ Klass.bind :slot do
43
+ puts "Go"
44
+ end
45
+
46
+ Klass.trigger :slot # => Go
47
+ ```
48
+
49
+ You will find more examples in the [tests](https://github.com/janlelis/microevent.rb/blob/master/spec/microevent_test.rb).
50
+
51
+ ## MIT License
52
+
53
+ Ruby version by [Jan Lelis](http://janlelis.com). Inspired by [MicroEvent.js](https://github.com/jeromeetienne/microevent.js) by Jerome Etienne.
data/lib/microevent.rb ADDED
@@ -0,0 +1,18 @@
1
+ module MicroEvent
2
+ VERSION = "1.0.0".freeze
3
+
4
+ def bind(event, &fn)
5
+ @_ ||= Hash.new{ |h,k| h[k] = [] }
6
+ fn ? @_[event] << fn : raise(ArgumentError, "no block given")
7
+ end
8
+
9
+ def unbind(event, &fn)
10
+ @_ ||= Hash.new{ |h,k| h[k] = [] }
11
+ fn ? @_[event].delete(fn) : @_.delete(event) || []
12
+ end
13
+
14
+ def trigger(event, *args)
15
+ @_ ||= Hash.new{ |h,k| h[k] = [] }
16
+ !@_[event].each{ |fn| instance_exec(*args, &fn) }.empty?
17
+ end
18
+ end
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require File.expand_path('../lib/microevent', __FILE__)
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "microevent"
7
+ gem.version = MicroEvent::VERSION
8
+ gem.summary = 'MicroEvent.rb is a event emitter library which provides the observer pattern to Ruby objects.'
9
+ gem.description = 'MicroEvent.rb is a event emitter library which provides the observer pattern to Ruby objects. It is inspired by[MicroEvent.js and implemented in less than 20 lines of Ruby.'
10
+ gem.license = "MIT"
11
+ gem.authors = ["Jan Lelis"]
12
+ gem.email = "mail@janlelis.de"
13
+ gem.homepage = "https://github.com/janlelis/microevent.rb"
14
+
15
+ gem.files = Dir['{**/}{.*,*}'].select { |path| File.file?(path) }
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ['lib']
19
+ end
@@ -0,0 +1,218 @@
1
+ require_relative '../lib/microevent'
2
+ require 'minitest/autorun'
3
+
4
+ describe MicroEvent do
5
+ let :object do
6
+ object = Object.new
7
+ object.extend(MicroEvent)
8
+ object
9
+ end
10
+
11
+ it "works" do
12
+ result = []
13
+
14
+ fn = proc{ result << 42 }
15
+ object.bind :slot, &fn
16
+ object.trigger :slot
17
+
18
+ assert_equal [42], result
19
+ end
20
+
21
+ it "works with blocks" do
22
+ result = []
23
+
24
+ object.bind(:slot){ result << 42 }
25
+ object.trigger :slot
26
+
27
+ assert_equal [42], result
28
+ end
29
+
30
+ it "supports multiple callbacks" do
31
+ result = []
32
+
33
+ fn = proc{ result << 42 }
34
+ fn2 = proc{ result << 23 }
35
+ object.bind :slot, &fn
36
+ object.bind :slot, &fn2
37
+ object.trigger :slot
38
+
39
+ assert_equal [42, 23], result
40
+ end
41
+
42
+ it "supports multiple of the same callback" do
43
+ result = []
44
+
45
+ fn = proc{ result << 21 }
46
+ object.bind :slot, &fn
47
+ object.bind :slot, &fn
48
+ object.trigger :slot
49
+
50
+ assert_equal [21, 21], result
51
+ end
52
+
53
+ it "only calls the triggered slot" do
54
+ result = []
55
+
56
+ fn = proc{ result << 42 }
57
+ object.bind :slot, &fn
58
+ object.trigger :another_slot
59
+
60
+ assert_equal [], result
61
+ end
62
+
63
+ it "works with arguments" do
64
+ result = []
65
+
66
+ fn = proc{ |add| result << add }
67
+ object.bind :slot, &fn
68
+ object.trigger :slot, 42
69
+
70
+ assert_equal [42], result
71
+ end
72
+
73
+ it "has access to object's instance variables" do
74
+ result = []
75
+ object.instance_variable_set :@var, 42
76
+
77
+ fn = proc{ result << @var }
78
+ object.bind :slot, &fn
79
+ object.trigger :slot
80
+
81
+ assert_equal [42], result
82
+ end
83
+
84
+ it "has access to object's methods" do
85
+ result = []
86
+ def object.m() 42 end
87
+
88
+ fn = proc{ result << m }
89
+ object.bind :slot, &fn
90
+ object.trigger :slot
91
+
92
+ assert_equal [42], result
93
+ end
94
+
95
+ describe '#bind details' do
96
+ it "returns array of callbacks" do
97
+ fn = proc{}
98
+ object.bind :slot, &fn
99
+ result = object.bind :slot, &fn
100
+
101
+ assert_equal [fn, fn], result
102
+ end
103
+
104
+ it "willl raise an ArgumentError if no block given" do
105
+ result = nil
106
+
107
+ begin
108
+ object.bind :slot
109
+ rescue ArgumentError => result
110
+ end
111
+
112
+ assert_equal ArgumentError, result.class
113
+ end
114
+ end
115
+
116
+ describe '#unbind details' do
117
+ it "removes an callback" do
118
+ result = []
119
+
120
+ fn = proc{ result << 42 }
121
+ object.bind :slot, &fn
122
+ object.unbind :slot, &fn
123
+ object.trigger :slot
124
+
125
+ assert_equal [], result
126
+ end
127
+
128
+ it "willl removes all instances of this callback" do
129
+ result = []
130
+
131
+ fn = proc{ result << 42 }
132
+ fn2 = proc{ result << 23 }
133
+ object.bind :slot, &fn
134
+ object.bind :slot, &fn2
135
+ object.bind :slot, &fn
136
+ object.unbind :slot, &fn
137
+ object.trigger :slot
138
+
139
+ assert_equal [23], result
140
+ end
141
+
142
+ it "will do nothing if there is nothing to remove" do
143
+ result = []
144
+
145
+ object.unbind :slot
146
+
147
+ assert_equal [], result
148
+ end
149
+
150
+ it "returns deleted callbacks" do
151
+ fn = proc{}
152
+ object.bind :slot, &fn
153
+ result = object.unbind :slot, &fn
154
+
155
+ assert_equal fn, result
156
+ end
157
+
158
+ it "will return nil if nothing is removed" do
159
+ fn = proc{}
160
+ result = object.unbind :slot, &fn
161
+
162
+ assert_equal nil, result
163
+ end
164
+
165
+ it 'will delete all callbacks if no block is given' do
166
+ result = []
167
+
168
+ fn = proc{ result << 42 }
169
+ fn2 = proc{ result << 23 }
170
+ object.bind :slot, &fn
171
+ object.bind :slot, &fn2
172
+ object.unbind :slot
173
+ object.trigger :slot
174
+
175
+ assert_equal [], result
176
+ end
177
+
178
+ it 'will return array of deleted callbacks if no block is given' do
179
+ fn = proc{ result << 42 }
180
+ fn2 = proc{ result << 23 }
181
+ object.bind :slot, &fn
182
+ object.bind :slot, &fn2
183
+ result = object.unbind :slot
184
+
185
+ assert_equal [fn, fn2], result
186
+ end
187
+
188
+ it 'will return emtpy array if no block is given and no callback deleted' do
189
+ result = object.unbind :slot
190
+
191
+ assert_equal [], result
192
+ end
193
+ end
194
+
195
+ describe '#trigger details' do
196
+ it "will do nothing if no callback is registered" do
197
+ result = []
198
+
199
+ object.trigger :slot
200
+
201
+ assert_equal [], result
202
+ end
203
+
204
+ it "will return true if there is a callback listening" do
205
+ fn = proc{}
206
+ object.bind :slot, &fn
207
+ result = object.trigger :slot
208
+
209
+ assert_equal true, result
210
+ end
211
+
212
+ it "will return false if there is no callback listening" do
213
+ result = object.trigger :slot
214
+
215
+ assert_equal false, result
216
+ end
217
+ end
218
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: microevent
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jan Lelis
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-12 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: MicroEvent.rb is a event emitter library which provides the observer
14
+ pattern to Ruby objects. It is inspired by[MicroEvent.js and implemented in less
15
+ than 20 lines of Ruby.
16
+ email: mail@janlelis.de
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".gitignore"
22
+ - ".travis.yml"
23
+ - CHANGELOG.md
24
+ - Gemfile
25
+ - Gemfile.lock
26
+ - MIT-LICENSE.txt
27
+ - README.md
28
+ - lib/microevent.rb
29
+ - microevent.gemspec
30
+ - spec/microevent_spec.rb
31
+ homepage: https://github.com/janlelis/microevent.rb
32
+ licenses:
33
+ - MIT
34
+ metadata: {}
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubyforge_project:
51
+ rubygems_version: 2.4.5
52
+ signing_key:
53
+ specification_version: 4
54
+ summary: MicroEvent.rb is a event emitter library which provides the observer pattern
55
+ to Ruby objects.
56
+ test_files:
57
+ - spec/microevent_spec.rb
58
+ has_rdoc: