event_object 0.9.4 → 1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e81c485937d85b0c9cd88b13cac0567f4aa0e03f
4
- data.tar.gz: 7ce74e45fd9f99329ffb8db70cf1c1852027307a
3
+ metadata.gz: efb92fc3d9f1021bb0cadb829e71023c737f62be
4
+ data.tar.gz: 6fb27e337868fc9fce74306554abab0138db5ab0
5
5
  SHA512:
6
- metadata.gz: 76a40e1cdc625aea5a08916da033eb5cc600a2b0255f9f1d84914baf57263e0fa6b0beb2c61e641d80ade7469ab5c2027f5de70d25da3e6d1a1140868b4a56bf
7
- data.tar.gz: 2956ba950da83365cef180a7788e02b6eeb0ffa93003dcb7a8b97cc144bdbf21043c42728cb1011d80e97a78d7d471dd6a828d2e3c47632af6c21e554f65a1d7
6
+ metadata.gz: 2b605073d048a3bffae93301ef440af33ae94e3d3f81295e2755bb2b02490fef53a45823bbcf6d0257b7167a9431f4cc2ce3e2a4c1838a695604674c0b176a4b
7
+ data.tar.gz: 5e7ee0daed6aba4e270252a1c07e91b4154e825cdcd7f91b7d9523d373a149084a8b19e38c33997794d19c1d088f665d54c8487016ba79b2c5867be1a3d580da
@@ -1,22 +1,21 @@
1
- Copyright (c) 2014 che
1
+ The MIT License (MIT)
2
2
 
3
- MIT License
3
+ Copyright (c) 2014-2016 Maksim V. <inre.storm@gmail.com>
4
4
 
5
- Permission is hereby granted, free of charge, to any person obtaining
6
- a copy of this software and associated documentation files (the
7
- "Software"), to deal in the Software without restriction, including
8
- without limitation the rights to use, copy, modify, merge, publish,
9
- distribute, sublicense, and/or sell copies of the Software, and to
10
- permit persons to whom the Software is furnished to do so, subject to
11
- the following conditions:
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
12
11
 
13
- The above copyright notice and this permission notice shall be
14
- included in all copies or substantial portions of the Software.
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
15
14
 
16
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md CHANGED
@@ -2,33 +2,59 @@
2
2
 
3
3
  EventObject - event-driven programming for ruby
4
4
 
5
- ## Installation
5
+ Минималистичность кода, простота использования, нету излишнего кода...
6
+ Данных гем поможет обеспечить простой способ поддержики событий для руби объектов. Так же имеет
7
+ простой синтаксис иницилизации и вызова событий.
6
8
 
7
- Add this line to your application's Gemfile:
9
+ ## Array refines
8
10
 
9
- ```ruby
10
- gem 'event_object'
11
- ```
11
+ Событие представляет собой ruby array.
12
12
 
13
- And then execute:
13
+ Простой пример:
14
14
 
15
- $ bundle
15
+ ```ruby
16
+ using EventObject
17
+ event = [lambda { 'BEGIN' }, lambda { 'END' }]
18
+ p event.fire
19
+ # ["BEGIN", "END"]
20
+ ```
16
21
 
17
- Or install it yourself as:
22
+ В примере мы запустили все события в фиксированном контексте, однако мы можем изменить контекст на другой
23
+ с помощью метода instance_fire
18
24
 
19
- $ gem install event_object
25
+ ## Class refines
20
26
 
21
- ## Usage
27
+ Позволяют легко создавать события в объекте
22
28
 
23
29
  ```ruby
24
- using EventObject
25
30
  class EventEmitter
31
+ using Emmy
26
32
  events :success, :error
27
33
  end
34
+ ```
28
35
 
36
+ ## Object refines
37
+
38
+ ```ruby
29
39
  emitter = EventEmitter.new
30
40
  emitter.on :success do
31
41
  [200, {}, "OK"]
32
42
  end
33
43
  p emitter.success!
34
44
  ```
45
+
46
+ ## Installation
47
+
48
+ Add this line to your application's Gemfile:
49
+
50
+ ```ruby
51
+ gem 'event_object'
52
+ ```
53
+
54
+ And then execute:
55
+
56
+ $ bundle
57
+
58
+ Or install it yourself as:
59
+
60
+ $ gem install event_object
@@ -4,24 +4,24 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'event_object/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "event_object"
7
+ spec.name = 'event_object'
8
8
  spec.version = EventObject::VERSION
9
- spec.authors = ["che"]
10
- spec.email = ["chelovekov@gmail.com"]
11
- spec.summary = %q{EventObject - do ruby object to be event-driven }
12
- #spec.description = %q{TODO: Write a longer description. Optional.}
13
- spec.homepage = ""
14
- spec.license = "MIT"
9
+ spec.authors = ['Maksim V.']
10
+ spec.email = ['inre.storm@gmail.com']
11
+ spec.summary = %q{Event-driven programming for ruby}
12
+ spec.description = spec.summary
13
+ spec.homepage = 'https://github.com/inre/event_object'
14
+ spec.license = 'MIT'
15
15
 
16
- spec.required_ruby_version = '>= 2.0.0'
16
+ spec.required_ruby_version = '>= 2.1.0'
17
17
  spec.required_rubygems_version = '>= 2.3.0'
18
18
 
19
19
  spec.files = `git ls-files -z`.split("\x0")
20
20
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
21
21
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
22
- spec.require_paths = ["lib"]
22
+ spec.require_paths = ['lib']
23
23
 
24
- spec.add_development_dependency "bundler", "~> 1.6"
25
- spec.add_development_dependency "rake", "~> 10.0"
26
- spec.add_development_dependency "rspec", "~> 3.0.0"
24
+ spec.add_development_dependency 'bundler', "~> 1.6"
25
+ spec.add_development_dependency 'rake', "~> 10.0"
26
+ spec.add_development_dependency 'rspec', "~> 3.3.0"
27
27
  end
@@ -1,21 +1,41 @@
1
1
  module EventObject
2
2
  refine Array do
3
- # array like event
3
+ # Emit event with specified arguments and specified block.
4
+ #
5
+ # @example
6
+ # using EventObject
7
+ # event = [() -> 1, () -> 2]
8
+ # p event.fire
9
+ # # [1, 2]
10
+ #
11
+ # @param *a [...] arguments to be passed
12
+ # @param &b [Block] block to be passed
13
+ # @return [Array] the results of the call
4
14
  def fire(*a, &b)
5
- dup.collect { |h| h.call(*a, &b) }
15
+ dup.map { |h| h.call(*a, &b) }
6
16
  end
17
+ alias emit fire
7
18
 
8
- # run events consecutive
9
- def chain(a)
10
- dup.inject(a) { |a, c| c.call(a) }
19
+ # Emit event with specified arguments within the context. It's giving the each listener
20
+ # access to object intance variables. Unlike fire, the block is not allowed.
21
+ #
22
+ # @param context [Object] the context of the receiver
23
+ # @return [Array]
24
+ def instance_fire(context, *a)
25
+ dup.map { |h| context.instance_exec(*a, &h) }
11
26
  end
12
27
 
13
- def fire_for(t, *a)
14
- dup.collect { |h| t.instance_exec(*a, &h) }
15
- end
16
-
17
- def off(proc=nil)
18
- (proc) ? delete(proc) : clear
28
+ # Removes the specified listener from the array. If no arguments are supplied then
29
+ # all listeners will be removed.
30
+ #
31
+ # @example
32
+ # events = [() -> 1, () -> 2]
33
+ # events.off
34
+ #
35
+ # @param listener [Proc] listener or nil
36
+ # @return [Proc, Array] listener or nil or array of listeners
37
+ def off(listener=nil)
38
+ listener ? delete(listener) : clear
19
39
  end
20
40
  end
21
41
  end
@@ -1,21 +1,32 @@
1
1
  module EventObject
2
2
  refine Class do
3
- def def(met, &b)
4
- define_method met, &b
5
- end
6
-
7
- def cdef(met, &b)
8
- define_singleton_method met, &b
3
+ # Create events in specified class. For each event we create two methods:
4
+ # `event` and `event!`. `event` returns an array of event listeners. `event!` emits
5
+ # the listeners.
6
+ #
7
+ # @param [Array] the array of the symbols
8
+ # @return [Symbol]
9
+ def events(*ev)
10
+ ev.each { |name| self.def("#{name}!") { |*a, &b| var(name, Array).fire(*a, &b) } }
11
+ ev.map { |name| self.def(name) { var(name, Array) } }
9
12
  end
10
13
 
11
- def events(*ev)
12
- ev.each { |name| self.def(name) { var(name, Array) } }
13
- ev.each { |name| self.def("#{name}!") { |*a| var(name, Array).fire(*a) } }
14
+ # Define an instance method. `def` is public method.
15
+ #
16
+ # @param [Symbol]
17
+ # @return [Symbol]
18
+ def def(symbol, &b)
19
+ define_method symbol, &b
14
20
  end
15
21
 
16
- def var(name, init=nil)
17
- key = "@#{name}"
18
- class_variable_defined?(key) ? class_variable_get(key) : class_variable_set(key, (init.is_a?(Class) ? init.new : init))
22
+ # Get a class variable. If the variable is undefined then the variable will be initialized.
23
+ #
24
+ # @param symbol [Symbol] the name of the variable
25
+ # @param init [...] default value
26
+ # @return [...] the value
27
+ def var(symbol, init=nil)
28
+ class_variable_defined?(sym = "@#{symbol}") ? class_variable_get(sym) :
29
+ class_variable_set(sym, (init.is_a?(Class) ? init.new : init))
19
30
  end
20
31
  end
21
32
  end
@@ -1,11 +1,17 @@
1
1
  module EventObject
2
2
  refine NilClass do
3
+
4
+ # if event missing it does nothing
3
5
  def fire(*a)
4
6
  []
5
7
  end
6
8
 
7
- def fire_for(t, *a)
9
+ def instance_fire(t, *a)
8
10
  []
9
11
  end
12
+
13
+ def off(listener=nil)
14
+ nil
15
+ end
10
16
  end
11
17
  end
@@ -1,42 +1,91 @@
1
1
  module EventObject
2
2
  refine Object do
3
- def def(method, &b)
4
- define_singleton_method method, &b
5
- end
6
3
 
7
- def var(name, init=nil)
8
- instance_variable_get("@#{name}") || instance_variable_set("@#{name}", (init.is_a?(Class) ? init.new : init))
4
+ # Add the `listener` block for the specified `event`. `on` calls can be chained.
5
+ #
6
+ # @example
7
+ # class EventEmitter
8
+ # using Emmy
9
+ # events :success, :error
10
+ # end
11
+ # emitter = EventEmitter.new
12
+ # emitter.on :success do
13
+ # [200, {}, "OK"]
14
+ # end
15
+ # p emitter.success!
16
+ #
17
+ # @param event [Symbol]
18
+ # @param listener [Proc]
19
+ # @param block [Block]
20
+ # @return [Proc]
21
+ def on(event, listener=nil, &block)
22
+ var(event, Array).tap { |e| e << (listener || block) }; (listener || block)
9
23
  end
10
24
 
11
- def on(name, bb=nil, &b)
12
- var(name, Array).tap { |e| e << (bb || b) }; (bb || b)
25
+ # Add the `listener` block for the specified `event`. When event happened `listener` will be removed.
26
+ #
27
+ # @param event
28
+ # @param listener
29
+ # @param block
30
+ # @return [Proc]
31
+ def once(event, listener=nil, &block)
32
+ (ev = nil).tap { |ev|
33
+ var(event, Array).tap { |e| e << (ev = ->(*a) { (listener || block).call(*a); e.delete(ev) }) }
34
+ }
13
35
  end
14
36
 
37
+ # Remove events depending on the argument counts.
15
38
  #
16
- # obj.off(:ev)
17
- # obj.off(:ev, proc)
18
- # obj.off(:ev, target, :meth)
19
- def off(*a)
20
- case a.size
21
- when 1 then var(a[0], Array).clear
22
- when 2 then var(a[0], Array).tap { |e| e.delete a[1] }
23
- when 3 then var(a[0], Array).tap { |e| e.delete a[1].method(a[2]) }
39
+ # @example
40
+ # obj.off(:event) # Remove all event listensers for the specified `event`
41
+ # obj.off(:event, proc) # Remove an event listener
42
+ # obj.off(:ev, object, :meth)
43
+ # @param args [...]
44
+ # @return []
45
+ def off(*args)
46
+ case args.size
47
+ when 1 then var(args[0], Array).clear
48
+ when 2 then var(args[0], Array).delete(args[1])
49
+ when 3 then var(args[0], Array).delete(args[1].method(args[2]))
50
+ else
51
+ raise "too many arguments"
24
52
  end
25
53
  self
26
54
  end
27
55
 
28
- def once(name, &b)
29
- (ev = nil).tap { |ev|
30
- var(name, Array).tap { |e| e << (ev = ->() { b.call; e.delete(ev) }) }
31
- }
56
+ # Add the `instance` method with `name` like block for the specified event.
57
+ #
58
+ # @param instance [Object]
59
+ # @param event [Symbol]
60
+ # @param name [Symbol]
61
+ # @return []
62
+ def listen(instance, event, name)
63
+ instance.on event, method(name)
32
64
  end
33
65
 
34
- def listen(target, ev, meth)
35
- target.on ev, method(meth)
66
+ # Remove all listeners being methods of the `instance`.
67
+ #
68
+ # @param []
69
+ # @return []
70
+ def stop_listen(instance, event)
71
+ instance.var(event, Array).delete_if { |e| e.is_a?(Method) && e.receiver == self }
36
72
  end
37
73
 
38
- def stop_listen(target, ev)
39
- target.var(ev, Array).delete_if { |e| e.is_a?(Method) && e.receiver == self }
74
+ # Define an instance method. The method will be appeared only in this instance.
75
+ #
76
+ # @param symbol [Symbol]
77
+ # @return [Symbol]
78
+ def def(symbol, &b)
79
+ define_singleton_method symbol, &b
80
+ end
81
+
82
+ # Get an instance variable. If the variable is undefined then the variable will be initialized.
83
+ #
84
+ # @param symbol [Symbol]
85
+ # @param init [...]
86
+ # @return [Symbol]
87
+ def var(symbol, init=nil)
88
+ instance_variable_get(sym = "@#{symbol}") || instance_variable_set(sym, (init.is_a?(Class) ? init.new : init))
40
89
  end
41
90
  end
42
91
  end
@@ -1,3 +1,3 @@
1
1
  module EventObject
2
- VERSION = "0.9.4"
2
+ VERSION = '1.0'
3
3
  end
@@ -0,0 +1,57 @@
1
+ require "spec_helper"
2
+
3
+ describe EventObject do
4
+ using EventObject
5
+
6
+ it "creates an array of events" do
7
+ events = []
8
+ listener = lambda { :result }
9
+ events.push listener
10
+ events_with_arguments = []
11
+ events_with_arguments.push lambda { |num| num+1 }
12
+
13
+ expect(events.fire.last).to be(:result)
14
+ expect(events.instance_fire(self).last).to be(:result)
15
+ expect(events_with_arguments.fire(5).last).to be(6)
16
+
17
+ events.off(listener)
18
+ expect(events).to be_empty
19
+ end
20
+
21
+ it "fires NilClass methods" do
22
+ expect(nil.fire).to eq([])
23
+ expect(nil.instance_fire(self)).to eq([])
24
+ end
25
+
26
+ it "creates events and fires it" do
27
+ emitter = EventEmitter.new
28
+ emitter.on :success do
29
+ 18
30
+ end
31
+ emitter.on :success do
32
+ 42
33
+ end
34
+
35
+ expect(emitter.success!).to eq([18,42])
36
+ expect(emitter.success.fire).to eq([18,42])
37
+ expect(emitter.error).to be_empty
38
+ expect(emitter.error.fire).to be_empty
39
+ end
40
+
41
+ =begin
42
+ DEPRECATED
43
+ it "makes event chain" do
44
+ emitter = EventEmitter.new
45
+ emitter.on :data do |chunk|
46
+ chunk + " world"
47
+ end
48
+ emitter.on :data do |chunk|
49
+ chunk + "!!!"
50
+ end
51
+
52
+ data = emitter.data.chain('Hello')
53
+
54
+ expect(data).to eq('Hello world!!!')
55
+ end
56
+ =end
57
+ end
@@ -0,0 +1,16 @@
1
+ require "spec_helper"
2
+
3
+ describe EventObject do
4
+ using EventObject
5
+
6
+ it "defines the instance method for the specified class" do
7
+ expect {
8
+ klass = Class.new
9
+ klass.def :foo do
10
+ 1
11
+ end
12
+ obj = klass.new
13
+ expect(obj.foo).to be 1
14
+ }.to_not raise_error
15
+ end
16
+ end
@@ -0,0 +1,62 @@
1
+ require "spec_helper"
2
+
3
+ describe EventObject do
4
+ using EventObject
5
+
6
+ class EventEmitter
7
+ events :success, :error, :data
8
+ end
9
+
10
+ class ParentObject
11
+ def ok
12
+ 'OK'
13
+ end
14
+ end
15
+
16
+
17
+ # dup required in fires
18
+ it "adds event to event" do
19
+ emitter = EventEmitter.new
20
+ counter = 0
21
+ emitter.on :success do
22
+ counter += 1
23
+ emitter.on :success do
24
+ counter += 1
25
+ end
26
+ end
27
+
28
+ emitter.success!
29
+
30
+ expect(counter).to be 1
31
+ end
32
+
33
+ it "adds instance method like listener to an event" do
34
+ emitter = EventEmitter.new
35
+ obj = ParentObject.new
36
+ obj.listen(emitter, :success, :ok)
37
+ expect(emitter.success!.last).to eq 'OK'
38
+
39
+ emitter.off(:success, obj, :ok)
40
+ expect(emitter.success!).to be_empty
41
+ end
42
+
43
+ it "stops listen event" do
44
+ emitter = EventEmitter.new
45
+ obj = ParentObject.new
46
+ obj.listen(emitter, :success, :ok)
47
+ expect(emitter.success!.last).to eq 'OK'
48
+
49
+ obj.stop_listen(emitter, :success)
50
+ expect(emitter.success!).to be_empty
51
+ end
52
+
53
+ it "defines singleton method" do
54
+ expect {
55
+ object = Object.new
56
+ object.def :foo do
57
+ 1
58
+ end
59
+ expect(object.foo).to be 1
60
+ }.to_not raise_error
61
+ end
62
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: event_object
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.4
4
+ version: '1.0'
5
5
  platform: ruby
6
6
  authors:
7
- - che
7
+ - Maksim V.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-27 00:00:00.000000000 Z
11
+ date: 2016-11-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -44,17 +44,17 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: 3.0.0
47
+ version: 3.3.0
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: 3.0.0
55
- description:
54
+ version: 3.3.0
55
+ description: Event-driven programming for ruby
56
56
  email:
57
- - chelovekov@gmail.com
57
+ - inre.storm@gmail.com
58
58
  executables: []
59
59
  extensions: []
60
60
  extra_rdoc_files: []
@@ -71,9 +71,11 @@ files:
71
71
  - lib/event_object/nil_class.rb
72
72
  - lib/event_object/object.rb
73
73
  - lib/event_object/version.rb
74
- - spec/event_object_spec.rb
74
+ - spec/array_nil_spec.rb
75
+ - spec/class_spec.rb
76
+ - spec/object_spec.rb
75
77
  - spec/spec_helper.rb
76
- homepage: ''
78
+ homepage: https://github.com/inre/event_object
77
79
  licenses:
78
80
  - MIT
79
81
  metadata: {}
@@ -85,7 +87,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
85
87
  requirements:
86
88
  - - ">="
87
89
  - !ruby/object:Gem::Version
88
- version: 2.0.0
90
+ version: 2.1.0
89
91
  required_rubygems_version: !ruby/object:Gem::Requirement
90
92
  requirements:
91
93
  - - ">="
@@ -93,10 +95,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
95
  version: 2.3.0
94
96
  requirements: []
95
97
  rubyforge_project:
96
- rubygems_version: 2.4.3
98
+ rubygems_version: 2.5.1
97
99
  signing_key:
98
100
  specification_version: 4
99
- summary: EventObject - do ruby object to be event-driven
101
+ summary: Event-driven programming for ruby
100
102
  test_files:
101
- - spec/event_object_spec.rb
103
+ - spec/array_nil_spec.rb
104
+ - spec/class_spec.rb
105
+ - spec/object_spec.rb
102
106
  - spec/spec_helper.rb
@@ -1,110 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe EventObject do
4
- using EventObject
5
-
6
- class EventEmitter
7
- events :success, :error, :data
8
- end
9
-
10
- class ParentObject
11
- def ok
12
- 'OK'
13
- end
14
- end
15
-
16
- it "should create array of events" do
17
- events = []
18
- proc = lambda { :result }
19
- events.push proc
20
- events_with_arguments = []
21
- events_with_arguments.push lambda { |num| num+1 }
22
-
23
- expect(events.fire.last).to be(:result)
24
- expect(events.fire_for(self).last).to be(:result)
25
- expect(events_with_arguments.fire(5).last).to be(6)
26
-
27
- events.off(proc)
28
- expect(events).to be_empty
29
- end
30
-
31
- it "should NilClass has fire" do
32
- expect(nil.fire).to eq([])
33
- expect(nil.fire_for(self)).to eq([])
34
- end
35
-
36
- it "should create events and fire them" do
37
- emitter = EventEmitter.new
38
- emitter.on :success do
39
- 18
40
- end
41
- emitter.on :success do
42
- 42
43
- end
44
-
45
- expect(emitter.success!).to eq([18,42])
46
- expect(emitter.success.fire).to eq([18,42])
47
- expect(emitter.error).to be_empty
48
- expect(emitter.error.fire).to be_empty
49
- end
50
-
51
- # dup required in fires
52
- it "add event in event" do
53
- emitter = EventEmitter.new
54
- counter = 0
55
- emitter.on :success do
56
- counter += 1
57
- emitter.on :success do
58
- counter += 1
59
- end
60
- end
61
-
62
- emitter.success!
63
-
64
- expect(counter).to be 1
65
- end
66
-
67
- it "listen other object" do
68
- emitter = EventEmitter.new
69
- obj = ParentObject.new
70
- obj.listen(emitter, :success, :ok)
71
- expect(emitter.success!.last).to eq 'OK'
72
-
73
- emitter.off(:success, obj, :ok)
74
- expect(emitter.success!).to be_empty
75
- end
76
-
77
- it "stop listen" do
78
- emitter = EventEmitter.new
79
- obj = ParentObject.new
80
- obj.listen(emitter, :success, :ok)
81
- expect(emitter.success!.last).to eq 'OK'
82
-
83
- obj.stop_listen(emitter, :success)
84
- expect(emitter.success!).to be_empty
85
- end
86
-
87
- it "events chain" do
88
- emitter = EventEmitter.new
89
- emitter.on :data do |chunk|
90
- chunk + " world"
91
- end
92
- emitter.on :data do |chunk|
93
- chunk + "!!!"
94
- end
95
-
96
- data = emitter.data.chain('Hello')
97
-
98
- expect(data).to eq('Hello world!!!')
99
- end
100
-
101
- it "define singleton method" do
102
- expect {
103
- object = Object.new
104
- object.def :foo do
105
- 1
106
- end
107
- expect(object.foo).to be 1
108
- }.to_not raise_error
109
- end
110
- end