eventful 1.0.0 → 1.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.
- data/History.txt +6 -0
- data/{README.txt → README.rdoc} +35 -44
- data/lib/eventful.rb +0 -1
- data/{test/test_eventful.rb → spec/eventful_spec.rb} +22 -24
- data/spec/spec_helper.rb +4 -0
- metadata +63 -62
- data/Manifest.txt +0 -6
- data/Rakefile +0 -12
data/History.txt
CHANGED
data/{README.txt → README.rdoc}
RENAMED
@@ -1,18 +1,10 @@
|
|
1
1
|
= Eventful
|
2
2
|
|
3
|
-
* http://github.com/jcoglan/eventful
|
4
|
-
|
5
3
|
+Eventful+ is a small extension on top of Ruby's +Observable+ module that
|
6
|
-
implements named events, block listeners and event bubbling. It allows
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
on event type.
|
11
|
-
|
12
|
-
|
13
|
-
== Installation
|
14
|
-
|
15
|
-
sudo gem install eventful
|
4
|
+
implements named events, block listeners and event bubbling. It allows much
|
5
|
+
more flexible event handling behaviour than is typically allowed by
|
6
|
+
+Observable+, which requires listeners to be objects that implement +update+
|
7
|
+
and provides no simple way of calling subsets of observers based on event type.
|
16
8
|
|
17
9
|
|
18
10
|
== Examples
|
@@ -23,10 +15,9 @@ Make a class listenable by mixing +Eventful+ into it:
|
|
23
15
|
include Eventful
|
24
16
|
end
|
25
17
|
|
26
|
-
Register event listeners using +on+ with an event name and a block.
|
27
|
-
|
28
|
-
|
29
|
-
to +fire+.
|
18
|
+
Register event listeners using +on+ with an event name and a block. Publish
|
19
|
+
events using +fire+ with the event name. The block accepts the object that
|
20
|
+
published the event, along with any parameters passed to +fire+.
|
30
21
|
|
31
22
|
w = Watcher.new
|
32
23
|
|
@@ -51,8 +42,8 @@ so you can remove it using +delete_observer+.
|
|
51
42
|
|
52
43
|
=== Method chains instead of blocks
|
53
44
|
|
54
|
-
Instead of passing a block, you can add behaviour to objects by chaining
|
55
|
-
|
45
|
+
Instead of passing a block, you can add behaviour to objects by chaining method
|
46
|
+
calls after the +on+ call. For example:
|
56
47
|
|
57
48
|
class Logger
|
58
49
|
include Eventful
|
@@ -71,10 +62,10 @@ method calls after the +on+ call. For example:
|
|
71
62
|
|
72
63
|
=== Events that bubble
|
73
64
|
|
74
|
-
When you +fire+ an event, the event 'bubbles' up the type system. What
|
75
|
-
|
76
|
-
|
77
|
-
|
65
|
+
When you +fire+ an event, the event 'bubbles' up the type system. What this
|
66
|
+
means is that you can listen to events on all the instances of a class just by
|
67
|
+
placing an event listener on the class itself. As above, the listener is called
|
68
|
+
with the instance that fired the event.
|
78
69
|
|
79
70
|
Logger.on(:receive) { |log, msg| puts "#{ log } :: #{ msg }" }
|
80
71
|
|
@@ -87,8 +78,8 @@ the listener is called with the instance that fired the event.
|
|
87
78
|
# #<Logger:0xb7bf103c> :: The first message
|
88
79
|
# #<Logger:0xb7bf1028> :: Another event
|
89
80
|
|
90
|
-
Method chains can also be used, and they will be replayed on the instance
|
91
|
-
|
81
|
+
Method chains can also be used, and they will be replayed on the instance that
|
82
|
+
initiated the event.
|
92
83
|
|
93
84
|
# Calls `log.print "Received message"`
|
94
85
|
|
@@ -102,23 +93,23 @@ that initiated the event.
|
|
102
93
|
|
103
94
|
(The MIT License)
|
104
95
|
|
105
|
-
Copyright (c) 2009 James Coglan
|
106
|
-
|
107
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
IN
|
122
|
-
|
123
|
-
|
124
|
-
|
96
|
+
Copyright (c) 2009-2012 James Coglan
|
97
|
+
|
98
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
99
|
+
this software and associated documentation files (the 'Software'), to deal in
|
100
|
+
the Software without restriction, including without limitation the rights to
|
101
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
102
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
103
|
+
so, subject to the following conditions:
|
104
|
+
|
105
|
+
The above copyright notice and this permission notice shall be included in all
|
106
|
+
copies or substantial portions of the Software.
|
107
|
+
|
108
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
109
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
110
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
111
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
112
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
113
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
114
|
+
SOFTWARE.
|
115
|
+
|
data/lib/eventful.rb
CHANGED
@@ -1,6 +1,4 @@
|
|
1
|
-
require "
|
2
|
-
require "eventful"
|
3
|
-
require "set"
|
1
|
+
require "spec_helper"
|
4
2
|
|
5
3
|
class Foo
|
6
4
|
include Eventful
|
@@ -19,31 +17,31 @@ class Bar
|
|
19
17
|
include Eventful
|
20
18
|
end
|
21
19
|
|
22
|
-
|
23
|
-
|
20
|
+
describe Eventful do
|
21
|
+
before do
|
24
22
|
[Foo, Bar, Eventful].each &it.delete_observers
|
25
23
|
end
|
26
24
|
|
27
|
-
|
25
|
+
it "fires events" do
|
28
26
|
ayes, noes = 0, 0
|
29
27
|
f = Foo.new
|
30
28
|
f.on(:aye) { |foo, x| ayes += x }
|
31
29
|
obs = f.on(:noe) { |foo, x| noes += x }
|
32
30
|
|
33
31
|
f.fire(:aye, 1)
|
34
|
-
|
35
|
-
|
32
|
+
ayes.should == 1
|
33
|
+
noes.should == 0
|
36
34
|
f.fire(:noe, 3)
|
37
|
-
|
38
|
-
|
35
|
+
ayes.should == 1
|
36
|
+
noes.should == 3
|
39
37
|
|
40
38
|
f.delete_observer(obs)
|
41
39
|
f.fire(:noe, 3)
|
42
|
-
|
43
|
-
|
40
|
+
ayes.should == 1
|
41
|
+
noes.should == 3
|
44
42
|
end
|
45
43
|
|
46
|
-
|
44
|
+
it "allows chaining" do
|
47
45
|
f = Foo.new
|
48
46
|
f.on(:aye).bump! 2
|
49
47
|
f.on(:noe).bump! -1
|
@@ -51,10 +49,10 @@ class TestEventful < Test::Unit::TestCase
|
|
51
49
|
2.times { f.fire(:aye) }
|
52
50
|
f.fire(:noe)
|
53
51
|
|
54
|
-
|
52
|
+
f.count.should == 3
|
55
53
|
end
|
56
54
|
|
57
|
-
|
55
|
+
it "bubbles events" do
|
58
56
|
bar1, bar2 = Bar.new, Bar.new
|
59
57
|
list = []
|
60
58
|
Bar.on(:aye) { |r| list << r }
|
@@ -62,24 +60,24 @@ class TestEventful < Test::Unit::TestCase
|
|
62
60
|
Eventful.on(:noe) { |r| list << r }
|
63
61
|
|
64
62
|
bar1.fire(:aye)
|
65
|
-
|
63
|
+
list.should == [bar1, bar1]
|
66
64
|
bar2.fire(:noe)
|
67
|
-
|
65
|
+
list.should == [bar1, bar1, bar2]
|
68
66
|
|
69
67
|
Bar.fire(:aye)
|
70
|
-
|
68
|
+
list.should == [bar1, bar1, bar2, Bar]
|
71
69
|
Bar.fire(:noe)
|
72
|
-
|
70
|
+
list.should == [bar1, bar1, bar2, Bar]
|
73
71
|
end
|
74
72
|
|
75
|
-
|
73
|
+
it "allows chaining on bubble" do
|
76
74
|
f1, f2 = Foo.new, Foo.new
|
77
75
|
Foo.on(:aye).bump! 5
|
78
76
|
f1.fire(:aye)
|
79
|
-
|
80
|
-
|
77
|
+
f1.count.should == 5
|
78
|
+
f2.count.should == 0
|
81
79
|
f2.fire(:aye)
|
82
|
-
|
83
|
-
|
80
|
+
f1.count.should == 5
|
81
|
+
f2.count.should == 5
|
84
82
|
end
|
85
83
|
end
|
data/spec/spec_helper.rb
ADDED
metadata
CHANGED
@@ -1,83 +1,84 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: eventful
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
prerelease:
|
5
6
|
platform: ruby
|
6
|
-
authors:
|
7
|
+
authors:
|
7
8
|
- James Coglan
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
dependencies:
|
15
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2012-12-22 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
16
15
|
name: methodphitamine
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - '='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.0.0
|
17
22
|
type: :runtime
|
18
|
-
|
19
|
-
version_requirements: !ruby/object:Gem::Requirement
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
- !ruby/object:Gem::Dependency
|
26
|
-
name:
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - '='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 1.0.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
27
38
|
type: :development
|
28
|
-
|
29
|
-
version_requirements: !ruby/object:Gem::Requirement
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
description:
|
36
|
-
email:
|
37
|
-
- jcoglan@googlemail.com
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description:
|
47
|
+
email: jcoglan@gmail.com
|
38
48
|
executables: []
|
39
|
-
|
40
49
|
extensions: []
|
41
|
-
|
42
|
-
|
50
|
+
extra_rdoc_files:
|
51
|
+
- README.rdoc
|
52
|
+
files:
|
53
|
+
- README.rdoc
|
43
54
|
- History.txt
|
44
|
-
- Manifest.txt
|
45
|
-
- README.txt
|
46
|
-
files:
|
47
|
-
- History.txt
|
48
|
-
- Manifest.txt
|
49
|
-
- README.txt
|
50
|
-
- Rakefile
|
51
55
|
- lib/eventful.rb
|
52
|
-
-
|
53
|
-
|
56
|
+
- spec/eventful_spec.rb
|
57
|
+
- spec/spec_helper.rb
|
54
58
|
homepage: http://github.com/jcoglan/eventful
|
55
59
|
licenses: []
|
56
|
-
|
57
60
|
post_install_message:
|
58
|
-
rdoc_options:
|
61
|
+
rdoc_options:
|
59
62
|
- --main
|
60
|
-
- README.
|
61
|
-
require_paths:
|
63
|
+
- README.rdoc
|
64
|
+
require_paths:
|
62
65
|
- lib
|
63
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ! '>='
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
75
78
|
requirements: []
|
76
|
-
|
77
|
-
|
78
|
-
rubygems_version: 1.3.3
|
79
|
+
rubyforge_project:
|
80
|
+
rubygems_version: 1.8.23
|
79
81
|
signing_key:
|
80
82
|
specification_version: 3
|
81
|
-
summary:
|
82
|
-
test_files:
|
83
|
-
- test/test_eventful.rb
|
83
|
+
summary: A little pub/sub abstraction based on Observable
|
84
|
+
test_files: []
|
data/Manifest.txt
DELETED
data/Rakefile
DELETED
@@ -1,12 +0,0 @@
|
|
1
|
-
# -*- ruby -*-
|
2
|
-
|
3
|
-
require 'rubygems'
|
4
|
-
require 'hoe'
|
5
|
-
|
6
|
-
Hoe.spec 'eventful' do |p|
|
7
|
-
# self.rubyforge_name = 'eventfulx' # if different than 'eventful'
|
8
|
-
p.developer('James Coglan', 'jcoglan@googlemail.com')
|
9
|
-
p.extra_deps = %w[methodphitamine]
|
10
|
-
end
|
11
|
-
|
12
|
-
# vim: syntax=ruby
|