event_emitter 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/History.txt +6 -1
- data/README.rdoc +14 -11
- data/lib/event_emitter/emitter.rb +2 -2
- data/lib/event_emitter.rb +1 -1
- data/samples/sample.rb +19 -17
- data/test/test_event_emitter.rb +8 -0
- metadata +2 -2
data/History.txt
CHANGED
data/README.rdoc
CHANGED
@@ -12,36 +12,39 @@ Ruby port of EventEmitter from Node.js
|
|
12
12
|
|
13
13
|
== SYNOPSIS:
|
14
14
|
|
15
|
-
require
|
15
|
+
require "rubygems"
|
16
|
+
require "event_emitter"
|
16
17
|
|
17
18
|
include
|
18
19
|
|
19
|
-
class
|
20
|
+
class User
|
20
21
|
include EventEmitter
|
22
|
+
attr_accessor :name
|
21
23
|
end
|
22
24
|
|
23
25
|
regist event listener
|
24
26
|
|
25
|
-
|
26
|
-
|
27
|
-
|
27
|
+
user = User.new
|
28
|
+
user.name = "shokai"
|
29
|
+
user.on :go do |data|
|
30
|
+
puts "#{name} go to #{data[:place]}"
|
28
31
|
end
|
29
32
|
|
30
33
|
call event
|
31
34
|
|
32
|
-
|
33
|
-
# =>
|
35
|
+
user.emit :go, {:place => "mountain"}
|
36
|
+
# => "shokai go to mountain"
|
34
37
|
|
35
38
|
regist event using "once"
|
36
39
|
|
37
|
-
|
38
|
-
puts data
|
40
|
+
user.once :eat do |data|
|
41
|
+
puts "#{name} -> #{data}"
|
39
42
|
end
|
40
43
|
|
41
44
|
call
|
42
45
|
|
43
|
-
|
44
|
-
|
46
|
+
user.emit :eat, "BEEF" # => "shokai -> BEEF"
|
47
|
+
user.emit :eat, "Ramen" # => do not call. call only first time.
|
45
48
|
|
46
49
|
see samples https://github.com/shokai/event_emitter/tree/master/samples
|
47
50
|
|
@@ -38,13 +38,13 @@ module EventEmitter
|
|
38
38
|
end
|
39
39
|
end
|
40
40
|
end
|
41
|
-
|
41
|
+
|
42
42
|
def emit(type, data)
|
43
43
|
events.each do |e|
|
44
44
|
if e[:type] == type.to_sym
|
45
45
|
listener = e[:listener]
|
46
46
|
remove_listener e[:id] if e[:params][:once]
|
47
|
-
listener
|
47
|
+
instance_exec(data, &listener)
|
48
48
|
end
|
49
49
|
end
|
50
50
|
end
|
data/lib/event_emitter.rb
CHANGED
data/samples/sample.rb
CHANGED
@@ -2,30 +2,32 @@
|
|
2
2
|
$:.unshift File.expand_path '../lib', File.dirname(__FILE__)
|
3
3
|
require 'event_emitter'
|
4
4
|
|
5
|
-
class
|
5
|
+
class User
|
6
6
|
include EventEmitter
|
7
|
+
attr_accessor :name
|
8
|
+
|
9
|
+
def initialize(name)
|
10
|
+
@name = name
|
11
|
+
end
|
7
12
|
end
|
8
13
|
|
9
|
-
|
14
|
+
shokai = User.new 'shokai'
|
15
|
+
ymrl = User.new 'ymrl'
|
10
16
|
|
11
|
-
|
12
|
-
puts "
|
17
|
+
shokai.on :go do |data|
|
18
|
+
puts "#{name} go to #{data[:place]}"
|
13
19
|
end
|
14
|
-
|
15
|
-
|
16
|
-
puts data.inject{|a,b|
|
17
|
-
a+b
|
18
|
-
}
|
20
|
+
ymrl.on :go do |data|
|
21
|
+
puts "#{name} go to #{data[:place]}"
|
19
22
|
end
|
20
23
|
|
21
|
-
|
22
|
-
|
23
|
-
end
|
24
|
+
shokai.emit :go, {:place => 'mountain'}
|
25
|
+
ymrl.emit :go, :place => 'cyberspace'
|
24
26
|
|
25
|
-
foo.emit 'chat', {:user => 'shokai', :message => 'hello world'}
|
26
|
-
foo.emit :chat, :user => 'ymrl', :message => 'hello work'
|
27
27
|
|
28
|
-
|
28
|
+
shokai.once :eat do |data|
|
29
|
+
puts "#{name} -> #{data}"
|
30
|
+
end
|
29
31
|
|
30
|
-
|
31
|
-
|
32
|
+
shokai.emit :eat, 'BEEF'
|
33
|
+
shokai.emit :eat, 'Ramen' # do not call. call only first time if regist with "once"
|
data/test/test_event_emitter.rb
CHANGED
@@ -4,34 +4,42 @@ class TestEventEmitter < MiniTest::Unit::TestCase
|
|
4
4
|
|
5
5
|
class Foo
|
6
6
|
include EventEmitter
|
7
|
+
attr_accessor :created_at
|
7
8
|
end
|
8
9
|
|
9
10
|
def setup
|
10
11
|
@foo = Foo.new
|
12
|
+
@foo.created_at = @now = Time.now
|
11
13
|
end
|
12
14
|
|
13
15
|
def test_on_emit
|
14
16
|
result = nil
|
17
|
+
__created_at = nil
|
15
18
|
@foo.on :chat do |data|
|
16
19
|
result = data
|
20
|
+
__created_at = created_at
|
17
21
|
end
|
18
22
|
|
19
23
|
@foo.emit :chat, :user => 'shokai', :message => 'hello world'
|
20
24
|
|
21
25
|
assert result[:user] == 'shokai'
|
22
26
|
assert result[:message] == 'hello world'
|
27
|
+
assert __created_at == @now, 'instance method'
|
23
28
|
end
|
24
29
|
|
25
30
|
def test_add_listener
|
26
31
|
result = nil
|
32
|
+
__created_at = nil
|
27
33
|
@foo.add_listener :chat do |data|
|
28
34
|
result = data
|
35
|
+
__created_at = created_at
|
29
36
|
end
|
30
37
|
|
31
38
|
@foo.emit :chat, :user => 'shokai', :message => 'hello world'
|
32
39
|
|
33
40
|
assert result[:user] == 'shokai'
|
34
41
|
assert result[:message] == 'hello world'
|
42
|
+
assert __created_at == @now, 'instance method'
|
35
43
|
end
|
36
44
|
|
37
45
|
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.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -101,7 +101,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
101
101
|
version: '0'
|
102
102
|
segments:
|
103
103
|
- 0
|
104
|
-
hash:
|
104
|
+
hash: -3803721958233588202
|
105
105
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
106
|
none: false
|
107
107
|
requirements:
|