emittable 0.0.1 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +55 -2
- data/lib/emittable.rb +43 -13
- data/lib/emittable/error.rb +4 -0
- data/spec/emittable_spec.rb +75 -0
- data/spec/spec_helper.rb +100 -0
- metadata +5 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e5c899f8668b16b6b6e51ada5aeddfb6438cdc35
|
|
4
|
+
data.tar.gz: 100c745cb652db8045df0357516218f52b696858
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 46569e1acbc0073b5b5b2e3a1becbe40b8c9219988f9ed14494afbef26c2c39579e63d0b2a6695d0a8565f7a1e6ba5fc4fb218d42bd54844d1d0a187c9e1da00
|
|
7
|
+
data.tar.gz: 5af9ef46b21c056a9da4935f9d758e397349f2bb9837078131f2ddda9ad81ca3f34721669aaa731c77a1b7401d969243ee9debbbae5ee02be35b88cf8933e569
|
data/README.md
CHANGED
|
@@ -1,3 +1,56 @@
|
|
|
1
1
|
# Emittable
|
|
2
|
-
A
|
|
3
|
-
|
|
2
|
+
A Ruby Gem to register and trigger events. It is a clone of `Vienna::Emittable`.
|
|
3
|
+
|
|
4
|
+
# Install
|
|
5
|
+
`gem install emittable`
|
|
6
|
+
|
|
7
|
+
# Example
|
|
8
|
+
First `require 'emittable'` in your project. Then you can `include` `Emittable` in your classes.
|
|
9
|
+
```ruby
|
|
10
|
+
require 'emittable'
|
|
11
|
+
|
|
12
|
+
class A
|
|
13
|
+
include Emittable
|
|
14
|
+
|
|
15
|
+
...
|
|
16
|
+
...
|
|
17
|
+
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
a = A.new(...)
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
To register a new event callback call `on` on an instance of your class, passing the name of the event and a block as the callback. You can add as many callbacks as you want for an event.
|
|
24
|
+
```ruby
|
|
25
|
+
a.on(:shutdown) do
|
|
26
|
+
...
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
a.on(:shutdown) do
|
|
30
|
+
...
|
|
31
|
+
...
|
|
32
|
+
end
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
To trigger all the event's callbacks call `trigger`, passing the name of the event.
|
|
36
|
+
```ruby
|
|
37
|
+
a.trigger(:shutdown)
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
You can also pass arguments to `trigger` that will get passed to a callback block.
|
|
41
|
+
```ruby
|
|
42
|
+
a.on(:shutdown) do |a, b|
|
|
43
|
+
...
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
a.trigger(:shutdown, 1, 2)
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
To remove a callback you must have already saved a reference to the callback block. You can then call `off`, passing the block.
|
|
50
|
+
```ruby
|
|
51
|
+
callback = proc { ... }
|
|
52
|
+
|
|
53
|
+
a.on(:shutdown, &callback)
|
|
54
|
+
|
|
55
|
+
a.off(:shutdown, callback)
|
|
56
|
+
```
|
data/lib/emittable.rb
CHANGED
|
@@ -1,24 +1,44 @@
|
|
|
1
|
-
|
|
1
|
+
require 'emittable/error'
|
|
2
|
+
|
|
3
|
+
|
|
2
4
|
module Emittable
|
|
3
5
|
|
|
6
|
+
def initialize(*args)
|
|
7
|
+
@emittable_events = {}
|
|
8
|
+
@emittable_mutex = Mutex.new
|
|
9
|
+
super
|
|
10
|
+
end
|
|
11
|
+
|
|
4
12
|
# Register a handler for the given event name.
|
|
5
13
|
#
|
|
6
14
|
# obj.on(:foo) { puts "foo was called" }
|
|
7
15
|
#
|
|
8
16
|
# @param [String, Symbol] name event name
|
|
9
17
|
# @return handler
|
|
10
|
-
def on(
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
18
|
+
def on(event_name, &callback)
|
|
19
|
+
raise ArgumentError, "event name must respond to 'to_s'" unless event_name.respond_to?(:to_s)
|
|
20
|
+
event_name = event_name.to_s
|
|
21
|
+
@emittable_mutex.synchronize do
|
|
22
|
+
@emittable_events[event_name] ||= []
|
|
23
|
+
callbacks = @emittable_events[event_name]
|
|
24
|
+
callbacks << callback
|
|
25
|
+
end
|
|
26
|
+
nil
|
|
14
27
|
end
|
|
15
|
-
|
|
16
|
-
def off(
|
|
17
|
-
|
|
18
|
-
|
|
28
|
+
|
|
29
|
+
def off(event_name, callback)
|
|
30
|
+
raise ArgumentError, "event name must respond to 'to_s'" unless event_name.respond_to?(:to_s)
|
|
31
|
+
event_name = event_name.to_s
|
|
32
|
+
@emittable_mutex.synchronize do
|
|
33
|
+
callbacks = @emittable_events[event_name]
|
|
34
|
+
raise Error, 'no callbacks found for that event' unless callbacks
|
|
35
|
+
raise Error, 'callback not found' unless callbacks.include?(callback)
|
|
36
|
+
callbacks.delete(callback)
|
|
37
|
+
@emittable_events.delete(event_name) if callbacks.empty?
|
|
19
38
|
end
|
|
39
|
+
nil
|
|
20
40
|
end
|
|
21
|
-
|
|
41
|
+
|
|
22
42
|
# Trigger the given event name and passes all args to each handler
|
|
23
43
|
# for this event.
|
|
24
44
|
#
|
|
@@ -26,9 +46,19 @@ module Emittable
|
|
|
26
46
|
# obj.trigger(:foo, 1, 2, 3)
|
|
27
47
|
#
|
|
28
48
|
# @param [String, Symbol] name event name to trigger
|
|
29
|
-
def trigger(
|
|
30
|
-
|
|
31
|
-
|
|
49
|
+
def trigger(event_name, *args)
|
|
50
|
+
raise ArgumentError, "event name must respond to 'to_s'" unless event_name.respond_to?(:to_s)
|
|
51
|
+
event_name = event_name.to_s
|
|
52
|
+
callbacks = nil
|
|
53
|
+
@emittable_mutex.synchronize do
|
|
54
|
+
callbacks = @emittable_events[event_name]
|
|
55
|
+
raise Error, 'no callbacks found for that event' unless callbacks
|
|
56
|
+
callbacks = callbacks.dup
|
|
57
|
+
end
|
|
58
|
+
# we are outside of the mutex so any callbacks are able to trigger an event from
|
|
59
|
+
# this class without deadlock
|
|
60
|
+
callbacks.each { |callback| callback.call(*args) }
|
|
61
|
+
nil
|
|
32
62
|
end
|
|
33
63
|
|
|
34
64
|
end
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
require 'emittable'
|
|
2
|
+
|
|
3
|
+
RSpec.describe Emittable do
|
|
4
|
+
|
|
5
|
+
before do
|
|
6
|
+
RSpec::Mocks.with_temporary_scope do
|
|
7
|
+
stub_const 'A', Class.new
|
|
8
|
+
A.class_eval{ include Emittable }
|
|
9
|
+
@a = A.new
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
describe "#on" do
|
|
14
|
+
|
|
15
|
+
context "when adding a callback" do
|
|
16
|
+
it "should not raise error" do
|
|
17
|
+
expect{ @a.on(:event) {} }.not_to raise_error
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
context "when adding a second callback for same event" do
|
|
22
|
+
it "should not raise error" do
|
|
23
|
+
@a.on(:event) {}
|
|
24
|
+
expect{ @a.on(:event) {} }.not_to raise_error
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
describe "#trigger" do
|
|
31
|
+
|
|
32
|
+
context "when triggering an event" do
|
|
33
|
+
it "should invoke all callbacks" do
|
|
34
|
+
$test = []
|
|
35
|
+
@a.on(:event) { $test << nil }
|
|
36
|
+
@a.on(:event) { $test << nil }
|
|
37
|
+
@a.trigger(:event)
|
|
38
|
+
expect($test.length).to eql 2
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
context "when triggering an event with arguments" do
|
|
43
|
+
it "should invoke all callback with arguments" do
|
|
44
|
+
@a.on(:event) { |arg| $test = arg }
|
|
45
|
+
@a.trigger(:event, 1)
|
|
46
|
+
expect($test).to eql 1
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
describe "#off" do
|
|
53
|
+
|
|
54
|
+
context "when removing an existing callback" do
|
|
55
|
+
it "should not raise error" do
|
|
56
|
+
callback = proc {}
|
|
57
|
+
@a.on(:event, &callback)
|
|
58
|
+
@a.trigger(:event)
|
|
59
|
+
expect{ @a.off(:event, callback) }.not_to raise_error
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
context "after removing a callback" do
|
|
64
|
+
it "should sould raise error" do
|
|
65
|
+
callback = proc {}
|
|
66
|
+
@a.on(:event, &callback)
|
|
67
|
+
@a.trigger(:event)
|
|
68
|
+
@a.off(:event, callback)
|
|
69
|
+
expect{ @a.trigger(:event) }.to raise_error(Emittable::Error)
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
end
|
data/spec/spec_helper.rb
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
|
3
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause
|
|
4
|
+
# this file to always be loaded, without a need to explicitly require it in any
|
|
5
|
+
# files.
|
|
6
|
+
#
|
|
7
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
|
8
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
|
9
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
|
10
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
|
11
|
+
# a separate helper file that requires the additional dependencies and performs
|
|
12
|
+
# the additional setup, and require it from the spec files that actually need
|
|
13
|
+
# it.
|
|
14
|
+
#
|
|
15
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
|
16
|
+
RSpec.configure do |config|
|
|
17
|
+
# rspec-expectations config goes here. You can use an alternate
|
|
18
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
|
19
|
+
# assertions if you prefer.
|
|
20
|
+
config.expect_with :rspec do |expectations|
|
|
21
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
|
22
|
+
# and `failure_message` of custom matchers include text for helper methods
|
|
23
|
+
# defined using `chain`, e.g.:
|
|
24
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
|
25
|
+
# # => "be bigger than 2 and smaller than 4"
|
|
26
|
+
# ...rather than:
|
|
27
|
+
# # => "be bigger than 2"
|
|
28
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
|
32
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
|
33
|
+
config.mock_with :rspec do |mocks|
|
|
34
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
|
35
|
+
# a real object. This is generally recommended, and will default to
|
|
36
|
+
# `true` in RSpec 4.
|
|
37
|
+
mocks.verify_partial_doubles = true
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# This option will default to `:apply_to_host_groups` in RSpec 4 (and will
|
|
41
|
+
# have no way to turn it off -- the option exists only for backwards
|
|
42
|
+
# compatibility in RSpec 3). It causes shared context metadata to be
|
|
43
|
+
# inherited by the metadata hash of host groups and examples, rather than
|
|
44
|
+
# triggering implicit auto-inclusion in groups with matching metadata.
|
|
45
|
+
config.shared_context_metadata_behavior = :apply_to_host_groups
|
|
46
|
+
|
|
47
|
+
# The settings below are suggested to provide a good initial experience
|
|
48
|
+
# with RSpec, but feel free to customize to your heart's content.
|
|
49
|
+
=begin
|
|
50
|
+
# This allows you to limit a spec run to individual examples or groups
|
|
51
|
+
# you care about by tagging them with `:focus` metadata. When nothing
|
|
52
|
+
# is tagged with `:focus`, all examples get run. RSpec also provides
|
|
53
|
+
# aliases for `it`, `describe`, and `context` that include `:focus`
|
|
54
|
+
# metadata: `fit`, `fdescribe` and `fcontext`, respectively.
|
|
55
|
+
config.filter_run_when_matching :focus
|
|
56
|
+
|
|
57
|
+
# Allows RSpec to persist some state between runs in order to support
|
|
58
|
+
# the `--only-failures` and `--next-failure` CLI options. We recommend
|
|
59
|
+
# you configure your source control system to ignore this file.
|
|
60
|
+
config.example_status_persistence_file_path = "spec/examples.txt"
|
|
61
|
+
|
|
62
|
+
# Limits the available syntax to the non-monkey patched syntax that is
|
|
63
|
+
# recommended. For more details, see:
|
|
64
|
+
# - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
|
|
65
|
+
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
|
66
|
+
# - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
|
|
67
|
+
config.disable_monkey_patching!
|
|
68
|
+
|
|
69
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
|
70
|
+
# be too noisy due to issues in dependencies.
|
|
71
|
+
config.warnings = true
|
|
72
|
+
|
|
73
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
|
74
|
+
# file, and it's useful to allow more verbose output when running an
|
|
75
|
+
# individual spec file.
|
|
76
|
+
if config.files_to_run.one?
|
|
77
|
+
# Use the documentation formatter for detailed output,
|
|
78
|
+
# unless a formatter has already been configured
|
|
79
|
+
# (e.g. via a command-line flag).
|
|
80
|
+
config.default_formatter = "doc"
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# Print the 10 slowest examples and example groups at the
|
|
84
|
+
# end of the spec run, to help surface which specs are running
|
|
85
|
+
# particularly slow.
|
|
86
|
+
config.profile_examples = 10
|
|
87
|
+
|
|
88
|
+
# Run specs in random order to surface order dependencies. If you find an
|
|
89
|
+
# order dependency and want to debug it, you can fix the order by providing
|
|
90
|
+
# the seed, which is printed after each run.
|
|
91
|
+
# --seed 1234
|
|
92
|
+
config.order = :random
|
|
93
|
+
|
|
94
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
|
95
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
|
96
|
+
# test failures related to randomization by passing the same `--seed` value
|
|
97
|
+
# as the one that triggered the failure.
|
|
98
|
+
Kernel.srand config.seed
|
|
99
|
+
=end
|
|
100
|
+
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: emittable
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0
|
|
4
|
+
version: 0.1.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Rob Fors
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2018-
|
|
11
|
+
date: 2018-04-15 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description: A simple event registering/triggering module to mix into classes.
|
|
14
14
|
email: mail@robfors.com
|
|
@@ -19,6 +19,9 @@ files:
|
|
|
19
19
|
- LICENSE
|
|
20
20
|
- README.md
|
|
21
21
|
- lib/emittable.rb
|
|
22
|
+
- lib/emittable/error.rb
|
|
23
|
+
- spec/emittable_spec.rb
|
|
24
|
+
- spec/spec_helper.rb
|
|
22
25
|
homepage: https://github.com/robfors/emittable
|
|
23
26
|
licenses:
|
|
24
27
|
- MIT
|