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 CHANGED
@@ -1,5 +1,6 @@
1
1
  .DS_Store
2
2
  *~
3
3
  pkg/*
4
+ tmp/*
4
5
  .\#*
5
6
  *\#*
data/History.txt CHANGED
@@ -1,6 +1,11 @@
1
+ === 0.0.3 2012-11-08
2
+
3
+ * call listener with Object#instance_exec
4
+
1
5
  === 0.0.2 2012-11-07
2
6
 
3
- * add function "once"
7
+ * EventEmitter#once : call listener only first time
8
+ * EventEmitter#add_listener, remove_listener
4
9
 
5
10
  === 0.0.1 2012-11-07
6
11
 
data/README.rdoc CHANGED
@@ -12,36 +12,39 @@ Ruby port of EventEmitter from Node.js
12
12
 
13
13
  == SYNOPSIS:
14
14
 
15
- require 'event_emitter'
15
+ require "rubygems"
16
+ require "event_emitter"
16
17
 
17
18
  include
18
19
 
19
- class Foo
20
+ class User
20
21
  include EventEmitter
22
+ attr_accessor :name
21
23
  end
22
24
 
23
25
  regist event listener
24
26
 
25
- foo = Foo.new
26
- foo.on :chat do |data|
27
- puts "chat - #{data[:user]} : #{data[:message]}"
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
- foo.emit :chat, {:user => 'shokai', :message => 'hello!!'}
33
- # => chat - shokai : hello!!
35
+ user.emit :go, {:place => "mountain"}
36
+ # => "shokai go to mountain"
34
37
 
35
38
  regist event using "once"
36
39
 
37
- foo.once :bar do |data|
38
- puts data
40
+ user.once :eat do |data|
41
+ puts "#{name} -> #{data}"
39
42
  end
40
43
 
41
44
  call
42
45
 
43
- foo.emit :bar, 100 # => call only first time
44
- foo.emit :bar, 200 # => not call
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.call data
47
+ instance_exec(data, &listener)
48
48
  end
49
49
  end
50
50
  end
data/lib/event_emitter.rb CHANGED
@@ -4,5 +4,5 @@ $:.unshift(File.dirname(__FILE__)) unless
4
4
  require 'event_emitter/emitter'
5
5
 
6
6
  module EventEmitter
7
- VERSION = '0.0.2'
7
+ VERSION = '0.0.3'
8
8
  end
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 Foo
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
- foo = Foo.new
14
+ shokai = User.new 'shokai'
15
+ ymrl = User.new 'ymrl'
10
16
 
11
- foo.on 'chat' do |data|
12
- puts "chat - #{data[:user]} : #{data[:message]}"
17
+ shokai.on :go do |data|
18
+ puts "#{name} go to #{data[:place]}"
13
19
  end
14
-
15
- foo.on :sum do |data|
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
- foo.once :bar do |data|
22
- puts "hello #{data}"
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
- foo.emit :sum, [1,2,3,45]
28
+ shokai.once :eat do |data|
29
+ puts "#{name} -> #{data}"
30
+ end
29
31
 
30
- foo.emit :bar, 'shokai'
31
- foo.emit :bar, 'shooooookai' # not call
32
+ shokai.emit :eat, 'BEEF'
33
+ shokai.emit :eat, 'Ramen' # do not call. call only first time if regist with "once"
@@ -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.2
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: 1154001054108273116
104
+ hash: -3803721958233588202
105
105
  required_rubygems_version: !ruby/object:Gem::Requirement
106
106
  none: false
107
107
  requirements: