emittr 0.1.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: b2ad0daaae0870209d4e8de5c55bd2f50aec7a3e
4
+ data.tar.gz: 851f7d38270aec17ee2ce9bb27c4c0818c8266aa
5
+ SHA512:
6
+ metadata.gz: 4258429a3e7719cbc92585cdcbbf11ba2cf138a1254a779a50b7d27ef400349958a54f0ca7f45e81b18a30f03ae44b4ed387eaf6b6bd6afa7680e269bf282ba6
7
+ data.tar.gz: d9dfe7936794f27e139d22c85817ef7bf4388b33539e38de57b334dc3ea71627b6e7a0efe9ef8b13b1057356745658dfbf845a6d3aa87b9e60da1fada7702a54
data/.editorconfig ADDED
@@ -0,0 +1,9 @@
1
+ root = true
2
+
3
+ [*]
4
+ indent_style = space
5
+ indent_size = 2
6
+ end_of_line = lf
7
+ charset = utf-8
8
+ trim_trailing_whitespace = true
9
+ insert_final_newline = true
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.12.5
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'pry', group: :development
6
+
7
+ gem 'simplecov', require: false, group: :test
8
+ gem 'coveralls', require: false
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Talysson
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,120 @@
1
+ # Emittr
2
+
3
+ Emittr is a event emitter for Ruby.
4
+
5
+ [![Build Status](https://travis-ci.org/talyssonoc/emittr.svg?branch=master)](https://travis-ci.org/talyssonoc/emittr) [![Coverage Status](https://coveralls.io/repos/github/talyssonoc/emittr/badge.svg?branch=master)](https://coveralls.io/github/talyssonoc/emittr?branch=master)
6
+
7
+ Installation
8
+ ------------
9
+
10
+ Add this line to your Gemfile:
11
+
12
+ ```ruby
13
+ gem 'emittr'
14
+ ```
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install emittr
19
+
20
+ Usage
21
+ -----
22
+
23
+ Emittr can be used in two ways. You can just create an `Emittr::Emitter` instance and use it to emit and listen to events, like this:
24
+
25
+ ```ruby
26
+ emitter = Emittr::Emitter.new
27
+
28
+ emitter.on :user_connected do |user_name|
29
+ puts "Hello, #{user_name}"
30
+ end
31
+
32
+ emitter.emit :user_connected, 'Mr. Anderson'
33
+ ```
34
+
35
+ Or you can include the `Emittr::Events` module into an existent class to make it an event emitter:
36
+
37
+ ```ruby
38
+ class Server
39
+ include Emittr::Events
40
+
41
+ def initialize
42
+ on(:user_connected) do |user_data|
43
+ handle_new_connection(user_data)
44
+ end
45
+ end
46
+
47
+ def handle_new_connection(user_data)
48
+ # do something clever here
49
+ end
50
+ end
51
+
52
+ server = Server.new
53
+
54
+ server.emit :user_connected, { name: 'Somebody', ip: '127.0.0.1' }
55
+ ```
56
+
57
+ ## Add event listeners
58
+
59
+ ### #on(event, callback)
60
+
61
+ Call `callback` when `event` is emitted.
62
+
63
+ ### #once(event, callback)
64
+
65
+ Call `callback` when `event` is emitted for the first time, then the `callback` is removed from the listeners list of the given `event`.
66
+
67
+ ### #on_any(callback)
68
+
69
+ Call `callback` whenever an event is emitted.
70
+
71
+ ### #once_any(callback)
72
+
73
+ Call `callback` the first time any event is emitted then removes it from list.
74
+
75
+ ## Remove event listeners
76
+
77
+ ### #off(event, callback)
78
+
79
+ Remove `callback` from `event` callbacks list.
80
+
81
+ ### #off(event)
82
+
83
+ Removes all callbacks for `event`.
84
+
85
+ ### #off
86
+
87
+ Removes all callbacks for all events.
88
+
89
+ ### #off_any(callback)
90
+
91
+ Remove `callback` from list to be run after any event is emitted.
92
+
93
+ ## Emitting events
94
+
95
+ ### #emit(type, *args)
96
+
97
+ Emit an `event` with all passed `args` as params.
98
+
99
+ ## Retrieving added events
100
+
101
+ ### #listeners_for(event)
102
+
103
+ Return all callbacks for `event`. Callbacks added with `#on_any` or `#once_any` will not be included.
104
+
105
+ ### #listeners_for_any
106
+
107
+ Return all callbacks added with `#on_any` or `#once_any`
108
+
109
+ ## Contributing
110
+
111
+ 1. Fork it
112
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
113
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
114
+ 4. Push to the branch (`git push origin my-new-feature`)
115
+ 5. Create new Pull Request
116
+
117
+
118
+ ## License
119
+
120
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "emittr"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/emittr.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'emittr/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "emittr"
8
+ spec.version = Emittr::VERSION
9
+ spec.authors = ["Reynaldo", "Talysson"]
10
+ spec.email = ["r.scardinho@gmail.com", "talyssonoc@gmail.com"]
11
+
12
+ spec.summary = %q{A Ruby event emitter implementation.}
13
+ spec.homepage = "https://github.com/talyssonoc/emittr"
14
+ spec.license = "MIT"
15
+
16
+ if spec.respond_to?(:metadata)
17
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
18
+ else
19
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
20
+ end
21
+
22
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ spec.bindir = "exe"
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ["lib"]
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.12"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "rspec", "~> 3.0"
30
+ end
data/lib/emittr.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "emittr/version"
2
+ require "emittr/events"
3
+ require "emittr/emitter"
4
+ require "emittr/callback"
5
+
6
+ module Emittr
7
+ end
@@ -0,0 +1,17 @@
1
+ module Emittr
2
+ class Callback
3
+ attr_accessor :wrapper, :callback
4
+
5
+ def initialize(&callback)
6
+ @callback = callback
7
+ end
8
+
9
+ def call(*args)
10
+ callback.call(*args)
11
+ end
12
+
13
+ def ==(block)
14
+ callback == block || wrapper == block
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,5 @@
1
+ module Emittr
2
+ class Emitter
3
+ include Emittr::Events
4
+ end
5
+ end
@@ -0,0 +1,98 @@
1
+ module Emittr
2
+ module Events
3
+ def self.included(klass)
4
+ klass.__send__ :include, InstanceMethods
5
+ end
6
+
7
+ module InstanceMethods
8
+ def on(event, &block)
9
+ raise_no_block_error unless block_given?
10
+
11
+ listeners[event.to_sym] << ::Emittr::Callback.new(&block)
12
+ self
13
+ end
14
+
15
+ def off(event=nil, &block)
16
+ unless event
17
+ listeners.clear
18
+ return self
19
+ end
20
+
21
+ if block_given?
22
+ listeners[event].reject! { |l| l == block }
23
+ else
24
+ listeners.delete event
25
+ end
26
+
27
+ self
28
+ end
29
+
30
+ def once(event, &block)
31
+ raise_no_block_error unless block_given?
32
+
33
+ callback = ::Emittr::Callback.new &block
34
+
35
+ off_block = Proc.new do |args|
36
+ callback.call(*args)
37
+ block_to_send = callback.wrapper || callback.callback
38
+
39
+ off(event, &block_to_send)
40
+ end
41
+
42
+ callback.wrapper = off_block
43
+
44
+ on(event, &off_block)
45
+ end
46
+
47
+ def on_any(&block)
48
+ raise_no_block_error unless block_given?
49
+ on(:*, &block)
50
+ end
51
+
52
+ def off_any(&block)
53
+ raise_no_block_error unless block_given?
54
+ off(:*, &block)
55
+ end
56
+
57
+ def once_any(&block)
58
+ raise_no_block_error unless block_given?
59
+ once(:*, &block)
60
+ end
61
+
62
+ def emit(event, *payload)
63
+ emit_any(event, *payload)
64
+
65
+ return unless listeners.key? event
66
+
67
+ listeners[event].each do |l|
68
+ l.call(*payload)
69
+ end
70
+
71
+ self
72
+ end
73
+
74
+ def listeners_for(event)
75
+ listeners[event.to_sym].dup
76
+ end
77
+
78
+ def listeners_for_any
79
+ listeners[:*].dup
80
+ end
81
+
82
+ private
83
+
84
+ def listeners
85
+ @listeners ||= Hash.new { |h,k| h[k] = [] }
86
+ end
87
+
88
+ def emit_any(event, *payload)
89
+ any_listeners = listeners[:*]
90
+ any_listeners.each { |l| l.call(event, *payload) } if any_listeners.any?
91
+ end
92
+
93
+ def raise_no_block_error
94
+ raise ArgumentError, 'required block not passed'
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,3 @@
1
+ module Emittr
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: emittr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Reynaldo
8
+ - Talysson
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2016-08-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.12'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.12'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '10.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '10.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '3.0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '3.0'
56
+ description:
57
+ email:
58
+ - r.scardinho@gmail.com
59
+ - talyssonoc@gmail.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".editorconfig"
65
+ - ".gitignore"
66
+ - ".rspec"
67
+ - ".travis.yml"
68
+ - Gemfile
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - bin/console
73
+ - bin/setup
74
+ - emittr.gemspec
75
+ - lib/emittr.rb
76
+ - lib/emittr/callback.rb
77
+ - lib/emittr/emitter.rb
78
+ - lib/emittr/events.rb
79
+ - lib/emittr/version.rb
80
+ homepage: https://github.com/talyssonoc/emittr
81
+ licenses:
82
+ - MIT
83
+ metadata:
84
+ allowed_push_host: https://rubygems.org
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.5.1
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: A Ruby event emitter implementation.
105
+ test_files: []
106
+ has_rdoc: