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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c9622ba5227d8436103a9b8d27c5c8c43499b81a
4
- data.tar.gz: 47849679660a76bc72bdc824e901a4f7318c8baf
3
+ metadata.gz: 5e73347f0743cee6ad8f9e61bde232ce90508b15
4
+ data.tar.gz: 030aab40ae2016c25031ab3e7136e384b0e9f67b
5
5
  SHA512:
6
- metadata.gz: 4475ff5a3264ab6b919b6f958edcbf06ba677cc267ab024206892067e6bace0776c50b815c8f46abf7a17fd5b8296c4b120b2d62eb64e7a518a4afa028979bab
7
- data.tar.gz: 0003f2b71961aeef736f64d04b3f8347c2601e0e6ebe4e7f0d48e7d277a54e9defa592529def1abfebf6d50ce021b8ff7acf938c54958364023cd8ae7e818509
6
+ metadata.gz: 170c391c95445cf10d497356ec4c3c0a71a1f395e1994c0ce19d21fed8d9ba6173b5227e35877319bd3165cbeffc23508ff1a12e4446e911b89ca508d1ece80b
7
+ data.tar.gz: c177bb1525cedb2eb5e7398372e86601e50d7bc38eae0b3c1eeca0836347c91ed6ec596bb7d964a5fd46eaca893e0d37ea2807c7b06cdd16d6cbd972b4efba3d
@@ -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)
@@ -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(:data)
17
- # obj.off(:data, proc)
18
- # obj.off(target, :data, proc) => target.off(:data, proc)
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].off(a[1], a[2])
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(object, name, meth)
35
- object.on :name, &method(meth)
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
@@ -1,3 +1,3 @@
1
1
  module EventObject
2
- VERSION = "0.9.1"
2
+ VERSION = "0.9.2"
3
3
  end
@@ -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.1
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-19 00:00:00.000000000 Z
11
+ date: 2015-04-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler