flirt 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/lib/flirt/callback.rb +5 -0
- data/lib/flirt/listener.rb +8 -0
- data/lib/flirt/version.rb +1 -1
- data/lib/flirt.rb +13 -3
- data/spec/flirt/flirt_callback_spec.rb +9 -0
- data/spec/flirt/flirt_listener_spec.rb +61 -28
- data/spec/flirt/flirt_spec.rb +69 -62
- data/spec/spec_helper.rb +3 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3595d41a6ad627adc7eefdbb93242a03522ff4e5
|
4
|
+
data.tar.gz: d2a89c116bf434fe75827ca5c85ae3fcfe6958de
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1c42aba9604a42b718bca09e68d7ade30b6dbdfc0d10e9114ca658762bc3e72485b27f0c0740227e7621aac39c907446a3fd67ba7c97641ab78532364d9d77d6
|
7
|
+
data.tar.gz: 55d2e892fd50a3694b5c13da4c5d8f4e32da0839d9fbec8b39670c536fee612995d08c423c2b8c171fa6857ccac1280bad2f0a87bf3c37e630281c0a1b5970e9
|
data/.gitignore
CHANGED
data/lib/flirt/callback.rb
CHANGED
@@ -3,18 +3,23 @@
|
|
3
3
|
|
4
4
|
module Flirt
|
5
5
|
class Callback
|
6
|
+
|
6
7
|
attr_accessor :object, :callback_name
|
8
|
+
|
7
9
|
def initialize(opts = {})
|
8
10
|
self.callback_name = opts.fetch(:callback_name)
|
9
11
|
self.object = opts.fetch(:object)
|
10
12
|
end
|
11
13
|
|
14
|
+
|
12
15
|
def call(event_data)
|
13
16
|
object.send callback_name, event_data
|
14
17
|
end
|
15
18
|
|
19
|
+
|
16
20
|
def ==(other_callback)
|
17
21
|
object == other_callback.object && callback_name == other_callback.callback_name
|
18
22
|
end
|
23
|
+
|
19
24
|
end
|
20
25
|
end
|
data/lib/flirt/listener.rb
CHANGED
@@ -38,5 +38,13 @@ module Flirt
|
|
38
38
|
Flirt.subscribe self, event_name, opts
|
39
39
|
end
|
40
40
|
alias_method :listen_to, :subscribe_to
|
41
|
+
|
42
|
+
|
43
|
+
def unsubscribe_from(event_name, opts = {})
|
44
|
+
raise ArgumentError.new("You must pass a callback") unless opts[:with].is_a? Symbol
|
45
|
+
Flirt.unsubscribe self, event_name, opts
|
46
|
+
end
|
47
|
+
alias_method :forget, :unsubscribe_from
|
48
|
+
|
41
49
|
end
|
42
50
|
end
|
data/lib/flirt/version.rb
CHANGED
data/lib/flirt.rb
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
require "flirt/version"
|
2
2
|
require "flirt/callback"
|
3
3
|
require "flirt/listener"
|
4
|
-
|
5
|
-
#
|
4
|
+
|
5
|
+
# The Flirt module provides the main interface for dealing with Flirt. Callbacks can be registered or unregistered
|
6
|
+
# against events, and Flirt can be enabled or disabled.
|
6
7
|
|
7
8
|
module Flirt
|
8
9
|
|
@@ -17,6 +18,7 @@ module Flirt
|
|
17
18
|
end
|
18
19
|
alias_method :broadcast, :publish
|
19
20
|
|
21
|
+
|
20
22
|
def subscribe(object, event_name, options = {})
|
21
23
|
check_subscription_arguments(event_name, object, options)
|
22
24
|
callback = Flirt::Callback.new object: object,
|
@@ -25,6 +27,7 @@ module Flirt
|
|
25
27
|
end
|
26
28
|
alias_method :listen, :subscribe
|
27
29
|
|
30
|
+
|
28
31
|
def unsubscribe(object, event_name, options = {})
|
29
32
|
check_subscription_arguments(event_name, object, options)
|
30
33
|
callback = Flirt::Callback.new object: object,
|
@@ -33,32 +36,39 @@ module Flirt
|
|
33
36
|
end
|
34
37
|
alias_method :unlisten, :unsubscribe
|
35
38
|
|
39
|
+
|
36
40
|
def enable
|
37
41
|
self.disabled = false
|
38
42
|
end
|
39
43
|
|
44
|
+
|
40
45
|
def disable
|
41
46
|
self.disabled = true
|
42
47
|
end
|
43
48
|
|
49
|
+
|
44
50
|
def clear
|
45
51
|
@callbacks = {}
|
46
52
|
end
|
47
53
|
|
54
|
+
|
48
55
|
private
|
49
56
|
|
50
57
|
attr_reader :callbacks
|
51
58
|
attr_accessor :disabled
|
52
59
|
|
60
|
+
|
53
61
|
def callbacks
|
54
62
|
@callbacks ||= {}
|
55
63
|
end
|
56
64
|
|
65
|
+
|
57
66
|
def add_callback(event_name, callback)
|
58
67
|
callbacks[event_name] ||= []
|
59
68
|
callbacks[event_name] << callback
|
60
69
|
end
|
61
70
|
|
71
|
+
|
62
72
|
def remove_callback(event_name, callback_to_delete)
|
63
73
|
return unless callbacks[event_name]
|
64
74
|
|
@@ -67,6 +77,7 @@ module Flirt
|
|
67
77
|
end
|
68
78
|
end
|
69
79
|
|
80
|
+
|
70
81
|
def check_subscription_arguments(event_name, object, options)
|
71
82
|
raise ArgumentError.new("You must pass a callback") unless options[:with].is_a? Symbol
|
72
83
|
raise ArgumentError.new("You must pass an object") if object.nil?
|
@@ -74,5 +85,4 @@ module Flirt
|
|
74
85
|
end
|
75
86
|
|
76
87
|
end
|
77
|
-
|
78
88
|
end
|
@@ -8,31 +8,40 @@ describe Flirt::Callback do
|
|
8
8
|
describe "when instantiated with an object and callback name" do
|
9
9
|
|
10
10
|
let(:listener) { Object.new }
|
11
|
+
|
11
12
|
let(:callback_name) { :call_me }
|
12
13
|
|
13
14
|
let(:callback) { Flirt::Callback.new object: listener, callback_name: callback_name }
|
14
15
|
|
16
|
+
|
15
17
|
it "sets the object" do
|
16
18
|
expect(callback.object).to eq(listener)
|
17
19
|
end
|
18
20
|
|
21
|
+
|
19
22
|
it "sets the callback" do
|
20
23
|
expect(callback.callback_name).to eq(callback_name)
|
21
24
|
end
|
22
25
|
|
26
|
+
|
23
27
|
describe "when called" do
|
24
28
|
|
25
29
|
let(:event_data) { {event: :data} }
|
26
30
|
|
31
|
+
|
27
32
|
it "calls the callback with the event data" do
|
28
33
|
expect(listener).to receive(callback_name).with(event_data)
|
29
34
|
callback.call(event_data)
|
30
35
|
end
|
36
|
+
|
31
37
|
end
|
32
38
|
|
39
|
+
|
33
40
|
it "tests equality if object and callback are the same" do
|
34
41
|
other_callback = Flirt::Callback.new object: listener, callback_name: callback_name
|
35
42
|
expect(callback).to eq(other_callback)
|
36
43
|
end
|
44
|
+
|
37
45
|
end
|
46
|
+
|
38
47
|
end
|
@@ -7,63 +7,96 @@ describe Flirt::Listener do
|
|
7
7
|
end
|
8
8
|
|
9
9
|
describe "when included in an instance" do
|
10
|
+
|
10
11
|
let(:event) { :grabbed_coin }
|
12
|
+
|
11
13
|
let(:callback_name) { :increase_score }
|
14
|
+
|
12
15
|
let(:event_data) { { value: 100 } }
|
16
|
+
|
13
17
|
let(:test_class) { Class.new }
|
18
|
+
|
14
19
|
let(:test_instance) { test_class.new }
|
15
20
|
|
21
|
+
|
16
22
|
before(:each) do
|
17
23
|
test_class.class_eval do
|
18
24
|
include Flirt::Listener
|
19
25
|
end
|
26
|
+
|
20
27
|
end
|
21
28
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
29
|
+
|
30
|
+
[:listen_to, :subscribe_to].each do |method|
|
31
|
+
it "subscribes to an event with ##{method}" do
|
32
|
+
block_event = event
|
33
|
+
block_callback_name = callback_name
|
34
|
+
expect(Flirt).to receive(:subscribe).with(test_instance, event, { with: callback_name })
|
35
|
+
test_instance.instance_eval do
|
36
|
+
send method, block_event, with: block_callback_name
|
37
|
+
end
|
38
|
+
|
27
39
|
end
|
28
|
-
|
29
|
-
Flirt.broadcast event, event_data
|
40
|
+
|
30
41
|
end
|
31
42
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
43
|
+
|
44
|
+
[:forget, :unsubscribe_from].each do |method|
|
45
|
+
it "unsubscribes from an event with ##{method}" do
|
46
|
+
block_event = event
|
47
|
+
block_callback_name = callback_name
|
48
|
+
expect(Flirt).to receive(:unsubscribe).with(test_instance, event, { with: callback_name })
|
49
|
+
test_instance.instance_eval do
|
50
|
+
send method, block_event, with: block_callback_name
|
51
|
+
end
|
52
|
+
|
37
53
|
end
|
38
|
-
|
39
|
-
Flirt.broadcast event, event_data
|
54
|
+
|
40
55
|
end
|
56
|
+
|
41
57
|
end
|
42
58
|
|
59
|
+
|
43
60
|
describe "when included in a class" do
|
61
|
+
|
44
62
|
let(:event) { :event_name }
|
63
|
+
|
45
64
|
let(:callback_name) { :callback_name }
|
65
|
+
|
46
66
|
let(:event_data) { { value: 100 } }
|
67
|
+
|
47
68
|
let(:test_class) { Class.new }
|
48
69
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
70
|
+
|
71
|
+
[:listen_to, :subscribe_to].each do |method|
|
72
|
+
it "subscribes to an event with ##{method}" do
|
73
|
+
block_event = event
|
74
|
+
block_callback_name = callback_name
|
75
|
+
expect(Flirt).to receive(:subscribe).with(test_class, event, { with: callback_name })
|
76
|
+
test_class.class_eval do
|
77
|
+
extend Flirt::Listener
|
78
|
+
send method, block_event, with: block_callback_name
|
79
|
+
end
|
80
|
+
|
56
81
|
end
|
82
|
+
|
57
83
|
end
|
58
84
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
85
|
+
|
86
|
+
[:forget, :unsubscribe_from].each do |method|
|
87
|
+
it "unsubscribes from an event with ##{method}" do
|
88
|
+
block_event = event
|
89
|
+
block_callback_name = callback_name
|
90
|
+
expect(Flirt).to receive(:unsubscribe).with(test_class, event, { with: callback_name })
|
91
|
+
test_class.class_eval do
|
92
|
+
extend Flirt::Listener
|
93
|
+
send method, block_event, with: block_callback_name
|
94
|
+
end
|
95
|
+
|
66
96
|
end
|
97
|
+
|
67
98
|
end
|
99
|
+
|
68
100
|
end
|
101
|
+
|
69
102
|
end
|
data/spec/flirt/flirt_spec.rb
CHANGED
@@ -6,94 +6,101 @@ describe Flirt do
|
|
6
6
|
describe "with a single :pancake_fried subscriber" do
|
7
7
|
|
8
8
|
let(:response) { { topping: "cream" } }
|
9
|
+
|
9
10
|
let(:event) { :pancake_fried }
|
11
|
+
|
10
12
|
let(:wrong_event) { :spud_baked }
|
13
|
+
|
11
14
|
let(:callback) { :respond }
|
15
|
+
|
12
16
|
let!(:listener) { Object.new }
|
13
17
|
|
14
|
-
before(:each) do
|
15
|
-
Flirt.listen listener, event, with: callback
|
16
|
-
end
|
17
18
|
|
18
|
-
|
19
|
-
expect(listener).to receive(callback).with(response)
|
20
|
-
Flirt.broadcast event, response
|
21
|
-
end
|
19
|
+
[:subscribe, :listen].each do |method|
|
22
20
|
|
23
|
-
|
24
|
-
expect(listener).to receive(callback).with(response)
|
25
|
-
Flirt.publish event, response
|
26
|
-
end
|
21
|
+
describe "and a listener added with ##{method}" do
|
27
22
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
end
|
23
|
+
before(:each) do
|
24
|
+
Flirt.send method, listener, event, with: callback
|
25
|
+
end
|
32
26
|
|
33
|
-
it "doesn't listen to the wrong publish event" do
|
34
|
-
expect(listener).not_to receive(callback)
|
35
|
-
Flirt.broadcast wrong_event, response
|
36
|
-
end
|
37
27
|
|
38
|
-
|
39
|
-
before(:each) { Flirt.disable }
|
40
|
-
after(:each) { Flirt.enable }
|
28
|
+
[:publish, :broadcast].each do |method|
|
41
29
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
30
|
+
it "listens to the correct event published with ##{method}" do
|
31
|
+
expect(listener).to receive(callback).with(response)
|
32
|
+
Flirt.send method, event, response
|
33
|
+
end
|
46
34
|
|
47
|
-
it "doesn't publish" do
|
48
|
-
expect(listener).not_to receive(callback)
|
49
|
-
Flirt.publish event, response
|
50
|
-
end
|
51
|
-
end
|
52
35
|
|
53
|
-
|
36
|
+
it "doesn't listen to the wrong event published with ##{method}" do
|
37
|
+
expect(listener).not_to receive(callback)
|
38
|
+
Flirt.send method, wrong_event, response
|
39
|
+
end
|
54
40
|
|
55
|
-
let(:callback2) { :respond2 }
|
56
41
|
|
57
|
-
|
58
|
-
Flirt.listen listener, event, with: callback2
|
59
|
-
Flirt.clear
|
60
|
-
end
|
42
|
+
describe "when disabled" do
|
61
43
|
|
62
|
-
|
63
|
-
expect(listener).not_to receive(callback)
|
64
|
-
expect(listener).not_to receive(callback2)
|
65
|
-
Flirt.publish event, response
|
66
|
-
end
|
44
|
+
before(:each) { Flirt.disable }
|
67
45
|
|
68
|
-
|
46
|
+
after(:each) { Flirt.enable }
|
69
47
|
|
70
|
-
describe "when unlistened" do
|
71
48
|
|
72
|
-
|
49
|
+
it "doesn't broadcast an event published with ##{method}" do
|
50
|
+
expect(listener).not_to receive(callback)
|
51
|
+
Flirt.send method, event, response
|
52
|
+
end
|
73
53
|
|
74
|
-
|
75
|
-
Flirt.listen listener, event, with: callback2
|
76
|
-
Flirt.unlisten listener, event, with: callback
|
77
|
-
end
|
54
|
+
end
|
78
55
|
|
79
|
-
|
80
|
-
expect(listener).not_to receive(callback)
|
81
|
-
expect(listener).to receive(callback2).with(response)
|
82
|
-
Flirt.broadcast event, response
|
83
|
-
end
|
56
|
+
end
|
84
57
|
|
85
|
-
|
58
|
+
describe "when cleared" do
|
86
59
|
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
60
|
+
let(:callback2) { :respond2 }
|
61
|
+
|
62
|
+
|
63
|
+
before(:each) do
|
64
|
+
Flirt.listen listener, event, with: callback2
|
65
|
+
Flirt.clear
|
66
|
+
end
|
67
|
+
|
68
|
+
|
69
|
+
it "forgets listeners" do
|
70
|
+
expect(listener).not_to receive(callback)
|
71
|
+
expect(listener).not_to receive(callback2)
|
72
|
+
Flirt.publish event, response
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
|
77
|
+
[:unsubscribe, :unlisten].each do |method|
|
78
|
+
|
79
|
+
describe "when another listener is added and the original unsubscribed with ##{method}" do
|
80
|
+
|
81
|
+
let(:callback2) { :respond2 }
|
82
|
+
|
83
|
+
|
84
|
+
before(:each) do
|
85
|
+
Flirt.listen listener, event, with: callback2
|
86
|
+
Flirt.send method, listener, event, with: callback
|
87
|
+
end
|
88
|
+
|
89
|
+
|
90
|
+
it "it forgets the original listener but remembers the new one" do
|
91
|
+
expect(listener).not_to receive(callback)
|
92
|
+
expect(listener).to receive(callback2).with(response)
|
93
|
+
Flirt.broadcast event, response
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
91
99
|
|
92
|
-
it "forgets the listener" do
|
93
|
-
expect(listener).not_to receive(callback).with(response)
|
94
|
-
Flirt.publish event, response
|
95
100
|
end
|
96
101
|
|
97
102
|
end
|
103
|
+
|
98
104
|
end
|
105
|
+
|
99
106
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -12,7 +12,9 @@ Bundler.setup
|
|
12
12
|
require 'flirt'
|
13
13
|
|
14
14
|
RSpec.configure do |config|
|
15
|
+
|
15
16
|
config.run_all_when_everything_filtered = true
|
17
|
+
|
16
18
|
config.filter_run :focus
|
17
19
|
|
18
20
|
config.before(:each) do
|
@@ -24,4 +26,5 @@ RSpec.configure do |config|
|
|
24
26
|
# the seed, which is printed after each run.
|
25
27
|
# --seed 1234
|
26
28
|
config.order = 'random'
|
29
|
+
|
27
30
|
end
|