event_spitter 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in event_spitter.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 'Mike Breen'
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,56 @@
1
+ # EventSpitter
2
+
3
+ Like Nodejs' EventEmitter but in Ruby.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'event_spitter'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install event_spitter
18
+
19
+ ## Usage
20
+
21
+ # include EventSpitter in your class.
22
+ class Something
23
+ include EventSpitter
24
+ end
25
+
26
+ something = Something.new
27
+
28
+ # Then add a listener
29
+ listener = ->(msg) { puts('msg') }
30
+ something.on('greeting', listener)
31
+
32
+ # Trigger the event
33
+ something.emit('greeting', 'hello world')
34
+ # => "hello world"
35
+
36
+ # Add a listener that will fire one time only
37
+ one_timer = ->(msg) { puts(msg + ' once') }
38
+ something.once('greeting', one_timer)
39
+
40
+ something.emit('greeting', 'hello world')
41
+ # => "hello world"
42
+ # => "hello world once"
43
+
44
+ something.emit('greeting', 'hello world')
45
+ # => "hello world"
46
+
47
+ # Remove a listener
48
+ # something.off('greeting', listener)
49
+
50
+ ## Contributing
51
+
52
+ 1. Fork it
53
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
54
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
55
+ 4. Push to the branch (`git push origin my-new-feature`)
56
+ 5. Create new Pull Request
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.libs.push "lib"
8
+ t.test_files = FileList['test/*_test.rb']
9
+ t.verbose = true
10
+ end
11
+
12
+ task :default => [:test]
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/event_spitter/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["'Mike Breen'"]
6
+ gem.email = ["hardbap@gmail.com"]
7
+ gem.description = %q{An EventEmitter library for Ruby}
8
+ gem.summary = %q{}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "event_spitter"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = EventSpitter::VERSION
17
+ end
@@ -0,0 +1,105 @@
1
+ require "event_spitter/version"
2
+
3
+ module EventSpitter
4
+
5
+ # Public: Add a listener for an event.
6
+ #
7
+ # event_name - The String name of the event.
8
+ # listener - The Proc listener for the event.
9
+ #
10
+ # Examples
11
+ #
12
+ # listener = ->(msg) { puts(msg) }
13
+ # emitter.on('connection', listener)
14
+ #
15
+ # Returns nothing.
16
+ def on(event_name, listener)
17
+ events[event_name] = Array(events[event_name]) << listener
18
+ end
19
+ alias_method :add_listener, :on
20
+
21
+ # Public: Remove a listener for an event.
22
+ #
23
+ # event_name - The String name of the event.
24
+ # listener - The Proc listener on the event.
25
+ #
26
+ # Examples
27
+ #
28
+ # emitter.off('connection', listener)
29
+ #
30
+ # Returns nothing.
31
+ def off(event_name, listener)
32
+ Array(events[event_name]).delete(listener)
33
+ end
34
+ alias_method :remove_listener, :off
35
+
36
+ # Public: Adds a one time listener for the event.
37
+ #
38
+ # event_name - The String name of the event.
39
+ # listener - The Proc listener for the event.
40
+ #
41
+ # Examples
42
+ #
43
+ # listener = ->(msg) { puts(msg) }
44
+ # emitter.once('connection', listener)
45
+ #
46
+ # Returns nothing.
47
+ def once(event_name, listener)
48
+ new_listener = ->(*args) do
49
+ listener.call(*args)
50
+ off(event_name, new_listener)
51
+ end
52
+
53
+ on(event_name, new_listener)
54
+ end
55
+
56
+ # Public: An Array of listeners for an event.
57
+ #
58
+ # Returns event listeners Array.
59
+ def listeners(event_name)
60
+ events[event_name]
61
+ end
62
+
63
+ # Public: Executes the listeners for an event.
64
+ #
65
+ # event_name - The String event name.
66
+ # args - Zero or more argument to be passed to the listeners.
67
+ #
68
+ # Examples
69
+ #
70
+ # emitter.emit('connection', 'hello world!')
71
+ #
72
+ # Returns nothing.
73
+ def emit(event_name, *args)
74
+ events.fetch(event_name, []).each do |listener|
75
+ listener.call(*args)
76
+ end
77
+ end
78
+
79
+ # Public: Remove all listeners for event names. If zero event names
80
+ # as supplied then all event listeners are removed.
81
+ #
82
+ # event_names - Zero or more String event names.
83
+ #
84
+ # Examples
85
+ #
86
+ # emitter.remove_all_listeners
87
+ #
88
+ # emitter.remove_all_listeners('connection')
89
+ #
90
+ # Returns nothing.
91
+ def remove_all_listeners(*event_names)
92
+ if event_names.any?
93
+ event_names.each { |key| events.delete(key) }
94
+ else
95
+ events.clear
96
+ end
97
+ end
98
+
99
+ # Internal: The list of events.
100
+ #
101
+ # Returns a Hash of events and their listeners.
102
+ def events
103
+ @events ||= {}
104
+ end
105
+ end
@@ -0,0 +1,3 @@
1
+ module EventSpitter
2
+ VERSION = "0.5.0"
3
+ end
@@ -0,0 +1,92 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/pride'
3
+
4
+ require_relative '../lib/event_spitter'
5
+
6
+ class DummyClass; include EventSpitter; end
7
+
8
+ class EventSpitterTest < MiniTest::Unit::TestCase
9
+
10
+ def test_adding_an_event_listener
11
+ obj = DummyClass.new
12
+ listener = ->() {}
13
+ obj.on('something-cool', listener)
14
+
15
+ assert_equal [listener], obj.listeners('something-cool')
16
+ end
17
+
18
+ def test_removing_an_event_listener
19
+ obj = DummyClass.new
20
+ listener = ->() {}
21
+ obj.on('something-cool', listener)
22
+
23
+ obj.off('something-cool', listener)
24
+ assert_empty obj.listeners('something-cool')
25
+ end
26
+
27
+ def test_emitting_an_event
28
+ obj = DummyClass.new
29
+ listener = MiniTest::Mock.new
30
+ listener.expect(:call, 42)
31
+
32
+ obj.on('something-cool', listener)
33
+ obj.emit('something-cool')
34
+ listener.verify
35
+ end
36
+
37
+ def test_emitting_an_event_passing_args
38
+ obj = DummyClass.new
39
+ listener = MiniTest::Mock.new
40
+ listener.expect(:call, 42, ['rock-on'])
41
+
42
+ obj.on('something-cool', listener)
43
+ obj.emit('something-cool', 'rock-on')
44
+ listener.verify
45
+ end
46
+
47
+ def test_remove_all_listeners_with_arguments
48
+ obj = DummyClass.new
49
+ listener = ->() {}
50
+
51
+ obj.on('something-cool', listener)
52
+ obj.on('whats-that', listener)
53
+
54
+ obj.remove_all_listeners('something-cool', 'whats-that')
55
+
56
+ assert_nil obj.listeners('something-cool'), '"something-cool" event listeners not removed'
57
+ assert_nil obj.listeners('whats-that'), '"whats-that" event listeners not removed'
58
+ end
59
+
60
+ def test_remove_all_listeners_without_arguments
61
+ obj = DummyClass.new
62
+ listener = ->() {}
63
+
64
+ obj.on('something-cool', listener)
65
+ obj.on('whats-that', listener)
66
+
67
+ obj.remove_all_listeners
68
+
69
+ assert_nil obj.listeners('something-cool'), '"something-cool" event listeners not removed'
70
+ assert_nil obj.listeners('whats-that'), '"whats-that" event listeners not removed'
71
+ end
72
+
73
+ def test_adding_one_time_listener
74
+ obj = DummyClass.new
75
+ listener = ->() { }
76
+
77
+ obj.once('something-cool', listener)
78
+ obj.emit('something-cool')
79
+
80
+ assert_empty obj.listeners('something-cool')
81
+ end
82
+
83
+ def test_one_time_listener_does_not_stomp_args
84
+ obj = DummyClass.new
85
+ listener = MiniTest::Mock.new
86
+ listener.expect(:call, 42, ['rock-on'])
87
+
88
+ obj.once('something-cool', listener)
89
+ obj.emit('something-cool', 'rock-on')
90
+ listener.verify
91
+ end
92
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: event_spitter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - ! '''Mike Breen'''
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-13 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: An EventEmitter library for Ruby
15
+ email:
16
+ - hardbap@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE
24
+ - README.md
25
+ - Rakefile
26
+ - event_spitter.gemspec
27
+ - lib/event_spitter.rb
28
+ - lib/event_spitter/version.rb
29
+ - test/event_spitter_test.rb
30
+ homepage: ''
31
+ licenses: []
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubyforge_project:
50
+ rubygems_version: 1.8.24
51
+ signing_key:
52
+ specification_version: 3
53
+ summary: ''
54
+ test_files:
55
+ - test/event_spitter_test.rb