flirt 0.0.4 → 0.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: ddeb6e470d1d83c9d031c9eadbd763929ea01863
4
- data.tar.gz: b9f82e07bde11c678184ee7ac79007c6e2ce7bea
3
+ metadata.gz: 564e3009a8c2864fb2de008cb57be72e8440017f
4
+ data.tar.gz: 87e6f2718d7ffa0d922b79f73bb718b469704a56
5
5
  SHA512:
6
- metadata.gz: 9b57170b981b3bbcc68cb5e57304ca0ccd341898eab2d668bb0d22a434c2addab6f73792cf029c084072a6346797ab0fc254a6493cccb82616d5a34e1e88649d
7
- data.tar.gz: c7519ff2ae78315188bb0ce004fc22ce36cb70708ab3cba8a62d69bcaeee1c3b6d930d87832abe1abbc90901b6270c5a5d21304acbd6d4bad880a9630c9d151d
6
+ metadata.gz: b97a5309761523a1e376c56333ebaaf4d9a8315d21774fd84b8b66bd37ffcdfe0d1e801bd9e37b7c9bf40f7feaeb821a46b5e90c22684574f2324677c2fdaa04
7
+ data.tar.gz: 19ffd5697790350cdab302e69aaaf39437589d17c0f6fcec617c1e22708a97a342ca3c3c1eb745c04f0bbdf44fe7a8f7b2b763f05b4b2784445a54d27dfef2f9
data/README.md CHANGED
@@ -29,14 +29,15 @@ Or install it yourself as:
29
29
 
30
30
  ## Usage
31
31
 
32
- To publish an event:
32
+ ###To publish an event:
33
33
 
34
34
 
35
35
  ```ruby
36
+ event_data = { fruit: 'apple' }
36
37
  Flirt.publish :picked, event_data
37
38
  ```
38
39
 
39
- To subscribe:
40
+ ###To subscribe:
40
41
 
41
42
  ```ruby
42
43
  class MyListener
@@ -50,7 +51,7 @@ class MyListener
50
51
  end
51
52
  ```
52
53
 
53
- To unsubscribe:
54
+ ###To unsubscribe:
54
55
 
55
56
  ```ruby
56
57
  Flirt.unsubscribe self, :picked, with: :picked_callback
@@ -94,7 +95,7 @@ end
94
95
 
95
96
  ```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
+ ###Flirt defaults to 'enabled'. Switch Flirt off:
98
99
 
99
100
  ```ruby
100
101
  Flirt.disable
@@ -106,27 +107,48 @@ And back on again:
106
107
  Flirt.enable
107
108
  ```
108
109
 
109
- TODO: Disable only a set of events:
110
+ Enabled status affects publishing only, listeners can still be added and will be
111
+ remembered. No listeners will be removed.
112
+
113
+ ###Disable only a set of events:
110
114
 
111
115
  ```ruby
112
116
  Flirt.disable only: [:pancake_cooked, :picked]
113
117
  ```
114
118
 
115
- TODO: Enable only a set of events:
119
+ ###Enable only a set of events:
116
120
 
117
121
  ```ruby
118
122
  Flirt.enable only: [:topping_added, :pancake_flipped]
119
123
  ```
120
124
 
121
- Enabled status affects broadcast/publish, listeners can still be added and will be
122
- remembered. No listeners will be removed.
125
+ Disabling and enabling sets of events is not cumulative. The new set of events will overwrite all previous calls.
126
+ For example:
127
+
128
+ ```ruby
129
+ Flirt.disable only: [:pancake_cooked, :picked]
130
+ Flirt.disable only: [:flipped]
131
+ ```
123
132
 
124
- Clear all listeners:
133
+ The above code will leave only ```:flipped``` disabled.
134
+
135
+ ```ruby
136
+ Flirt.enable only: [:flipped, :picked]
137
+ Flirt.disable only: [:flipped]
138
+ ```
139
+
140
+ The above code will also leave only ```:flipped``` disabled.
141
+
142
+ Calling ```Flirt.enable``` or ```Flirt.disable``` will clear any previously set enabled or disabled events.
143
+
144
+ ###Clear all listeners:
125
145
 
126
146
  ```ruby
127
147
  Flirt.clear
128
148
  ```
129
149
 
150
+ This operation cannot be undone.
151
+
130
152
  ## Contributing
131
153
 
132
154
  1. Fork it ( https://github.com/[my-github-username]/flirt/fork )
data/flirt.gemspec CHANGED
@@ -21,4 +21,5 @@ Gem::Specification.new do |spec|
21
21
  spec.add_development_dependency "bundler", "~> 1.7"
22
22
  spec.add_development_dependency "rake", "~> 10.0"
23
23
  spec.add_development_dependency "rspec", "~> 3.2"
24
+ spec.add_development_dependency "byebug", "~> 4.0"
24
25
  end
@@ -7,8 +7,6 @@
7
7
  #
8
8
  # def initialize
9
9
  # subscribe_to :picked, with: :picked_callback
10
- # # or the alias
11
- # listen_to :picked, with: :picked_callback
12
10
  # end
13
11
  #
14
12
  # def picked_callback(event_data)
@@ -22,8 +20,6 @@
22
20
  # extend Flirt::Listener
23
21
  #
24
22
  # subscribe_to :picked, with: :picked_callback
25
- # # or the alias
26
- # listen_to :picked, with: :picked_callback
27
23
  #
28
24
  # def self.picked_callback(event_data)
29
25
  # puts "The #{event_data[:fruit]} has been picked"
data/lib/flirt/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Flirt
2
2
 
3
- VERSION = "0.0.4"
4
-
3
+ VERSION = "0.1.0"
4
+
5
5
  end
data/lib/flirt.rb CHANGED
@@ -11,7 +11,10 @@ module Flirt
11
11
 
12
12
  def publish(event_name, event_data)
13
13
  return if disabled
14
+ return if disabled_list && disabled_list.include?(event_name)
15
+ return if enabled_list && !enabled_list.include?(event_name)
14
16
  raise ArgumentError.new("Event name must be a symbol") unless event_name.is_a? Symbol
17
+
15
18
  (callbacks[event_name] || []).each do |callback|
16
19
  callback.call(event_data)
17
20
  end
@@ -34,13 +37,23 @@ module Flirt
34
37
  end
35
38
 
36
39
 
37
- def enable
40
+ def enable(opts = {})
41
+ clear_event_lists
42
+ if opts[:only]
43
+ set_enabled opts[:only]
44
+ end
38
45
  self.disabled = false
39
46
  end
40
47
 
41
48
 
42
- def disable
43
- self.disabled = true
49
+ def disable(opts = {})
50
+ clear_event_lists
51
+ if opts[:only]
52
+ set_disabled opts[:only]
53
+ self.disabled = false
54
+ else
55
+ self.disabled = true
56
+ end
44
57
  end
45
58
 
46
59
 
@@ -52,7 +65,7 @@ module Flirt
52
65
  private
53
66
 
54
67
  attr_reader :callbacks
55
- attr_accessor :disabled
68
+ attr_accessor :disabled, :disabled_list, :enabled_list
56
69
 
57
70
 
58
71
  def callbacks
@@ -66,6 +79,26 @@ module Flirt
66
79
  end
67
80
 
68
81
 
82
+ def set_disabled(events)
83
+ self.disabled_list = wrap_event_list(events)
84
+ end
85
+
86
+
87
+ def set_enabled(events)
88
+ self.enabled_list = wrap_event_list(events)
89
+ end
90
+
91
+
92
+ def clear_event_lists
93
+ self.enabled_list = nil
94
+ self.disabled_list = nil
95
+ end
96
+
97
+
98
+ def wrap_event_list(events)
99
+ events.is_a?(Array) ? events : [events]
100
+ end
101
+
69
102
  def remove_callback(event_name, callback_to_delete)
70
103
  return unless callbacks[event_name]
71
104
 
@@ -50,6 +50,126 @@ describe Flirt do
50
50
  end
51
51
 
52
52
 
53
+ describe "then more listeners added" do
54
+
55
+ let(:event2) { :topping_added }
56
+
57
+ let(:event3) { :plated_up }
58
+
59
+ let(:callback2) { :respond2 }
60
+
61
+ let(:callback3) { :respond3 }
62
+
63
+ before(:each) do
64
+ Flirt.subscribe listener, event2, with: callback2
65
+
66
+ Flirt.subscribe listener, event3, with: callback3
67
+ end
68
+
69
+ after(:each) { Flirt.enable }
70
+
71
+ describe "when a single event is disabled" do
72
+
73
+ it "disables just that event" do
74
+ Flirt.disable only: :pancake_fried
75
+ expect(listener).not_to receive(callback)
76
+ expect(listener).to receive(callback2).with(response)
77
+ expect(listener).to receive(callback3).with(response)
78
+ Flirt.publish event, response
79
+ Flirt.publish event2, response
80
+ Flirt.publish event3, response
81
+ end
82
+
83
+ end
84
+
85
+ describe "when multiple events are disabled" do
86
+
87
+ before(:each) { Flirt.disable only: [:pancake_fried, :topping_added] }
88
+
89
+ it "disables those events" do
90
+ expect(listener).not_to receive(callback)
91
+ expect(listener).not_to receive(callback2)
92
+ expect(listener).to receive(callback3).with(response)
93
+ Flirt.publish event, response
94
+ Flirt.publish event2, response
95
+ Flirt.publish event3, response
96
+ end
97
+
98
+ it "forgets those events after a call to #enable" do
99
+ Flirt.enable
100
+ expect(listener).to receive(callback)
101
+ expect(listener).to receive(callback2).with(response)
102
+ expect(listener).to receive(callback3).with(response)
103
+ Flirt.publish event, response
104
+ Flirt.publish event2, response
105
+ Flirt.publish event3, response
106
+ end
107
+
108
+
109
+ it "forgets those events after a call to #disable" do
110
+ Flirt.disable
111
+ expect(listener).not_to receive(callback)
112
+ expect(listener).not_to receive(callback2).with(response)
113
+ expect(listener).not_to receive(callback3).with(response)
114
+ Flirt.publish event, response
115
+ Flirt.publish event2, response
116
+ Flirt.publish event3, response
117
+ end
118
+ end
119
+
120
+ describe "when a single event is enabled" do
121
+
122
+ it "enables just that event" do
123
+ Flirt.enable only: :pancake_fried
124
+ expect(listener).to receive(callback)
125
+ expect(listener).not_to receive(callback2).with(response)
126
+ expect(listener).not_to receive(callback3).with(response)
127
+ Flirt.publish event, response
128
+ Flirt.publish event2, response
129
+ Flirt.publish event3, response
130
+ end
131
+
132
+ end
133
+
134
+
135
+ describe "when multiple events are enabled" do
136
+
137
+ before(:each) { Flirt.enable only: [:pancake_fried, :topping_added] }
138
+ it "enables those events" do
139
+ Flirt.enable only: [:pancake_fried, :topping_added]
140
+ expect(listener). to receive(callback)
141
+ expect(listener). to receive(callback2).with(response)
142
+ expect(listener).not_to receive(callback3).with(response)
143
+ Flirt.publish event, response
144
+ Flirt.publish event2, response
145
+ Flirt.publish event3, response
146
+ end
147
+
148
+
149
+ it "forgets those events after a call to #enable" do
150
+ Flirt.enable
151
+ expect(listener).to receive(callback)
152
+ expect(listener).to receive(callback2).with(response)
153
+ expect(listener).to receive(callback3).with(response)
154
+ Flirt.publish event, response
155
+ Flirt.publish event2, response
156
+ Flirt.publish event3, response
157
+ end
158
+
159
+
160
+ it "forgets those events after a call to #disable" do
161
+ Flirt.disable
162
+ expect(listener).not_to receive(callback)
163
+ expect(listener).not_to receive(callback2).with(response)
164
+ expect(listener).not_to receive(callback3).with(response)
165
+ Flirt.publish event, response
166
+ Flirt.publish event2, response
167
+ Flirt.publish event3, response
168
+ end
169
+ end
170
+ end
171
+
172
+
53
173
  describe "when cleared" do
54
174
 
55
175
  let(:callback2) { :respond2 }
data/spec/spec_helper.rb CHANGED
@@ -9,6 +9,7 @@ require 'rubygems'
9
9
  require 'bundler/setup'
10
10
  Bundler.setup
11
11
 
12
+ require 'byebug'
12
13
  require 'flirt'
13
14
 
14
15
  RSpec.configure do |config|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flirt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benjamin Randles-Dunkley
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '4.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '4.0'
55
69
  description: " Provides a single point for the publication and subscription of events,
56
70
  promoting extreme decoupling. "
57
71
  email: