event_emitter 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,8 @@
1
+ === 0.2.0 2012-12-16
2
+
3
+ * remove_listener
4
+ * fix method-name duplication
5
+
1
6
  === 0.1.0 2012-11-15
2
7
 
3
8
  * support on/emit multiple arguments
data/README.rdoc CHANGED
@@ -30,6 +30,7 @@ include
30
30
  attr_accessor :name
31
31
  end
32
32
 
33
+
33
34
  regist event listener
34
35
 
35
36
  user = User.new
@@ -38,22 +39,26 @@ regist event listener
38
39
  puts "#{name} go to #{data[:place]}"
39
40
  end
40
41
 
42
+
41
43
  call event
42
44
 
43
45
  user.emit :go, {:place => "mountain"}
44
46
  # => "shokai go to mountain"
45
47
 
48
+
46
49
  regist event using "once"
47
50
 
48
51
  user.once :eat do |what, where|
49
52
  puts "#{name} -> eat #{what} at #{where}"
50
53
  end
51
54
 
55
+
52
56
  call
53
57
 
54
58
  user.emit :eat, "BEEF", "zanmai" # => "shokai -> eat BEEF at zanmai"
55
59
  user.emit :eat, "Ramen", "marutomo" # => do not call. call only first time.
56
60
 
61
+
57
62
  apply as instance-specific method
58
63
 
59
64
  class Foo
@@ -63,6 +68,12 @@ apply as instance-specific method
63
68
  EventEmitter.apply foo
64
69
 
65
70
 
71
+ remove event listener
72
+
73
+ user.remove_listener :go
74
+ user.remove_listener event_id
75
+
76
+
66
77
  see samples https://github.com/shokai/event_emitter/tree/master/samples
67
78
 
68
79
  == TEST:
@@ -13,14 +13,14 @@ module EventEmitter
13
13
  end
14
14
 
15
15
  module InstanceMethods
16
- def events
17
- @events ||= []
16
+ def __events
17
+ @__events ||= []
18
18
  end
19
19
 
20
20
  def add_listener(type, params={}, &block)
21
21
  raise ArgumentError, 'listener block not given' unless block_given?
22
- id = events.empty? ? 0 : events.last[:id]+1
23
- events << {
22
+ id = __events.empty? ? 0 : __events.last[:id]+1
23
+ __events << {
24
24
  :type => type.to_sym,
25
25
  :listener => block,
26
26
  :params => params,
@@ -33,18 +33,18 @@ module EventEmitter
33
33
 
34
34
  def remove_listener(id_or_type)
35
35
  if id_or_type.class == Fixnum
36
- events.delete_if do |e|
36
+ __events.delete_if do |e|
37
37
  e[:id] == id_or_type
38
38
  end
39
39
  elsif [String, Symbol].include? id_or_type.class
40
- events.delete_if do |e|
40
+ __events.delete_if do |e|
41
41
  e[:type] == id_or_type.to_sym
42
42
  end
43
43
  end
44
44
  end
45
45
 
46
46
  def emit(type, *data)
47
- events.each do |e|
47
+ __events.each do |e|
48
48
  if e[:type] == type.to_sym
49
49
  listener = e[:listener]
50
50
  remove_listener e[:id] if e[:params][:once]
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.1.0'
7
+ VERSION = '0.2.0'
8
8
  end
@@ -78,7 +78,7 @@ class TestClassMethod < MiniTest::Unit::TestCase
78
78
  end
79
79
 
80
80
  def test_remove_listener
81
- size = Foo.events.size
81
+ size = Foo.__events.size
82
82
 
83
83
  Foo.on :foo do |data|
84
84
  puts "bar #{data}"
@@ -91,12 +91,12 @@ class TestClassMethod < MiniTest::Unit::TestCase
91
91
  p data
92
92
  end
93
93
 
94
- assert Foo.events.size == size+3, 'check registerd listener count'
94
+ assert Foo.__events.size == size+3, 'check registerd listener count'
95
95
  Foo.remove_listener id
96
- assert Foo.events.size == size+2, 'remove listener by id'
96
+ assert Foo.__events.size == size+2, 'remove listener by id'
97
97
 
98
98
  Foo.remove_listener :foo
99
- assert Foo.events.size == size, 'remove all "foo" listener'
99
+ assert Foo.__events.size == size, 'remove all "foo" listener'
100
100
  end
101
101
 
102
102
  def test_once
@@ -85,12 +85,12 @@ class TestEventEmitter < MiniTest::Unit::TestCase
85
85
  p data
86
86
  end
87
87
 
88
- assert @foo.events.size == 3, 'check registerd listener count'
88
+ assert @foo.__events.size == 3, 'check registerd listener count'
89
89
  @foo.remove_listener id
90
- assert @foo.events.size == 2, 'remove listener by id'
90
+ assert @foo.__events.size == 2, 'remove listener by id'
91
91
 
92
92
  @foo.remove_listener :bar
93
- assert @foo.events.size == 0, 'remove all "bar" listener'
93
+ assert @foo.__events.size == 0, 'remove all "bar" listener'
94
94
  end
95
95
 
96
96
  def test_once
@@ -85,12 +85,12 @@ class TestSingularMethod < MiniTest::Unit::TestCase
85
85
  p data
86
86
  end
87
87
 
88
- assert @foo.events.size == 3, 'check registerd listener count'
88
+ assert @foo.__events.size == 3, 'check registerd listener count'
89
89
  @foo.remove_listener id
90
- assert @foo.events.size == 2, 'remove listener by id'
90
+ assert @foo.__events.size == 2, 'remove listener by id'
91
91
 
92
92
  @foo.remove_listener :bar
93
- assert @foo.events.size == 0, 'remove all "bar" listener'
93
+ assert @foo.__events.size == 0, 'remove all "bar" listener'
94
94
  end
95
95
 
96
96
  def test_once
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.1.0
4
+ version: 0.2.0
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: 2012-11-14 00:00:00.000000000 Z
12
+ date: 2012-12-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rdoc
@@ -106,7 +106,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
106
106
  version: '0'
107
107
  segments:
108
108
  - 0
109
- hash: -4020786743775049336
109
+ hash: -1579719327575037681
110
110
  required_rubygems_version: !ruby/object:Gem::Requirement
111
111
  none: false
112
112
  requirements: