flirt 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/flirt/callback.rb +4 -0
- data/lib/flirt/version.rb +1 -1
- data/lib/flirt.rb +25 -5
- data/spec/flirt/flirt_callback_spec.rb +6 -1
- data/spec/flirt/flirt_listener_spec.rb +36 -16
- data/spec/flirt/flirt_spec.rb +72 -6
- metadata +2 -8
- data/spec/flirt/flirt_test_classes.rb +0 -45
- data/spec/flirt/test_class_listener.rb +0 -9
- data/spec/flirt/test_class_subscriber.rb +0 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e91741f80d191782e97feb4f999ea217cea73ab0
|
4
|
+
data.tar.gz: c82e4ca1147147b4dbe58eceda66cb1f24724e9c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 663901d78250f3486d9deee98b17a299032270f93bbaa8c833a7838d597dbef8d5b5ee57565ee9417ef1e0292530504d75a0e28e4b72c8c2f78a473ae5dcb635
|
7
|
+
data.tar.gz: e9176cd92e3178a0ee7d291dab6fbf97956a4ca24aa39af78debd4b3ac96202148de7573fe90c41b619d778990ece3d79bfb19e69b33c466d7a6f34b1773cda6
|
data/lib/flirt/callback.rb
CHANGED
data/lib/flirt/version.rb
CHANGED
data/lib/flirt.rb
CHANGED
@@ -8,25 +8,31 @@ module Flirt
|
|
8
8
|
|
9
9
|
class << self
|
10
10
|
|
11
|
-
def
|
11
|
+
def publish(event_name, event_data)
|
12
12
|
return if disabled
|
13
13
|
raise ArgumentError.new("Event name must be a symbol") unless event_name.is_a? Symbol
|
14
14
|
(callbacks[event_name] || []).each do |callback|
|
15
15
|
callback.call(event_data)
|
16
16
|
end
|
17
17
|
end
|
18
|
-
alias_method :
|
18
|
+
alias_method :broadcast, :publish
|
19
19
|
|
20
20
|
def subscribe(object, event_name, options = {})
|
21
|
-
|
22
|
-
raise ArgumentError.new("You must pass an object") if object.nil?
|
23
|
-
raise ArgumentError.new("You must pass an event name") unless event_name.is_a? Symbol
|
21
|
+
check_subscription_arguments(event_name, object, options)
|
24
22
|
callback = Flirt::Callback.new object: object,
|
25
23
|
callback_name: options[:with]
|
26
24
|
add_callback(event_name, callback)
|
27
25
|
end
|
28
26
|
alias_method :listen, :subscribe
|
29
27
|
|
28
|
+
def unsubscribe(object, event_name, options = {})
|
29
|
+
check_subscription_arguments(event_name, object, options)
|
30
|
+
callback = Flirt::Callback.new object: object,
|
31
|
+
callback_name: options[:with]
|
32
|
+
remove_callback(event_name, callback)
|
33
|
+
end
|
34
|
+
alias_method :unlisten, :unsubscribe
|
35
|
+
|
30
36
|
def enable
|
31
37
|
self.disabled = false
|
32
38
|
end
|
@@ -53,6 +59,20 @@ module Flirt
|
|
53
59
|
callbacks[event_name] << callback
|
54
60
|
end
|
55
61
|
|
62
|
+
def remove_callback(event_name, callback_to_delete)
|
63
|
+
return unless callbacks[event_name]
|
64
|
+
|
65
|
+
callbacks[event_name].each do |callback|
|
66
|
+
callbacks[event_name].delete(callback) if callback == callback_to_delete
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def check_subscription_arguments(event_name, object, options)
|
71
|
+
raise ArgumentError.new("You must pass a callback") unless options[:with].is_a? Symbol
|
72
|
+
raise ArgumentError.new("You must pass an object") if object.nil?
|
73
|
+
raise ArgumentError.new("You must pass an event name") unless event_name.is_a? Symbol
|
74
|
+
end
|
75
|
+
|
56
76
|
end
|
57
77
|
|
58
78
|
end
|
@@ -5,7 +5,7 @@ describe Flirt::Callback do
|
|
5
5
|
Flirt.enable
|
6
6
|
end
|
7
7
|
|
8
|
-
describe "when instantiated with an object and callback" do
|
8
|
+
describe "when instantiated with an object and callback name" do
|
9
9
|
|
10
10
|
let(:listener) { Object.new }
|
11
11
|
let(:callback_name) { :call_me }
|
@@ -29,5 +29,10 @@ describe Flirt::Callback do
|
|
29
29
|
callback.call(event_data)
|
30
30
|
end
|
31
31
|
end
|
32
|
+
|
33
|
+
it "tests equality if object and callback are the same" do
|
34
|
+
other_callback = Flirt::Callback.new object: listener, callback_name: callback_name
|
35
|
+
expect(callback).to eq(other_callback)
|
36
|
+
end
|
32
37
|
end
|
33
38
|
end
|
@@ -1,5 +1,5 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
-
require 'flirt/flirt_test_classes'
|
2
|
+
#require 'flirt/flirt_test_classes'
|
3
3
|
|
4
4
|
describe Flirt::Listener do
|
5
5
|
before(:each) do
|
@@ -10,16 +10,32 @@ describe Flirt::Listener do
|
|
10
10
|
let(:event) { :grabbed_coin }
|
11
11
|
let(:callback_name) { :increase_score }
|
12
12
|
let(:event_data) { { value: 100 } }
|
13
|
-
let(:
|
14
|
-
let(:
|
13
|
+
let(:test_class) { Class.new }
|
14
|
+
let(:test_instance) { test_class.new }
|
15
|
+
|
16
|
+
before(:each) do
|
17
|
+
test_class.class_eval do
|
18
|
+
include Flirt::Listener
|
19
|
+
end
|
20
|
+
end
|
15
21
|
|
16
22
|
it "subscribes to an event" do
|
17
|
-
|
23
|
+
block_event = event
|
24
|
+
block_callback_name = callback_name
|
25
|
+
test_instance.instance_eval do
|
26
|
+
subscribe_to block_event, with: block_callback_name
|
27
|
+
end
|
28
|
+
expect(test_instance).to receive(callback_name).with(event_data)
|
18
29
|
Flirt.broadcast event, event_data
|
19
30
|
end
|
20
31
|
|
21
32
|
it "listens to an event" do
|
22
|
-
|
33
|
+
block_event = event
|
34
|
+
block_callback_name = callback_name
|
35
|
+
test_instance.instance_eval do
|
36
|
+
listen_to block_event, with: block_callback_name
|
37
|
+
end
|
38
|
+
expect(test_instance).to receive(callback_name).with(event_data)
|
23
39
|
Flirt.broadcast event, event_data
|
24
40
|
end
|
25
41
|
end
|
@@ -28,22 +44,26 @@ describe Flirt::Listener do
|
|
28
44
|
let(:event) { :event_name }
|
29
45
|
let(:callback_name) { :callback_name }
|
30
46
|
let(:event_data) { { value: 100 } }
|
31
|
-
let(:
|
32
|
-
let(:class_listener) { TestClassListener }
|
33
|
-
|
34
|
-
# Because rspec has to clear the Flirt callbacks for each test,
|
35
|
-
# we need to require the test classes after rspec init.
|
47
|
+
let(:test_class) { Class.new }
|
36
48
|
|
37
49
|
it "subscribes to an event" do
|
38
|
-
|
39
|
-
|
40
|
-
Flirt.
|
50
|
+
block_event = event
|
51
|
+
block_callback_name = callback_name
|
52
|
+
expect(Flirt).to receive(:subscribe).with(test_class, event, { with: callback_name })
|
53
|
+
test_class.class_eval do
|
54
|
+
extend Flirt::Listener
|
55
|
+
subscribe_to block_event, with: block_callback_name
|
56
|
+
end
|
41
57
|
end
|
42
58
|
|
43
59
|
it "listens to an event" do
|
44
|
-
|
45
|
-
|
46
|
-
Flirt.
|
60
|
+
block_event = event
|
61
|
+
block_callback_name = callback_name
|
62
|
+
expect(Flirt).to receive(:subscribe).with(test_class, event, { with: callback_name })
|
63
|
+
test_class.class_eval do
|
64
|
+
extend Flirt::Listener
|
65
|
+
listen_to block_event, with: block_callback_name
|
66
|
+
end
|
47
67
|
end
|
48
68
|
end
|
49
69
|
end
|
data/spec/flirt/flirt_spec.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
-
require 'flirt/flirt_test_classes'
|
2
|
+
#require 'flirt/flirt_test_classes'
|
3
3
|
|
4
4
|
describe Flirt do
|
5
5
|
|
@@ -8,26 +8,92 @@ describe Flirt do
|
|
8
8
|
let(:response) { { topping: "cream" } }
|
9
9
|
let(:event) { :pancake_fried }
|
10
10
|
let(:wrong_event) { :spud_baked }
|
11
|
-
let
|
11
|
+
let(:callback) { :respond }
|
12
|
+
let!(:listener) { Object.new }
|
13
|
+
|
14
|
+
before(:each) do
|
15
|
+
Flirt.listen listener, event, with: callback
|
16
|
+
end
|
12
17
|
|
13
18
|
it "listens to the correct broadcast event" do
|
19
|
+
expect(listener).to receive(callback).with(response)
|
14
20
|
Flirt.broadcast event, response
|
15
|
-
expect(listener.responded).to eq(response)
|
16
21
|
end
|
17
22
|
|
18
23
|
it "listens to the correct publish event" do
|
24
|
+
expect(listener).to receive(callback).with(response)
|
19
25
|
Flirt.publish event, response
|
20
|
-
expect(listener.responded).to eq(response)
|
21
26
|
end
|
22
27
|
|
23
28
|
it "doesn't listen to the wrong broadcast event" do
|
29
|
+
expect(listener).not_to receive(callback)
|
24
30
|
Flirt.broadcast wrong_event, response
|
25
|
-
expect(listener.responded).to be_nil
|
26
31
|
end
|
27
32
|
|
28
33
|
it "doesn't listen to the wrong publish event" do
|
34
|
+
expect(listener).not_to receive(callback)
|
29
35
|
Flirt.broadcast wrong_event, response
|
30
|
-
|
36
|
+
end
|
37
|
+
|
38
|
+
describe "when disabled" do
|
39
|
+
before(:each) { Flirt.disable }
|
40
|
+
after(:each) { Flirt.enable }
|
41
|
+
|
42
|
+
it "doesn't broadcast" do
|
43
|
+
expect(listener).not_to receive(callback)
|
44
|
+
Flirt.broadcast event, response
|
45
|
+
end
|
46
|
+
|
47
|
+
it "doesn't publish" do
|
48
|
+
expect(listener).not_to receive(callback)
|
49
|
+
Flirt.publish event, response
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe "when cleared" do
|
54
|
+
|
55
|
+
let(:callback2) { :respond2 }
|
56
|
+
|
57
|
+
before(:each) do
|
58
|
+
Flirt.listen listener, event, with: callback2
|
59
|
+
Flirt.clear
|
60
|
+
end
|
61
|
+
|
62
|
+
it "forgets listeners" do
|
63
|
+
expect(listener).not_to receive(callback)
|
64
|
+
expect(listener).not_to receive(callback2)
|
65
|
+
Flirt.publish event, response
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
|
70
|
+
describe "when unlistened" do
|
71
|
+
|
72
|
+
let(:callback2) { :respond2 }
|
73
|
+
|
74
|
+
before(:each) do
|
75
|
+
Flirt.listen listener, event, with: callback2
|
76
|
+
Flirt.unlisten listener, event, with: callback
|
77
|
+
end
|
78
|
+
|
79
|
+
it "forgets the listener" do
|
80
|
+
expect(listener).not_to receive(callback)
|
81
|
+
expect(listener).to receive(callback2).with(response)
|
82
|
+
Flirt.broadcast event, response
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
describe "when unsubscribed" do
|
88
|
+
before(:each) do
|
89
|
+
Flirt.unsubscribe listener, event, with: callback
|
90
|
+
end
|
91
|
+
|
92
|
+
it "forgets the listener" do
|
93
|
+
expect(listener).not_to receive(callback).with(response)
|
94
|
+
Flirt.publish event, response
|
95
|
+
end
|
96
|
+
|
31
97
|
end
|
32
98
|
end
|
33
99
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: flirt
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Benjamin Randles-Dunkley
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-03-
|
11
|
+
date: 2015-03-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -74,9 +74,6 @@ files:
|
|
74
74
|
- spec/flirt/flirt_callback_spec.rb
|
75
75
|
- spec/flirt/flirt_listener_spec.rb
|
76
76
|
- spec/flirt/flirt_spec.rb
|
77
|
-
- spec/flirt/flirt_test_classes.rb
|
78
|
-
- spec/flirt/test_class_listener.rb
|
79
|
-
- spec/flirt/test_class_subscriber.rb
|
80
77
|
- spec/spec_helper.rb
|
81
78
|
homepage: https://github.com/chemica/flirt
|
82
79
|
licenses:
|
@@ -106,7 +103,4 @@ test_files:
|
|
106
103
|
- spec/flirt/flirt_callback_spec.rb
|
107
104
|
- spec/flirt/flirt_listener_spec.rb
|
108
105
|
- spec/flirt/flirt_spec.rb
|
109
|
-
- spec/flirt/flirt_test_classes.rb
|
110
|
-
- spec/flirt/test_class_listener.rb
|
111
|
-
- spec/flirt/test_class_subscriber.rb
|
112
106
|
- spec/spec_helper.rb
|
@@ -1,45 +0,0 @@
|
|
1
|
-
|
2
|
-
class TestSubscriber
|
3
|
-
|
4
|
-
attr_accessor :responded
|
5
|
-
|
6
|
-
def initialize(event)
|
7
|
-
Flirt.subscribe self, event, with: :respond
|
8
|
-
end
|
9
|
-
|
10
|
-
def respond(event_data)
|
11
|
-
self.responded = event_data
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
|
16
|
-
class TestListener
|
17
|
-
|
18
|
-
attr_accessor :responded
|
19
|
-
|
20
|
-
def initialize(event)
|
21
|
-
Flirt.listen self, event, with: :respond
|
22
|
-
end
|
23
|
-
|
24
|
-
def respond(event_data)
|
25
|
-
self.responded = event_data
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
class TestInstanceSubscriber
|
30
|
-
include Flirt::Listener
|
31
|
-
|
32
|
-
def initialize(event, callback)
|
33
|
-
subscribe_to event, with: callback
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
class TestInstanceListener
|
38
|
-
include Flirt::Listener
|
39
|
-
|
40
|
-
attr_accessor :responded
|
41
|
-
|
42
|
-
def initialize(event, callback)
|
43
|
-
listen_to event, with: callback
|
44
|
-
end
|
45
|
-
end
|