event_object 0.9.1 → 0.9.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/event_object/array.rb +2 -2
- data/lib/event_object/object.rb +12 -8
- data/lib/event_object/version.rb +1 -1
- data/spec/event_object_spec.rb +47 -4
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5e73347f0743cee6ad8f9e61bde232ce90508b15
|
4
|
+
data.tar.gz: 030aab40ae2016c25031ab3e7136e384b0e9f67b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 170c391c95445cf10d497356ec4c3c0a71a1f395e1994c0ce19d21fed8d9ba6173b5227e35877319bd3165cbeffc23508ff1a12e4446e911b89ca508d1ece80b
|
7
|
+
data.tar.gz: c177bb1525cedb2eb5e7398372e86601e50d7bc38eae0b3c1eeca0836347c91ed6ec596bb7d964a5fd46eaca893e0d37ea2807c7b06cdd16d6cbd972b4efba3d
|
data/lib/event_object/array.rb
CHANGED
@@ -2,11 +2,11 @@ module EventObject
|
|
2
2
|
refine Array do
|
3
3
|
# array like event
|
4
4
|
def fire(*a, &b)
|
5
|
-
collect { |h| h.call(*a, &b) }
|
5
|
+
dup.collect { |h| h.call(*a, &b) }
|
6
6
|
end
|
7
7
|
|
8
8
|
def fire_for(t, *a)
|
9
|
-
collect { |h| t.instance_exec(*a, &h) }
|
9
|
+
dup.collect { |h| t.instance_exec(*a, &h) }
|
10
10
|
end
|
11
11
|
|
12
12
|
def off(proc=nil)
|
data/lib/event_object/object.rb
CHANGED
@@ -8,19 +8,19 @@ module EventObject
|
|
8
8
|
instance_variable_get("@#{name}") || instance_variable_set("@#{name}", (init.is_a?(Class) ? init.new : init))
|
9
9
|
end
|
10
10
|
|
11
|
-
def on(name, &b)
|
12
|
-
var(name, Array).tap { |e| e << b }; b
|
11
|
+
def on(name, bb=nil, &b)
|
12
|
+
var(name, Array).tap { |e| e << (bb || b) }; (bb || b)
|
13
13
|
end
|
14
14
|
|
15
15
|
#
|
16
|
-
# obj.off(:
|
17
|
-
# obj.off(:
|
18
|
-
# obj.off(
|
16
|
+
# obj.off(:ev)
|
17
|
+
# obj.off(:ev, proc)
|
18
|
+
# obj.off(:ev, target, :meth)
|
19
19
|
def off(*a)
|
20
20
|
case a.size
|
21
21
|
when 1 then var(a[0], Array).clear
|
22
22
|
when 2 then var(a[0], Array).tap { |e| e.delete a[1] }
|
23
|
-
when 3 then a[0].
|
23
|
+
when 3 then var(a[0], Array).tap { |e| e.delete a[1].method(a[2]) }
|
24
24
|
end
|
25
25
|
self
|
26
26
|
end
|
@@ -31,8 +31,12 @@ module EventObject
|
|
31
31
|
}
|
32
32
|
end
|
33
33
|
|
34
|
-
def listen(
|
35
|
-
|
34
|
+
def listen(target, ev, meth)
|
35
|
+
target.on ev, method(meth)
|
36
|
+
end
|
37
|
+
|
38
|
+
def stop_listen(target, ev)
|
39
|
+
target.var(ev, Array).delete_if { |e| e.is_a?(Method) && e.receiver == self }
|
36
40
|
end
|
37
41
|
end
|
38
42
|
end
|
data/lib/event_object/version.rb
CHANGED
data/spec/event_object_spec.rb
CHANGED
@@ -1,7 +1,18 @@
|
|
1
1
|
require "spec_helper"
|
2
|
+
|
2
3
|
describe EventObject do
|
3
4
|
using EventObject
|
4
5
|
|
6
|
+
class EventEmitter
|
7
|
+
events :success, :error
|
8
|
+
end
|
9
|
+
|
10
|
+
class ParentObject
|
11
|
+
def ok
|
12
|
+
'OK'
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
5
16
|
it "should create array of events" do
|
6
17
|
events = []
|
7
18
|
proc = lambda { :result }
|
@@ -23,10 +34,6 @@ describe EventObject do
|
|
23
34
|
end
|
24
35
|
|
25
36
|
it "should create events and fire them" do
|
26
|
-
class EventEmitter
|
27
|
-
events :success, :error
|
28
|
-
end
|
29
|
-
|
30
37
|
emitter = EventEmitter.new
|
31
38
|
emitter.on :success do
|
32
39
|
18
|
@@ -40,4 +47,40 @@ describe EventObject do
|
|
40
47
|
expect(emitter.error).to be_empty
|
41
48
|
expect(emitter.error.fire).to be_empty
|
42
49
|
end
|
50
|
+
|
51
|
+
# dup required in fires
|
52
|
+
it "add event in event" do
|
53
|
+
emitter = EventEmitter.new
|
54
|
+
counter = 0
|
55
|
+
emitter.on :success do
|
56
|
+
counter += 1
|
57
|
+
emitter.on :success do
|
58
|
+
counter += 1
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
emitter.success!
|
63
|
+
|
64
|
+
expect(counter).to be 1
|
65
|
+
end
|
66
|
+
|
67
|
+
it "listen other object" do
|
68
|
+
emitter = EventEmitter.new
|
69
|
+
obj = ParentObject.new
|
70
|
+
obj.listen(emitter, :success, :ok)
|
71
|
+
expect(emitter.success!.last).to eq 'OK'
|
72
|
+
|
73
|
+
emitter.off(:success, obj, :ok)
|
74
|
+
expect(emitter.success!).to be_empty
|
75
|
+
end
|
76
|
+
|
77
|
+
it "stop listen" do
|
78
|
+
emitter = EventEmitter.new
|
79
|
+
obj = ParentObject.new
|
80
|
+
obj.listen(emitter, :success, :ok)
|
81
|
+
expect(emitter.success!.last).to eq 'OK'
|
82
|
+
|
83
|
+
obj.stop_listen(emitter, :success)
|
84
|
+
expect(emitter.success!).to be_empty
|
85
|
+
end
|
43
86
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: event_object
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- che
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-04-
|
11
|
+
date: 2015-04-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|