event_emitter 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +2 -0
- data/History.txt +4 -0
- data/Manifest.txt +2 -0
- data/README.rdoc +15 -0
- data/lib/event_emitter/emitter.rb +4 -0
- data/lib/event_emitter.rb +1 -1
- data/samples/instance-specific-method.rb +29 -0
- data/test/test_singular_method.rb +76 -0
- metadata +5 -2
data/Gemfile.lock
CHANGED
data/History.txt
CHANGED
data/Manifest.txt
CHANGED
@@ -7,9 +7,11 @@ README.rdoc
|
|
7
7
|
Rakefile
|
8
8
|
lib/event_emitter.rb
|
9
9
|
lib/event_emitter/emitter.rb
|
10
|
+
samples/instance-specific-method.rb
|
10
11
|
samples/sample.rb
|
11
12
|
script/console
|
12
13
|
script/destroy
|
13
14
|
script/generate
|
14
15
|
test/test_event_emitter.rb
|
15
16
|
test/test_helper.rb
|
17
|
+
test/test_singular_method.rb
|
data/README.rdoc
CHANGED
@@ -10,6 +10,12 @@
|
|
10
10
|
|
11
11
|
Ruby port of EventEmitter from Node.js
|
12
12
|
|
13
|
+
== REQUIREMENTS:
|
14
|
+
|
15
|
+
* Ruby 1.8.7+
|
16
|
+
* Ruby 1.9.3+
|
17
|
+
* JRuby 1.6.7+
|
18
|
+
|
13
19
|
== SYNOPSIS:
|
14
20
|
|
15
21
|
require "rubygems"
|
@@ -46,6 +52,15 @@ call
|
|
46
52
|
user.emit :eat, "BEEF" # => "shokai -> BEEF"
|
47
53
|
user.emit :eat, "Ramen" # => do not call. call only first time.
|
48
54
|
|
55
|
+
apply as instance-specific method
|
56
|
+
|
57
|
+
class Foo
|
58
|
+
end
|
59
|
+
|
60
|
+
foo = Foo.new
|
61
|
+
EventEmitter.apply foo
|
62
|
+
|
63
|
+
|
49
64
|
see samples https://github.com/shokai/event_emitter/tree/master/samples
|
50
65
|
|
51
66
|
== TEST:
|
data/lib/event_emitter.rb
CHANGED
@@ -0,0 +1,29 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
$:.unshift File.expand_path '../lib', File.dirname(__FILE__)
|
3
|
+
require 'event_emitter'
|
4
|
+
|
5
|
+
class User
|
6
|
+
attr_accessor :name
|
7
|
+
def initialize(name)
|
8
|
+
@name = name
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
shokai = User.new "shokai"
|
13
|
+
ymrl = User.new "ymrl"
|
14
|
+
EventEmitter.apply shokai ## set instance-specific method
|
15
|
+
|
16
|
+
shokai.on :go do |data|
|
17
|
+
puts "#{name} go to #{data[:place]}"
|
18
|
+
end
|
19
|
+
|
20
|
+
shokai.emit :go, :place => "chiba city"
|
21
|
+
|
22
|
+
## raise undefined-method error
|
23
|
+
begin
|
24
|
+
ymrl.on :go do |data|
|
25
|
+
puts "#{name} go to #{data[:place]}"
|
26
|
+
end
|
27
|
+
rescue => e
|
28
|
+
STDERR.puts e
|
29
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require File.expand_path 'test_helper', File.dirname(__FILE__)
|
2
|
+
|
3
|
+
class TestSingularMethod < MiniTest::Unit::TestCase
|
4
|
+
|
5
|
+
class Foo
|
6
|
+
attr_accessor :created_at
|
7
|
+
end
|
8
|
+
|
9
|
+
def setup
|
10
|
+
@foo = Foo.new
|
11
|
+
@foo.created_at = @now = Time.now
|
12
|
+
EventEmitter.apply @foo
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_on_emit
|
16
|
+
result = nil
|
17
|
+
__created_at = nil
|
18
|
+
@foo.on :chat do |data|
|
19
|
+
result = data
|
20
|
+
__created_at = created_at
|
21
|
+
end
|
22
|
+
|
23
|
+
@foo.emit :chat, :user => 'shokai', :message => 'hello world'
|
24
|
+
|
25
|
+
assert result[:user] == 'shokai'
|
26
|
+
assert result[:message] == 'hello world'
|
27
|
+
assert __created_at == @now, 'instance method'
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_add_listener
|
31
|
+
result = nil
|
32
|
+
__created_at = nil
|
33
|
+
@foo.add_listener :chat do |data|
|
34
|
+
result = data
|
35
|
+
__created_at = created_at
|
36
|
+
end
|
37
|
+
|
38
|
+
@foo.emit :chat, :user => 'shokai', :message => 'hello world'
|
39
|
+
|
40
|
+
assert result[:user] == 'shokai'
|
41
|
+
assert result[:message] == 'hello world'
|
42
|
+
assert __created_at == @now, 'instance method'
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_remove_listener
|
46
|
+
@foo.on :bar do |data|
|
47
|
+
puts "bar #{data}"
|
48
|
+
end
|
49
|
+
@foo.on :bar do |data|
|
50
|
+
puts "barbar: #{data}"
|
51
|
+
end
|
52
|
+
|
53
|
+
id = @foo.on :baz do |data|
|
54
|
+
p data
|
55
|
+
end
|
56
|
+
|
57
|
+
assert @foo.events.size == 3, 'check registerd listener count'
|
58
|
+
@foo.remove_listener id
|
59
|
+
assert @foo.events.size == 2, 'remove listener by id'
|
60
|
+
|
61
|
+
@foo.remove_listener :bar
|
62
|
+
assert @foo.events.size == 0, 'remove all "bar" listener'
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_once
|
66
|
+
total = 0
|
67
|
+
@foo.once :add do |data|
|
68
|
+
total += data
|
69
|
+
end
|
70
|
+
|
71
|
+
@foo.emit :add, 1
|
72
|
+
assert total == 1, 'first call'
|
73
|
+
@foo.emit :add, 1
|
74
|
+
assert total == 1, 'call listener only first time'
|
75
|
+
end
|
76
|
+
end
|
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.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -78,12 +78,14 @@ files:
|
|
78
78
|
- Rakefile
|
79
79
|
- lib/event_emitter.rb
|
80
80
|
- lib/event_emitter/emitter.rb
|
81
|
+
- samples/instance-specific-method.rb
|
81
82
|
- samples/sample.rb
|
82
83
|
- script/console
|
83
84
|
- script/destroy
|
84
85
|
- script/generate
|
85
86
|
- test/test_event_emitter.rb
|
86
87
|
- test/test_helper.rb
|
88
|
+
- test/test_singular_method.rb
|
87
89
|
- .gemtest
|
88
90
|
homepage: http://github.com/shokai/event_emitter
|
89
91
|
licenses: []
|
@@ -101,7 +103,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
101
103
|
version: '0'
|
102
104
|
segments:
|
103
105
|
- 0
|
104
|
-
hash:
|
106
|
+
hash: 3525957517936737541
|
105
107
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
108
|
none: false
|
107
109
|
requirements:
|
@@ -117,3 +119,4 @@ summary: Ruby port of EventEmitter from Node.js
|
|
117
119
|
test_files:
|
118
120
|
- test/test_event_emitter.rb
|
119
121
|
- test/test_helper.rb
|
122
|
+
- test/test_singular_method.rb
|