event_emitter 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +5 -0
- data/Manifest.txt +2 -0
- data/lib/event_emitter/emitter.rb +1 -1
- data/lib/event_emitter.rb +1 -1
- data/samples/timer.rb +31 -0
- data/test/test_class_method.rb +92 -0
- data/test/test_event_emitter.rb +17 -7
- data/test/test_singular_method.rb +16 -6
- metadata +5 -2
data/History.txt
CHANGED
data/Manifest.txt
CHANGED
@@ -10,9 +10,11 @@ lib/event_emitter/emitter.rb
|
|
10
10
|
samples/class-method.rb
|
11
11
|
samples/instance-specific-method.rb
|
12
12
|
samples/sample.rb
|
13
|
+
samples/timer.rb
|
13
14
|
script/console
|
14
15
|
script/destroy
|
15
16
|
script/generate
|
17
|
+
test/test_class_method.rb
|
16
18
|
test/test_event_emitter.rb
|
17
19
|
test/test_helper.rb
|
18
20
|
test/test_singular_method.rb
|
data/lib/event_emitter.rb
CHANGED
data/samples/timer.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
$:.unshift File.expand_path '../lib', File.dirname(__FILE__)
|
3
|
+
require 'event_emitter'
|
4
|
+
|
5
|
+
class Timer
|
6
|
+
include EventEmitter
|
7
|
+
|
8
|
+
def start(sec, count)
|
9
|
+
count.times do
|
10
|
+
sleep sec
|
11
|
+
emit :tick
|
12
|
+
end
|
13
|
+
emit :end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
timer = Timer.new
|
18
|
+
|
19
|
+
timer.on :tick do
|
20
|
+
puts Time.now
|
21
|
+
end
|
22
|
+
|
23
|
+
timer.once :tick do
|
24
|
+
puts "timer start"
|
25
|
+
end
|
26
|
+
|
27
|
+
timer.on :end do
|
28
|
+
puts "timer end"
|
29
|
+
end
|
30
|
+
|
31
|
+
timer.start 1, 5
|
@@ -0,0 +1,92 @@
|
|
1
|
+
require File.expand_path 'test_helper', File.dirname(__FILE__)
|
2
|
+
|
3
|
+
class TestClassMethod < MiniTest::Unit::TestCase
|
4
|
+
|
5
|
+
class Foo
|
6
|
+
def self.created_at
|
7
|
+
@@created_at
|
8
|
+
end
|
9
|
+
def self.created_at=(time)
|
10
|
+
@@created_at = time
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def setup
|
15
|
+
Foo.created_at = @now = Time.now
|
16
|
+
EventEmitter.apply Foo
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_simple
|
20
|
+
created_at = nil
|
21
|
+
Foo.on :bar do
|
22
|
+
created_at = self.created_at
|
23
|
+
end
|
24
|
+
Foo.emit :bar
|
25
|
+
|
26
|
+
assert created_at == @now
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_on_emit
|
30
|
+
result = nil
|
31
|
+
created_at = nil
|
32
|
+
Foo.on :chat do |data|
|
33
|
+
result = data
|
34
|
+
created_at = self.created_at
|
35
|
+
end
|
36
|
+
|
37
|
+
Foo.emit :chat, :user => 'shokai', :message => 'hello world'
|
38
|
+
|
39
|
+
assert result[:user] == 'shokai'
|
40
|
+
assert result[:message] == 'hello world'
|
41
|
+
assert created_at == @now, 'instance method'
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_add_listener
|
45
|
+
result = nil
|
46
|
+
created_at = nil
|
47
|
+
Foo.add_listener :chat do |data|
|
48
|
+
result = data
|
49
|
+
created_at = self.created_at
|
50
|
+
end
|
51
|
+
|
52
|
+
Foo.emit :chat, :user => 'shokai', :message => 'hello world'
|
53
|
+
|
54
|
+
assert result[:user] == 'shokai'
|
55
|
+
assert result[:message] == 'hello world'
|
56
|
+
assert created_at == @now, 'instance method'
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_remove_listener
|
60
|
+
size = Foo.events.size
|
61
|
+
|
62
|
+
Foo.on :foo do |data|
|
63
|
+
puts "bar #{data}"
|
64
|
+
end
|
65
|
+
Foo.on :foo do |data|
|
66
|
+
puts "barbar: #{data}"
|
67
|
+
end
|
68
|
+
|
69
|
+
id = Foo.on :baz do |data|
|
70
|
+
p data
|
71
|
+
end
|
72
|
+
|
73
|
+
assert Foo.events.size == size+3, 'check registerd listener count'
|
74
|
+
Foo.remove_listener id
|
75
|
+
assert Foo.events.size == size+2, 'remove listener by id'
|
76
|
+
|
77
|
+
Foo.remove_listener :foo
|
78
|
+
assert Foo.events.size == size, 'remove all "foo" listener'
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_once
|
82
|
+
total = 0
|
83
|
+
Foo.once :add do |data|
|
84
|
+
total += data
|
85
|
+
end
|
86
|
+
|
87
|
+
Foo.emit :add, 1
|
88
|
+
assert total == 1, 'first call'
|
89
|
+
Foo.emit :add, 1
|
90
|
+
assert total == 1, 'call listener only first time'
|
91
|
+
end
|
92
|
+
end
|
data/test/test_event_emitter.rb
CHANGED
@@ -11,35 +11,45 @@ class TestEventEmitter < MiniTest::Unit::TestCase
|
|
11
11
|
@foo = Foo.new
|
12
12
|
@foo.created_at = @now = Time.now
|
13
13
|
end
|
14
|
-
|
14
|
+
|
15
|
+
def test_simple
|
16
|
+
created_at = nil
|
17
|
+
@foo.on :bar do
|
18
|
+
created_at = self.created_at
|
19
|
+
end
|
20
|
+
@foo.emit :bar
|
21
|
+
|
22
|
+
assert created_at == @now
|
23
|
+
end
|
24
|
+
|
15
25
|
def test_on_emit
|
16
26
|
result = nil
|
17
|
-
|
27
|
+
created_at = nil
|
18
28
|
@foo.on :chat do |data|
|
19
29
|
result = data
|
20
|
-
|
30
|
+
created_at = self.created_at
|
21
31
|
end
|
22
32
|
|
23
33
|
@foo.emit :chat, :user => 'shokai', :message => 'hello world'
|
24
34
|
|
25
35
|
assert result[:user] == 'shokai'
|
26
36
|
assert result[:message] == 'hello world'
|
27
|
-
assert
|
37
|
+
assert created_at == @now, 'instance method'
|
28
38
|
end
|
29
39
|
|
30
40
|
def test_add_listener
|
31
41
|
result = nil
|
32
|
-
|
42
|
+
created_at = nil
|
33
43
|
@foo.add_listener :chat do |data|
|
34
44
|
result = data
|
35
|
-
|
45
|
+
created_at = self.created_at
|
36
46
|
end
|
37
47
|
|
38
48
|
@foo.emit :chat, :user => 'shokai', :message => 'hello world'
|
39
49
|
|
40
50
|
assert result[:user] == 'shokai'
|
41
51
|
assert result[:message] == 'hello world'
|
42
|
-
assert
|
52
|
+
assert created_at == @now, 'instance method'
|
43
53
|
end
|
44
54
|
|
45
55
|
def test_remove_listener
|
@@ -11,35 +11,45 @@ class TestSingularMethod < MiniTest::Unit::TestCase
|
|
11
11
|
@foo.created_at = @now = Time.now
|
12
12
|
EventEmitter.apply @foo
|
13
13
|
end
|
14
|
+
|
15
|
+
def test_simple
|
16
|
+
created_at = nil
|
17
|
+
@foo.on :bar do
|
18
|
+
created_at = self.created_at
|
19
|
+
end
|
20
|
+
@foo.emit :bar
|
21
|
+
|
22
|
+
assert created_at == @now
|
23
|
+
end
|
14
24
|
|
15
25
|
def test_on_emit
|
16
26
|
result = nil
|
17
|
-
|
27
|
+
created_at = nil
|
18
28
|
@foo.on :chat do |data|
|
19
29
|
result = data
|
20
|
-
|
30
|
+
created_at = self.created_at
|
21
31
|
end
|
22
32
|
|
23
33
|
@foo.emit :chat, :user => 'shokai', :message => 'hello world'
|
24
34
|
|
25
35
|
assert result[:user] == 'shokai'
|
26
36
|
assert result[:message] == 'hello world'
|
27
|
-
assert
|
37
|
+
assert created_at == @now, 'instance method'
|
28
38
|
end
|
29
39
|
|
30
40
|
def test_add_listener
|
31
41
|
result = nil
|
32
|
-
|
42
|
+
created_at = nil
|
33
43
|
@foo.add_listener :chat do |data|
|
34
44
|
result = data
|
35
|
-
|
45
|
+
created_at = self.created_at
|
36
46
|
end
|
37
47
|
|
38
48
|
@foo.emit :chat, :user => 'shokai', :message => 'hello world'
|
39
49
|
|
40
50
|
assert result[:user] == 'shokai'
|
41
51
|
assert result[:message] == 'hello world'
|
42
|
-
assert
|
52
|
+
assert created_at == @now, 'instance method'
|
43
53
|
end
|
44
54
|
|
45
55
|
def test_remove_listener
|
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.0.
|
4
|
+
version: 0.0.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -81,9 +81,11 @@ files:
|
|
81
81
|
- samples/class-method.rb
|
82
82
|
- samples/instance-specific-method.rb
|
83
83
|
- samples/sample.rb
|
84
|
+
- samples/timer.rb
|
84
85
|
- script/console
|
85
86
|
- script/destroy
|
86
87
|
- script/generate
|
88
|
+
- test/test_class_method.rb
|
87
89
|
- test/test_event_emitter.rb
|
88
90
|
- test/test_helper.rb
|
89
91
|
- test/test_singular_method.rb
|
@@ -104,7 +106,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
104
106
|
version: '0'
|
105
107
|
segments:
|
106
108
|
- 0
|
107
|
-
hash: -
|
109
|
+
hash: -3918695079194691329
|
108
110
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
111
|
none: false
|
110
112
|
requirements:
|
@@ -118,6 +120,7 @@ signing_key:
|
|
118
120
|
specification_version: 3
|
119
121
|
summary: Ruby port of EventEmitter from Node.js
|
120
122
|
test_files:
|
123
|
+
- test/test_class_method.rb
|
121
124
|
- test/test_event_emitter.rb
|
122
125
|
- test/test_helper.rb
|
123
126
|
- test/test_singular_method.rb
|