on 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/on/version.rb +1 -1
- data/lib/on.rb +8 -2
- data/test/on_test.rb +3 -0
- metadata +4 -4
data/lib/on/version.rb
CHANGED
data/lib/on.rb
CHANGED
@@ -23,10 +23,14 @@ require 'set'
|
|
23
23
|
# end
|
24
24
|
# end
|
25
25
|
class On
|
26
|
+
# Returns the Callback called or +nil+ if none called.
|
27
|
+
attr_reader :callback
|
28
|
+
|
26
29
|
def initialize(*callbacks, &block)
|
27
30
|
raise ArgumentError, "please provide at least one callback" if callbacks.empty?
|
28
31
|
raise ArgumentError, "please provide a block" unless block
|
29
32
|
@callbacks = Set.new(callbacks)
|
33
|
+
@callback = nil
|
30
34
|
@block = block
|
31
35
|
end
|
32
36
|
|
@@ -41,11 +45,13 @@ class On
|
|
41
45
|
def on(name, &block)
|
42
46
|
validate_callback!(name)
|
43
47
|
if @callback && @callback.name == name
|
44
|
-
block.call(
|
48
|
+
block.call(*callback.args)
|
45
49
|
end
|
46
50
|
end
|
47
51
|
|
48
|
-
|
52
|
+
# Represents a callback called with its +name+ and +args+.
|
53
|
+
class Callback < Struct.new(:name, :args)
|
54
|
+
end
|
49
55
|
|
50
56
|
class InvalidCallback < StandardError # :nodoc:
|
51
57
|
def initialize(name)
|
data/test/on_test.rb
CHANGED
@@ -46,6 +46,8 @@ class OnTest < Testem
|
|
46
46
|
on.call :success, :foo, :bar
|
47
47
|
|
48
48
|
assert_called [ :block ], [ :success, :foo, :bar ]
|
49
|
+
assert on.callback
|
50
|
+
assert_equal :success, on.callback.name
|
49
51
|
end
|
50
52
|
|
51
53
|
test "calls invalid callback" do
|
@@ -79,6 +81,7 @@ class OnTest < Testem
|
|
79
81
|
end
|
80
82
|
|
81
83
|
assert_nothing_called
|
84
|
+
assert_nil on.callback
|
82
85
|
end
|
83
86
|
end
|
84
87
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: 'on'
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.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: 2012-11-
|
12
|
+
date: 2012-11-29 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
@@ -110,7 +110,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
110
110
|
version: '0'
|
111
111
|
segments:
|
112
112
|
- 0
|
113
|
-
hash:
|
113
|
+
hash: 3260149224139585846
|
114
114
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
115
115
|
none: false
|
116
116
|
requirements:
|
@@ -119,7 +119,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
119
119
|
version: '0'
|
120
120
|
segments:
|
121
121
|
- 0
|
122
|
-
hash:
|
122
|
+
hash: 3260149224139585846
|
123
123
|
requirements: []
|
124
124
|
rubyforge_project:
|
125
125
|
rubygems_version: 1.8.24
|