audible 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +11 -0
- data/Rakefile +6 -0
- data/VERSION +1 -1
- data/audible.gemspec +3 -2
- data/lib/audible/audible.rb +13 -3
- data/spec/listening_spec.rb +37 -9
- metadata +4 -3
data/.travis.yml
ADDED
data/Rakefile
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0
|
1
|
+
0.1.0
|
data/audible.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "audible"
|
8
|
-
s.version = "0.0
|
8
|
+
s.version = "0.1.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Ben Biddington"]
|
12
|
-
s.date = "2013-07-
|
12
|
+
s.date = "2013-07-11"
|
13
13
|
s.description = "Object communications"
|
14
14
|
s.email = "ben.biddington@gmail.com"
|
15
15
|
s.extra_rdoc_files = [
|
@@ -18,6 +18,7 @@ Gem::Specification.new do |s|
|
|
18
18
|
]
|
19
19
|
s.files = [
|
20
20
|
".document",
|
21
|
+
".travis.yml",
|
21
22
|
"Gemfile",
|
22
23
|
"Gemfile.lock",
|
23
24
|
"LICENSE.txt",
|
data/lib/audible/audible.rb
CHANGED
@@ -1,20 +1,30 @@
|
|
1
1
|
module Audible
|
2
|
-
def on(
|
3
|
-
|
2
|
+
def on(*events, &block)
|
3
|
+
events.each{|e| attach(e, &block)}
|
4
4
|
end
|
5
5
|
|
6
6
|
protected
|
7
7
|
|
8
|
+
def attach(event,&block)
|
9
|
+
fail no_way_to_notify unless block_given?
|
10
|
+
|
11
|
+
listeners_for(event) << block
|
12
|
+
end
|
13
|
+
|
8
14
|
def notify(event, *args)
|
9
15
|
listeners_for(event).each do |listener|
|
10
16
|
listener.call event, args
|
11
17
|
end
|
12
18
|
end
|
13
19
|
|
14
|
-
def accepts?(e); accept_all_by_default end
|
20
|
+
def accepts?(e); accept_all_by_default; end
|
15
21
|
|
16
22
|
private
|
17
23
|
|
24
|
+
def no_way_to_notify
|
25
|
+
"No block supplied. How will I notify you?"
|
26
|
+
end
|
27
|
+
|
18
28
|
def accept_all_by_default; true; end
|
19
29
|
|
20
30
|
def listeners_for(event)
|
data/spec/listening_spec.rb
CHANGED
@@ -1,25 +1,53 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
3
|
describe "An audible object" do
|
4
|
-
|
5
|
-
|
4
|
+
let(:an_audible_object) do
|
5
|
+
an_audible_class = Class.new do
|
6
6
|
require "audible"; include Audible
|
7
7
|
|
8
|
-
def poke
|
9
|
-
|
10
|
-
end
|
11
|
-
end
|
8
|
+
def poke; notify :poked ; end
|
9
|
+
def tap ; notify :tapped; end
|
12
10
|
|
13
|
-
|
11
|
+
protected
|
14
12
|
|
13
|
+
def accepts?(e);
|
14
|
+
[:poked,:tapped].include? e
|
15
|
+
end
|
16
|
+
end.new
|
17
|
+
end
|
18
|
+
|
19
|
+
it "notifies of the event I ask for" do
|
15
20
|
notified = false
|
16
21
|
|
17
|
-
|
22
|
+
an_audible_object.on :poked do
|
18
23
|
notified = true
|
19
24
|
end
|
20
25
|
|
21
|
-
|
26
|
+
an_audible_object.poke
|
22
27
|
|
23
28
|
notified.must be_true
|
24
29
|
end
|
30
|
+
|
31
|
+
it "fails if the event does not match a supported one" do
|
32
|
+
expect{an_audible_object.on(:xxx_does_not_exist_xxx){}}.to raise_error /Event .+ not supported/
|
33
|
+
end
|
34
|
+
|
35
|
+
it "fails without a block" do
|
36
|
+
expect{an_audible_object.on :tapped}.to raise_error /No block supplied. How will I notify you?/
|
37
|
+
end
|
38
|
+
|
39
|
+
it "you can subscribe to multiple events at once and receive multiple notifications" do
|
40
|
+
notifications = 0
|
41
|
+
|
42
|
+
an_audible_object.on(:poked, :tapped) do
|
43
|
+
notifications += 1
|
44
|
+
end
|
45
|
+
|
46
|
+
an_audible_object.poke
|
47
|
+
an_audible_object.tap
|
48
|
+
|
49
|
+
notifications.must === 2
|
50
|
+
end
|
51
|
+
|
52
|
+
# TEST: what about notification order?
|
25
53
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: audible
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-07-
|
12
|
+
date: 2013-07-11 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rdoc
|
@@ -68,6 +68,7 @@ extra_rdoc_files:
|
|
68
68
|
- README.rdoc
|
69
69
|
files:
|
70
70
|
- .document
|
71
|
+
- .travis.yml
|
71
72
|
- Gemfile
|
72
73
|
- Gemfile.lock
|
73
74
|
- LICENSE.txt
|
@@ -94,7 +95,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
94
95
|
version: '0'
|
95
96
|
segments:
|
96
97
|
- 0
|
97
|
-
hash:
|
98
|
+
hash: 466976973
|
98
99
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
100
|
none: false
|
100
101
|
requirements:
|