flirt 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +17 -17
- data/lib/flirt/callback.rb +1 -0
- data/lib/flirt/listener.rb +3 -4
- data/lib/flirt/version.rb +3 -1
- data/lib/flirt.rb +3 -5
- data/spec/flirt/flirt_listener_spec.rb +26 -38
- data/spec/flirt/flirt_spec.rb +41 -51
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ddeb6e470d1d83c9d031c9eadbd763929ea01863
|
4
|
+
data.tar.gz: b9f82e07bde11c678184ee7ac79007c6e2ce7bea
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9b57170b981b3bbcc68cb5e57304ca0ccd341898eab2d668bb0d22a434c2addab6f73792cf029c084072a6346797ab0fc254a6493cccb82616d5a34e1e88649d
|
7
|
+
data.tar.gz: c7519ff2ae78315188bb0ce004fc22ce36cb70708ab3cba8a62d69bcaeee1c3b6d930d87832abe1abbc90901b6270c5a5d21304acbd6d4bad880a9630c9d151d
|
data/README.md
CHANGED
@@ -29,20 +29,12 @@ Or install it yourself as:
|
|
29
29
|
|
30
30
|
## Usage
|
31
31
|
|
32
|
-
To publish
|
32
|
+
To publish an event:
|
33
33
|
|
34
|
-
```ruby
|
35
|
-
event_data = { fruit: "apple" }
|
36
|
-
Flirt.broadcast :picked, event_data
|
37
|
-
```
|
38
|
-
|
39
|
-
Or:
|
40
34
|
|
41
35
|
```ruby
|
42
36
|
Flirt.publish :picked, event_data
|
43
37
|
```
|
44
|
-
|
45
|
-
(These two versions are aliases)
|
46
38
|
|
47
39
|
To subscribe:
|
48
40
|
|
@@ -50,8 +42,6 @@ To subscribe:
|
|
50
42
|
class MyListener
|
51
43
|
def initialize
|
52
44
|
Flirt.subscribe self, :picked, with: :picked_callback
|
53
|
-
# or the alias
|
54
|
-
Flirt.listen self, :picked, with: :picked_callback
|
55
45
|
end
|
56
46
|
|
57
47
|
def picked_callback(event_data)
|
@@ -60,7 +50,15 @@ class MyListener
|
|
60
50
|
end
|
61
51
|
```
|
62
52
|
|
63
|
-
|
53
|
+
To unsubscribe:
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
Flirt.unsubscribe self, :picked, with: :picked_callback
|
57
|
+
end
|
58
|
+
```
|
59
|
+
|
60
|
+
|
61
|
+
Syntactic sugar for subscription and unsubscription has been provided in the form of a module:
|
64
62
|
|
65
63
|
```ruby
|
66
64
|
class MyListener
|
@@ -68,8 +66,10 @@ class MyListener
|
|
68
66
|
|
69
67
|
def initialize
|
70
68
|
subscribe_to :picked, with: :picked_callback
|
71
|
-
|
72
|
-
|
69
|
+
end
|
70
|
+
|
71
|
+
def before_destroy
|
72
|
+
unsubscribe_from :picked, with: :picked_callback
|
73
73
|
end
|
74
74
|
|
75
75
|
def picked_callback(event_data)
|
@@ -85,8 +85,6 @@ class MyListener
|
|
85
85
|
extend Flirt::Listener
|
86
86
|
|
87
87
|
subscribe_to :picked, with: :picked_callback
|
88
|
-
# or the alias
|
89
|
-
listen_to :picked, with: :picked_callback
|
90
88
|
|
91
89
|
def self.picked_callback(event_data)
|
92
90
|
puts "The #{event_data[:fruit]} has been picked"
|
@@ -94,6 +92,8 @@ class MyListener
|
|
94
92
|
end
|
95
93
|
```
|
96
94
|
|
95
|
+
```unsubscribe_from``` can technically be used in the class context, but probably doesn't have as much use.
|
96
|
+
|
97
97
|
Flirt defaults to 'enabled'. Switch Flirt off:
|
98
98
|
|
99
99
|
```ruby
|
@@ -121,7 +121,7 @@ Flirt.enable only: [:topping_added, :pancake_flipped]
|
|
121
121
|
Enabled status affects broadcast/publish, listeners can still be added and will be
|
122
122
|
remembered. No listeners will be removed.
|
123
123
|
|
124
|
-
Clear all listeners
|
124
|
+
Clear all listeners:
|
125
125
|
|
126
126
|
```ruby
|
127
127
|
Flirt.clear
|
data/lib/flirt/callback.rb
CHANGED
data/lib/flirt/listener.rb
CHANGED
@@ -32,19 +32,18 @@
|
|
32
32
|
#
|
33
33
|
|
34
34
|
module Flirt
|
35
|
+
|
35
36
|
module Listener
|
37
|
+
|
36
38
|
def subscribe_to(event_name, opts = {})
|
37
|
-
raise ArgumentError.new("You must pass a callback") unless opts[:with].is_a? Symbol
|
38
39
|
Flirt.subscribe self, event_name, opts
|
39
40
|
end
|
40
|
-
alias_method :listen_to, :subscribe_to
|
41
41
|
|
42
42
|
|
43
43
|
def unsubscribe_from(event_name, opts = {})
|
44
|
-
raise ArgumentError.new("You must pass a callback") unless opts[:with].is_a? Symbol
|
45
44
|
Flirt.unsubscribe self, event_name, opts
|
46
45
|
end
|
47
|
-
alias_method :forget, :unsubscribe_from
|
48
46
|
|
49
47
|
end
|
48
|
+
|
50
49
|
end
|
data/lib/flirt/version.rb
CHANGED
data/lib/flirt.rb
CHANGED
@@ -16,7 +16,6 @@ module Flirt
|
|
16
16
|
callback.call(event_data)
|
17
17
|
end
|
18
18
|
end
|
19
|
-
alias_method :broadcast, :publish
|
20
19
|
|
21
20
|
|
22
21
|
def subscribe(object, event_name, options = {})
|
@@ -25,7 +24,6 @@ module Flirt
|
|
25
24
|
callback_name: options[:with]
|
26
25
|
add_callback(event_name, callback)
|
27
26
|
end
|
28
|
-
alias_method :listen, :subscribe
|
29
27
|
|
30
28
|
|
31
29
|
def unsubscribe(object, event_name, options = {})
|
@@ -34,7 +32,6 @@ module Flirt
|
|
34
32
|
callback_name: options[:with]
|
35
33
|
remove_callback(event_name, callback)
|
36
34
|
end
|
37
|
-
alias_method :unlisten, :unsubscribe
|
38
35
|
|
39
36
|
|
40
37
|
def enable
|
@@ -79,10 +76,11 @@ module Flirt
|
|
79
76
|
|
80
77
|
|
81
78
|
def check_subscription_arguments(event_name, object, options)
|
82
|
-
raise ArgumentError.new("You must pass a callback")
|
83
|
-
raise ArgumentError.new("You must pass an object") if object.nil?
|
79
|
+
raise ArgumentError.new("You must pass a callback") unless options[:with].is_a? Symbol
|
84
80
|
raise ArgumentError.new("You must pass an event name") unless event_name.is_a? Symbol
|
81
|
+
raise ArgumentError.new("You must pass an object") if object.nil?
|
85
82
|
end
|
86
83
|
|
87
84
|
end
|
85
|
+
|
88
86
|
end
|
@@ -27,29 +27,23 @@ describe Flirt::Listener do
|
|
27
27
|
end
|
28
28
|
|
29
29
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
send method, block_event, with: block_callback_name
|
37
|
-
end
|
38
|
-
|
30
|
+
it "subscribes to an event with #subscribe_to" do
|
31
|
+
block_event = event
|
32
|
+
block_callback_name = callback_name
|
33
|
+
expect(Flirt).to receive(:subscribe).with(test_instance, event, { with: callback_name })
|
34
|
+
test_instance.instance_eval do
|
35
|
+
subscribe_to block_event, with: block_callback_name
|
39
36
|
end
|
40
37
|
|
41
38
|
end
|
42
39
|
|
43
40
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
send method, block_event, with: block_callback_name
|
51
|
-
end
|
52
|
-
|
41
|
+
it "unsubscribes from an event with #unsubscribe_from" do
|
42
|
+
block_event = event
|
43
|
+
block_callback_name = callback_name
|
44
|
+
expect(Flirt).to receive(:unsubscribe).with(test_instance, event, { with: callback_name })
|
45
|
+
test_instance.instance_eval do
|
46
|
+
unsubscribe_from block_event, with: block_callback_name
|
53
47
|
end
|
54
48
|
|
55
49
|
end
|
@@ -68,31 +62,25 @@ describe Flirt::Listener do
|
|
68
62
|
let(:test_class) { Class.new }
|
69
63
|
|
70
64
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
send method, block_event, with: block_callback_name
|
79
|
-
end
|
80
|
-
|
65
|
+
it "subscribes to an event with #subscribe_to" do
|
66
|
+
block_event = event
|
67
|
+
block_callback_name = callback_name
|
68
|
+
expect(Flirt).to receive(:subscribe).with(test_class, event, { with: callback_name })
|
69
|
+
test_class.class_eval do
|
70
|
+
extend Flirt::Listener
|
71
|
+
subscribe_to block_event, with: block_callback_name
|
81
72
|
end
|
82
73
|
|
83
74
|
end
|
84
75
|
|
85
76
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
send method, block_event, with: block_callback_name
|
94
|
-
end
|
95
|
-
|
77
|
+
it "unsubscribes from an event with #unsubscribe_from" do
|
78
|
+
block_event = event
|
79
|
+
block_callback_name = callback_name
|
80
|
+
expect(Flirt).to receive(:unsubscribe).with(test_class, event, { with: callback_name })
|
81
|
+
test_class.class_eval do
|
82
|
+
extend Flirt::Listener
|
83
|
+
unsubscribe_from block_event, with: block_callback_name
|
96
84
|
end
|
97
85
|
|
98
86
|
end
|
data/spec/flirt/flirt_spec.rb
CHANGED
@@ -16,85 +16,75 @@ describe Flirt do
|
|
16
16
|
let!(:listener) { Object.new }
|
17
17
|
|
18
18
|
|
19
|
-
|
19
|
+
describe "and a listener added with #subscribe" do
|
20
20
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
Flirt.send method, listener, event, with: callback
|
25
|
-
end
|
26
|
-
|
27
|
-
|
28
|
-
[:publish, :broadcast].each do |method|
|
29
|
-
|
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
|
21
|
+
before(:each) do
|
22
|
+
Flirt.subscribe listener, event, with: callback
|
23
|
+
end
|
34
24
|
|
35
25
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
26
|
+
it "listens to the correct event published with #publish" do
|
27
|
+
expect(listener).to receive(callback).with(response)
|
28
|
+
Flirt.publish event, response
|
29
|
+
end
|
40
30
|
|
41
31
|
|
42
|
-
|
32
|
+
it "doesn't listen to the wrong event published with #publish" do
|
33
|
+
expect(listener).not_to receive(callback)
|
34
|
+
Flirt.publish wrong_event, response
|
35
|
+
end
|
43
36
|
|
44
|
-
before(:each) { Flirt.disable }
|
45
37
|
|
46
|
-
|
38
|
+
describe "when disabled" do
|
47
39
|
|
40
|
+
before(:each) { Flirt.disable }
|
48
41
|
|
49
|
-
|
50
|
-
expect(listener).not_to receive(callback)
|
51
|
-
Flirt.send method, event, response
|
52
|
-
end
|
42
|
+
after(:each) { Flirt.enable }
|
53
43
|
|
54
|
-
end
|
55
44
|
|
45
|
+
it "doesn't publish an event" do
|
46
|
+
expect(listener).not_to receive(callback)
|
47
|
+
Flirt.publish event, response
|
56
48
|
end
|
57
49
|
|
58
|
-
|
59
|
-
|
60
|
-
let(:callback2) { :respond2 }
|
50
|
+
end
|
61
51
|
|
62
52
|
|
63
|
-
|
64
|
-
Flirt.listen listener, event, with: callback2
|
65
|
-
Flirt.clear
|
66
|
-
end
|
53
|
+
describe "when cleared" do
|
67
54
|
|
55
|
+
let(:callback2) { :respond2 }
|
68
56
|
|
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
57
|
|
58
|
+
before(:each) do
|
59
|
+
Flirt.subscribe listener, event, with: callback2
|
60
|
+
Flirt.clear
|
75
61
|
end
|
76
62
|
|
77
|
-
[:unsubscribe, :unlisten].each do |method|
|
78
63
|
|
79
|
-
|
64
|
+
it "forgets listeners" do
|
65
|
+
expect(listener).not_to receive(callback)
|
66
|
+
expect(listener).not_to receive(callback2)
|
67
|
+
Flirt.publish event, response
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
80
71
|
|
81
|
-
let(:callback2) { :respond2 }
|
82
72
|
|
73
|
+
describe "when another listener is added and the original is unsubscribed" do
|
83
74
|
|
84
|
-
|
85
|
-
Flirt.listen listener, event, with: callback2
|
86
|
-
Flirt.send method, listener, event, with: callback
|
87
|
-
end
|
75
|
+
let(:callback2) { :respond2 }
|
88
76
|
|
89
77
|
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
end
|
78
|
+
before(:each) do
|
79
|
+
Flirt.subscribe listener, event, with: callback2
|
80
|
+
Flirt.unsubscribe listener, event, with: callback
|
81
|
+
end
|
95
82
|
|
96
|
-
end
|
97
83
|
|
84
|
+
it "it forgets the original listener but remembers the new one" do
|
85
|
+
expect(listener).not_to receive(callback)
|
86
|
+
expect(listener).to receive(callback2).with(response)
|
87
|
+
Flirt.publish event, response
|
98
88
|
end
|
99
89
|
|
100
90
|
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.4
|
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-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|