event_emitter 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,8 +1,12 @@
1
- === 0.2.2 2012-01-04
1
+ === 0.2.3 2013-03-05
2
+
3
+ * catch all events with "EventEmitter#on :*"
4
+
5
+ === 0.2.2 2013-01-04
2
6
 
3
7
  * bugfix
4
8
 
5
- === 0.2.1 2012-01-04
9
+ === 0.2.1 2013-01-04
6
10
 
7
11
  * use bundler gem template
8
12
 
data/README.md CHANGED
@@ -13,8 +13,11 @@ Install
13
13
  Requirements
14
14
  ------------
15
15
 
16
+ testing on
17
+
16
18
  * Ruby 1.8.7+
17
19
  * Ruby 1.9.2+
20
+ * Ruby 2.0.0+
18
21
  * JRuby 1.6.7+
19
22
 
20
23
 
@@ -78,6 +81,14 @@ user.remove_listener :go
78
81
  user.remove_listener event_id
79
82
  ```
80
83
 
84
+ catch all events
85
+ ```ruby
86
+ user.on :* do |event_name, args|
87
+ puts event_name + " called"
88
+ p args
89
+ end
90
+ ```
91
+
81
92
  see samples https://github.com/shokai/event_emitter/tree/master/samples
82
93
 
83
94
 
@@ -45,10 +45,15 @@ module EventEmitter
45
45
 
46
46
  def emit(type, *data)
47
47
  __events.each do |e|
48
- if e[:type] == type.to_sym
48
+ case e[:type]
49
+ when type.to_sym
49
50
  listener = e[:listener]
50
51
  remove_listener e[:id] if e[:params][:once]
51
52
  instance_exec(*data, &listener)
53
+ when :*
54
+ listener = e[:listener]
55
+ remove_listener e[:id] if e[:params][:once]
56
+ instance_exec(type, *data, &listener)
52
57
  end
53
58
  end
54
59
  end
@@ -1,3 +1,3 @@
1
1
  module EventEmitter
2
- VERSION = '0.2.2'
2
+ VERSION = '0.2.3'
3
3
  end
@@ -0,0 +1,61 @@
1
+ require File.expand_path 'test_helper', File.dirname(__FILE__)
2
+
3
+ class TestCatchAllEvents < MiniTest::Unit::TestCase
4
+
5
+ class Foo
6
+ include EventEmitter
7
+ attr_accessor :created_at
8
+ end
9
+
10
+ def setup
11
+ @foo = Foo.new
12
+ @foo.created_at = @now = Time.now
13
+ end
14
+
15
+ def test_catch_all_emits
16
+ created_at = nil
17
+ created_at_ = nil
18
+ called_event = nil
19
+ @foo.on :* do |event|
20
+ called_event = event
21
+ created_at = self.created_at
22
+ end
23
+ @foo.on :bar do
24
+ created_at_ = self.created_at
25
+ end
26
+ @foo.emit :bar
27
+
28
+ assert created_at == @now
29
+ assert called_event == :bar
30
+ assert created_at_ == @now
31
+ end
32
+
33
+ def test_catch_all_emits_with_args
34
+ arg1_ = nil
35
+ arg2_ = nil
36
+ called_event = nil
37
+ @foo.on :* do |event, arg1, arg2|
38
+ called_event = event
39
+ arg1_ = arg1
40
+ arg2_ = arg2
41
+ end
42
+ @foo.emit :bar, 'kazusuke', 'zanmai'
43
+
44
+ assert called_event == :bar
45
+ assert arg1_ == 'kazusuke'
46
+ assert arg2_ == 'zanmai'
47
+ end
48
+
49
+ def test_once
50
+ total = 0
51
+ @foo.once :* do |event, data|
52
+ total += data[:num] if event == :add
53
+ end
54
+
55
+ @foo.emit :add, :num => 10
56
+ assert total == 10, 'first call'
57
+ @foo.emit :add, :num => 5
58
+ assert total == 10, 'call listener only first time'
59
+ end
60
+
61
+ end
@@ -22,6 +22,14 @@ class TestEventEmitter < MiniTest::Unit::TestCase
22
22
  assert created_at == @now
23
23
  end
24
24
 
25
+ def test_string_event_name
26
+ created_at = nil
27
+ @foo.on "bar" do
28
+ created_at = self.created_at
29
+ end
30
+ @foo.emit :bar
31
+ end
32
+
25
33
  def test_on_emit
26
34
  result = nil
27
35
  created_at = nil
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: event_emitter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
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-01-03 00:00:00.000000000 Z
12
+ date: 2013-03-05 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Ruby port of EventEmitter from Node.js
15
15
  email:
@@ -32,6 +32,7 @@ files:
32
32
  - samples/instance-specific-method.rb
33
33
  - samples/sample.rb
34
34
  - samples/timer.rb
35
+ - test/test_catch_all_events.rb
35
36
  - test/test_class_method.rb
36
37
  - test/test_event_emitter.rb
37
38
  - test/test_helper.rb
@@ -61,6 +62,7 @@ signing_key:
61
62
  specification_version: 3
62
63
  summary: Ruby port of EventEmitter from Node.js
63
64
  test_files:
65
+ - test/test_catch_all_events.rb
64
66
  - test/test_class_method.rb
65
67
  - test/test_event_emitter.rb
66
68
  - test/test_helper.rb