simple_events 0.0.1
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 +7 -0
- data/.gitignore +18 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +42 -0
- data/Rakefile +12 -0
- data/lib/simple_events.rb +2 -0
- data/lib/simple_events/notifier.rb +62 -0
- data/lib/simple_events/version.rb +3 -0
- data/simple_events.gemspec +25 -0
- data/test/lib/simple_events/notifier_test.rb +79 -0
- data/test/lib/simple_events_test.rb +7 -0
- data/test/minitest_helper.rb +4 -0
- metadata +103 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2fe99b62b6f4ad0043c5f399e3f361dce022969c
|
4
|
+
data.tar.gz: 01bfd3635def53a48f57ef87fdcb7a2bc7915cd3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7689b5343f2452a2e27c35defa264aeeafb0f93c016b2571a5ab65b34f926feebc46e21587a30a525e99c5cb739bae84cff102aabc9434d1630b45852788cc1a
|
7
|
+
data.tar.gz: 2bab23fe61c6df2881765f59394faa44c95824ff5fdc75c2043b19785f778cf03a726c986d310e05e4497d6042c1dd024bc11ca9d46bb2217cccfa6185997178
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
MIT License
|
2
|
+
----
|
3
|
+
|
4
|
+
Copyright (c) 2013 Jason R. Clark
|
5
|
+
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
8
|
+
in the Software without restriction, including without limitation the rights
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
11
|
+
furnished to do so, subject to the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be included in
|
14
|
+
all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# SimpleEvents
|
2
|
+
|
3
|
+
A basic eventing system
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'simple_events'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install simple_events
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Create a new event notifier:
|
22
|
+
|
23
|
+
events = SimpleEvent::Notifier.new
|
24
|
+
|
25
|
+
Subscribe to an event:
|
26
|
+
|
27
|
+
events.subscribe(:all_done) do |*args|
|
28
|
+
# Handle the event here...
|
29
|
+
end
|
30
|
+
|
31
|
+
Notify of an event:
|
32
|
+
|
33
|
+
events.notify(:all_done, "pass", "along", "args")
|
34
|
+
|
35
|
+
|
36
|
+
## Contributing
|
37
|
+
|
38
|
+
1. Fork it
|
39
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
40
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
41
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
42
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'logger'
|
4
|
+
|
5
|
+
module SimpleEvents
|
6
|
+
class Notifier
|
7
|
+
|
8
|
+
attr_accessor :runaway_threshold
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
@events = {}
|
12
|
+
@runaway_threshold = 100
|
13
|
+
end
|
14
|
+
|
15
|
+
def subscribe(event, &handler)
|
16
|
+
@events[event] ||= []
|
17
|
+
@events[event] << handler
|
18
|
+
check_for_runaway_subscriptions(event)
|
19
|
+
end
|
20
|
+
|
21
|
+
def check_for_runaway_subscriptions(event)
|
22
|
+
count = @events[event].size
|
23
|
+
logger.debug("Run-away event subscription on #{event}? Subscribed #{count}") if count > @runaway_threshold
|
24
|
+
end
|
25
|
+
|
26
|
+
def clear
|
27
|
+
@events.clear
|
28
|
+
end
|
29
|
+
|
30
|
+
def notify(event, *args)
|
31
|
+
return unless @events.has_key?(event)
|
32
|
+
|
33
|
+
@events[event].each do |handler|
|
34
|
+
begin
|
35
|
+
handler.call(*args)
|
36
|
+
rescue => err
|
37
|
+
logger.debug("Failure during notify for #{@event}\n#{err.class} #{err.message}")
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
@@logger = nil
|
43
|
+
|
44
|
+
def self.logger
|
45
|
+
@@logger ||= ::Logger.new($stdout)
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.logger=(new_logger)
|
49
|
+
@@logger = new_logger
|
50
|
+
end
|
51
|
+
|
52
|
+
def logger
|
53
|
+
self.class.logger
|
54
|
+
end
|
55
|
+
|
56
|
+
def logger=(new_logger)
|
57
|
+
self.class.logger = new_logger
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'simple_events/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = "simple_events"
|
9
|
+
spec.version = SimpleEvents::VERSION
|
10
|
+
spec.authors = ["Jason R. Clark"]
|
11
|
+
spec.email = ["jason@jasonrclark.net"]
|
12
|
+
spec.description = %q{A basic event notification system}
|
13
|
+
spec.summary = %q{Simple Events provides basic, synchronous, in-process event dispatch to help in decoupling your app}
|
14
|
+
spec.homepage = "http://github.com/jasonrclark/simple_events"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files`.split($/)
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
23
|
+
spec.add_development_dependency "rake"
|
24
|
+
spec.add_development_dependency "minitest"
|
25
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require File.expand_path(File.join(File.dirname(__FILE__),'..','..','minitest_helper'))
|
4
|
+
|
5
|
+
class NotifierTest < MiniTest::Unit::TestCase
|
6
|
+
|
7
|
+
def setup
|
8
|
+
@events = SimpleEvents::Notifier.new
|
9
|
+
@events.logger = MiniTest::Mock.new
|
10
|
+
@events.logger.expect(:debug, nil, [String])
|
11
|
+
|
12
|
+
@called = false
|
13
|
+
@called_with = nil
|
14
|
+
|
15
|
+
@check_method = Proc.new do |*args|
|
16
|
+
@called = true
|
17
|
+
@called_with = args
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def teardown
|
22
|
+
@events.logger = nil
|
23
|
+
end
|
24
|
+
|
25
|
+
#
|
26
|
+
# Tests
|
27
|
+
#
|
28
|
+
|
29
|
+
def test_notifies
|
30
|
+
@events.subscribe(:before_call, &@check_method)
|
31
|
+
@events.notify(:before_call, :env => "env")
|
32
|
+
|
33
|
+
assert_was_called
|
34
|
+
assert_equal([{:env => "env"}], @called_with)
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_failure_during_notify_doesnt_block_other_hooks
|
38
|
+
@events.subscribe(:after_call) { raise "Boo!" }
|
39
|
+
@events.subscribe(:after_call, &@check_method)
|
40
|
+
|
41
|
+
@events.notify(:after_call)
|
42
|
+
|
43
|
+
assert_was_called
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_runaway_events
|
47
|
+
logger = MiniTest::Mock.new
|
48
|
+
logger.expect(:debug, nil, [String])
|
49
|
+
SimpleEvents::Notifier.logger = logger
|
50
|
+
|
51
|
+
@events.runaway_threshold = 0
|
52
|
+
@events.subscribe(:my_event) {}
|
53
|
+
|
54
|
+
logger.verify
|
55
|
+
ensure
|
56
|
+
SimpleEvents::Notifier.logger = nil
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_clear
|
60
|
+
@events.subscribe(:after_call, &@check_method)
|
61
|
+
@events.clear
|
62
|
+
@events.notify(:after_call)
|
63
|
+
|
64
|
+
assert_was_not_called
|
65
|
+
end
|
66
|
+
|
67
|
+
#
|
68
|
+
# Helpers
|
69
|
+
#
|
70
|
+
|
71
|
+
def assert_was_called
|
72
|
+
assert @called, "Event wasn't called"
|
73
|
+
end
|
74
|
+
|
75
|
+
def assert_was_not_called
|
76
|
+
assert !@called, "Event was called"
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
metadata
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_events
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jason R. Clark
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-06-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: A basic event notification system
|
56
|
+
email:
|
57
|
+
- jason@jasonrclark.net
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- .travis.yml
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- lib/simple_events.rb
|
69
|
+
- lib/simple_events/notifier.rb
|
70
|
+
- lib/simple_events/version.rb
|
71
|
+
- simple_events.gemspec
|
72
|
+
- test/lib/simple_events/notifier_test.rb
|
73
|
+
- test/lib/simple_events_test.rb
|
74
|
+
- test/minitest_helper.rb
|
75
|
+
homepage: http://github.com/jasonrclark/simple_events
|
76
|
+
licenses:
|
77
|
+
- MIT
|
78
|
+
metadata: {}
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options: []
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - '>='
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
requirements: []
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 2.0.0
|
96
|
+
signing_key:
|
97
|
+
specification_version: 4
|
98
|
+
summary: Simple Events provides basic, synchronous, in-process event dispatch to help
|
99
|
+
in decoupling your app
|
100
|
+
test_files:
|
101
|
+
- test/lib/simple_events/notifier_test.rb
|
102
|
+
- test/lib/simple_events_test.rb
|
103
|
+
- test/minitest_helper.rb
|