wisper_subscription 0.1.1 → 0.2.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: 912d8f12cd963ba1e732752590ca8c86022fae28
4
- data.tar.gz: 71b57abd56add0f8163f9757d17589893844fdcd
3
+ metadata.gz: cb3e3d3ee397931293ca4b2a84b98d8c49017048
4
+ data.tar.gz: b4cb1bb9c49d9c9adbeb6f144f8a8501d0458935
5
5
  SHA512:
6
- metadata.gz: 5dc155254a0c2aebc81902f451be952d8d3cd2b4c4b54a3218be998dcd4bc6c1d5861fa36cd8669f8dee5556ebc979abdf329cf809f62ce43855362889e95580
7
- data.tar.gz: 36759f7dad409e02d1d1713bf8f293603a60f11e1e01373885e92f02242802edcac2d6b115c215b39f6374ff6dbe1350c87ca991a4e4839627ce8e14700f3bc8
6
+ metadata.gz: d0c41b7a09ad91f941282bff92b75ad72f39226c2bf0d51c791660b4400235d38b0f6f654ee05663a9c13ab969cfd4f92e5d7758dc1a0a60ad4fd41f494c2f4b
7
+ data.tar.gz: 2d3d19cc13c6714b7bd754912e0610ab342671f0cd27a9f3d27d8898bebdbb938533ef9ac709adcf19a9cabe8830431bff1c4bdae3c12778e6528b02c7351712
data/README.md CHANGED
@@ -54,21 +54,76 @@ describe SomeWisperPublishingClass do
54
54
  before :each do
55
55
  # some setup
56
56
  subscriber.define_message :success
57
- subscriber.define_Message :failure
57
+ subscriber.define_message :failure
58
58
  command.subscribe subscriber
59
59
  command.do_something_that_broadcasts_a_success
60
60
  end
61
61
 
62
62
  it 'was successful' do
63
- payload = subscriber.payload_for(:success).to_a.first
64
- expect(payload).to be_a WhatYouExpectOnSuccess # as opposed to nil
63
+ payload = subscriber.payload_for(:success, 0)
64
+ expect(payload.first).to be_a WhatYouExpectOnSuccess # as opposed to nil
65
65
  end
66
66
 
67
67
  # ...
68
68
  end
69
69
  ```
70
70
 
71
- You get the idea. If not, open an issue or ask on our Gitter channel.
71
+ You get the idea. If not, open an issue or ask on our
72
+ [Gitter channel](https://gitter.im/jdickey/wisper_subscription).
73
+
74
+ ### Methods
75
+
76
+ #### `initialize`
77
+
78
+ Parameters: none.
79
+
80
+ Initialises instance internal state.
81
+
82
+ #### `define_message(message)`
83
+
84
+ Parameters:
85
+
86
+ 1. `message` (a Symbol; e.g., `:bangbang`)
87
+
88
+ Returns: `self`
89
+
90
+ Defines a *listener method* (e.g., `#bangbang`) to receive events published by a
91
+ [Wisper publisher](https://github.com/krisleech/wisper/#publishing) or similar
92
+ mechaism. Any parameters a listener method is called with will be retreivaable
93
+ by using the `#payload_for` or `#payloads_for` methods (see below).
94
+
95
+ Defines a query method (e.g., `#bangbang?`) which returns `true` if payloads for
96
+ the message have been received; `false` otherwise.
97
+
98
+ #### `payloads_for(message)`
99
+
100
+ Parameters:
101
+
102
+ 1. `message` (a Symbol, e.g., `:bangbang`)
103
+
104
+ Returns: an Array
105
+
106
+ Returns an Array of all *payloads* received by the listener method corresponding
107
+ to the parameter. If no calls to the listener method have been made, *or* if the
108
+ listener method has not been defined because `#define_message` has not been
109
+ called using that `message`, then an empty Array is returned.
110
+
111
+ #### `payload_for(message, index = 0)`
112
+
113
+ Parameters:
114
+
115
+ 1. `message` (a Symbol, e.g., `:bangbang`)
116
+ 1. `index` (an integer, defaulting to 0)
117
+
118
+ Returns: an Array or `nil`
119
+
120
+ Returns an Array containing the *payload* received by the `index`th invocation
121
+ of the listener method (zero-based). A *payload* is simply the set of (zero or)
122
+ more) parameters sent to the listener method. If no calls to the listener method
123
+ have been made, *or* if the listener method has not been defined because
124
+ `#define_message` has not been called using that `message`, then this method
125
+ returns `nil`. If the listener method *has* been defined but the specified index
126
+ is outside the range of received payloads, then returns an empty Array.
72
127
 
73
128
  ## Contributing
74
129
 
@@ -1,6 +1,8 @@
1
1
 
2
2
  require 'wisper_subscription/version'
3
3
 
4
+ require 'awesome_print'
5
+
4
6
  # Collects and reports on messages sent to an instance.
5
7
  class WisperSubscription
6
8
  def initialize
@@ -13,11 +15,16 @@ class WisperSubscription
13
15
  @message = message
14
16
  add_internals_entry
15
17
  add_query_method
16
- add_responder_method
18
+ add_listener_method
17
19
  self
18
20
  end
19
21
 
20
- def payload_for(message)
22
+ def payload_for(message, index = 0)
23
+ return nil unless @internals.key? message
24
+ payloads_for(message)[index]
25
+ end
26
+
27
+ def payloads_for(message)
21
28
  return empty_payload unless @internals.key? message
22
29
  @internals[message]
23
30
  end
@@ -49,7 +56,7 @@ class WisperSubscription
49
56
  self
50
57
  end
51
58
 
52
- def add_responder_method
59
+ def add_listener_method
53
60
  message = @message
54
61
  define_singleton_method @message.to_sym do |*params|
55
62
  @internals[message].push params
@@ -1,5 +1,5 @@
1
1
 
2
2
  # Collects and reports on messages sent to an instance.
3
3
  class WisperSubscription
4
- VERSION = '0.1.1'
4
+ VERSION = '0.2.0'
5
5
  end
data/spec/spec_helper.rb CHANGED
@@ -7,4 +7,7 @@ end
7
7
 
8
8
  $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
9
9
 
10
+ require 'awesome_print'
11
+ require 'pry'
12
+
10
13
  require 'wisper_subscription'
@@ -69,8 +69,62 @@ describe WisperSubscription do
69
69
  end # describe '#define_message'
70
70
 
71
71
  describe '#payload_for' do
72
+ describe 'accepts two parameters, with' do
73
+ let(:method) { described_class.new.method :payload_for }
74
+
75
+ it 'the first, :message, being required' do
76
+ expect(method.parameters.count).to eq 2
77
+ expect(method.parameters.first).to eq [:req, :message]
78
+ end
79
+
80
+ it 'the second, :index, being optional' do
81
+ expect(method.parameters.last).to eq [:opt, :index]
82
+ end
83
+ end # describe 'accepts two parameters, with'
84
+
85
+ describe 'returns nil when' do
86
+ it 'no messages have been defined' do
87
+ expect(obj.payload_for message).to eq nil
88
+ end
89
+
90
+ it 'the specified message has been defined but not received' do
91
+ obj.define_message other_message
92
+ expect(obj.payload_for message).to eq nil
93
+ end
94
+
95
+ it 'the specified index exceeds the range of received messages' do
96
+ obj.define_message message
97
+ obj.send message, 'foo'
98
+ expect(obj.payload_for message, 52).to eq nil
99
+ end
100
+ end # describe 'returns nil when'
101
+
102
+ context 'when the message was received and the index is in range' do
103
+ before :each do
104
+ obj.define_message message
105
+ end
106
+
107
+ it 'returns an Array containing the specified payload when supplied' do
108
+ obj.send message, 'foo'
109
+ expect(obj.payload_for message, 0).to eq ['foo']
110
+ end
111
+
112
+ it 'returns an empty Array if no payload for the message was specified' do
113
+ obj.send message
114
+ expect(obj.payload_for message, 0).to eq []
115
+ end
116
+ end # context 'when the message was received and the index is in range'
117
+
118
+ fit 'uses zero as a default for the second (index) parameter' do
119
+ obj.define_message message
120
+ [:foo, :bar, :baz].each { |payload| obj.send message, payload }
121
+ expect(obj.payload_for message).to eq [:foo]
122
+ end
123
+ end # describe '#payload_for'
124
+
125
+ describe '#payloads_for' do
72
126
  it 'takes a single parameter' do
73
- method = obj.method :payload_for
127
+ method = obj.method :payloads_for
74
128
  expect(method.arity).to eq 1
75
129
  end
76
130
 
@@ -78,7 +132,7 @@ describe WisperSubscription do
78
132
  let(:none_received) { [] }
79
133
 
80
134
  after :each do
81
- expect(obj.payload_for message).to eq none_received
135
+ expect(obj.payloads_for message).to eq none_received
82
136
  end
83
137
 
84
138
  it 'no messages have been defined' do
@@ -94,14 +148,31 @@ describe WisperSubscription do
94
148
  end # describe 'returns a no-payloads-received indicator when'
95
149
 
96
150
  describe 'returns the payload(s) received for the specified message when' do
97
- it 'payloads for that message have been stored' do
151
+ before :each do
98
152
  obj.define_message message
99
- expected = 'testing'
100
- obj.instance_variable_get(:@internals)[message].push expected
101
- expect(obj.payload_for message).to eq Array.new([expected])
153
+ end
154
+
155
+ it 'a single payload for that message has been stored' do
156
+ expected = %w(This is a test.)
157
+ obj.send message, *expected
158
+ payloads = obj.payloads_for message
159
+ expect(payloads).to be_an Array
160
+ expect(payloads.count).to eq 1 # One payload has been delivered
161
+ payload = payloads.first
162
+ expect(payload.count).to eq expected.count
163
+ expect(payload).to eq expected
164
+ end
165
+
166
+ it 'multiple payloads for that message have been stored' do
167
+ obj.send message, 'one'
168
+ obj.send message, 'two', 'three'
169
+ payloads = obj.payloads_for message
170
+ expect(payloads.count).to eq 2 # Two payloads delivered
171
+ expect(payloads.first).to eq ['one']
172
+ expect(payloads.last).to eq %w(two three)
102
173
  end
103
174
  end # describe 'returns the payload(s) received for the ... message when'
104
- end # describe '#payload_for'
175
+ end # describe '#payloads_for'
105
176
 
106
177
  describe '#respond_to?' do
107
178
  it 'returns true for any query method' do
@@ -23,9 +23,9 @@ Gem::Specification.new do |spec|
23
23
 
24
24
  spec.add_development_dependency 'rubocop', '>= 0.28.0'
25
25
  spec.add_development_dependency 'simplecov', '>= 0.9.1'
26
- # spec.add_development_dependency 'awesome_print'
27
- # spec.add_development_dependency 'pry-byebug'
28
- # spec.add_development_dependency 'pry-doc'
26
+ spec.add_development_dependency 'awesome_print'
27
+ spec.add_development_dependency 'pry-byebug'
28
+ spec.add_development_dependency 'pry-doc'
29
29
 
30
30
  spec.description = %q{May be used to subscribe to Wisper broadcasts, or other
31
31
  similar mechanisms. Define what messages you want an instance to respond to and,
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wisper_subscription
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Dickey
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-29 00:00:00.000000000 Z
11
+ date: 2015-01-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -80,6 +80,48 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: 0.9.1
83
+ - !ruby/object:Gem::Dependency
84
+ name: awesome_print
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry-byebug
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: pry-doc
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
83
125
  description: |-
84
126
  May be used to subscribe to Wisper broadcasts, or other
85
127
  similar mechanisms. Define what messages you want an instance to respond to and,
@@ -131,3 +173,4 @@ summary: Records and reports messages sent using Wisper conventions.
131
173
  test_files:
132
174
  - spec/spec_helper.rb
133
175
  - spec/wisper_subscription_spec.rb
176
+ has_rdoc: