response_state 0.3.0 → 0.4.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: 582d7c3101becc563d9c0696000bc50ab4d3646c
4
- data.tar.gz: 67cb929a24c3261d6e3c1bc9adf5677d2ae5785e
3
+ metadata.gz: 49640a1d8e13c04e6904e1672de5233b74e10577
4
+ data.tar.gz: 5ecc66ed44412cedb76b30afc5b8021d1d2ef96d
5
5
  SHA512:
6
- metadata.gz: 1bfae51a4b1628e3726a16217cb254abbe0cafcebb142f09920d0b8d2b3ddc8f2e9df2e3607ff22550137f735cf910454d7bc64693a06e3e4a6f80a96ec26939
7
- data.tar.gz: 97eb8d8a09ea753479fb470ca659e50a64872f0a5b01e86c8749d2480f116cd921ea33ecdf686a67f7a5c2270698d6ddd59bec8ebd87d2cc8b060b1235b10a04
6
+ metadata.gz: 5104ca4e50e10cf325155e4c6e5108f353a8dd91a7a2dfd0e7775b7054def88814ec3b9dd27dac8fbdbeef61a2fdb6392fa1b01d0bb58a47f8adab96b8892bab
7
+ data.tar.gz: dfcc5f13dd9c9b6d8fa60ce39363d2a4f7fece15c17888ea2bcdca0f9158dcda349e2786e4e473a3405ed276003240d69bb26069fd93f87438696d5ed6c6a4f5
data/README.md CHANGED
@@ -24,44 +24,60 @@ Create a service class and subclass ResponseState::Service.
24
24
 
25
25
  ```ruby
26
26
  class MyService < ResponseState::Service
27
+ response_states :success, :failure
28
+
27
29
  def initialize(param)
28
30
  @param = param
29
31
  end
30
32
 
31
33
  def call(&block)
32
34
  # do some work
33
- yield send_state(:success)
35
+ yield send_state :success
34
36
  end
35
37
  end
36
38
  ```
37
39
 
38
- You must implement a `call` method.
39
- Your call method should yield with a `ResponseState::Response`.
40
- The response can be generated with a helper method `send_state` in your service class.
40
+ You must implement a `call` method. You must also indicated the valid response states
41
+ using the `response_states` class macro method.
41
42
 
42
- You can optionally declare and restrict the set of valid states for your service's responses.
43
- The service will use this set of states when generating the response with the `send_state` method.
43
+ Your call method should yield with a call to `send_state` which will create a `ResponseState::Response`.
44
44
 
45
- ```ruby
46
- class MyService < ResponseState::Service
47
- response_states :success, :failure
45
+ The `send_state` method takes at a minimum a symbol representing the state. It optionally can also
46
+ take a message and a context. The message by convention should be a string but there are no restrictions.
47
+ The context can be any object. An error will be raised if a state is specified that is not in the list
48
+ of valid response states.
48
49
 
49
- def call(&block)
50
- # do some work
51
- yield send_state(:success)
52
- end
50
+ ### Your service API
51
+
52
+ Your service can now be used as such:
53
+
54
+ ```ruby
55
+ MyService.('Some param') do |response|
56
+ response.success { puts 'I was successful.' }
57
+ response.failure { puts 'I failed.' }
53
58
  end
54
59
  ```
55
60
 
61
+ You can optionally ensure all states have been handled by placing a call
62
+ to `unhandled_states` at the end of your response block. This will yield an array of
63
+ unhandled states to the given block if there are any unhandled states.
64
+
65
+ ```ruby
66
+ MyService.('Some param') do |response|
67
+ response.success { puts 'I was successful.' }
68
+ response.failure { puts 'I failed.' }
69
+ response.unhandled_states { |states| raise "You didn't handle #{states.join(', ')}" }
70
+ end
71
+ ```
56
72
 
57
- ### Response
73
+ ### ResponseState::Response
58
74
 
59
75
  A `ResponseState::Response` can take up to 4 arguments but must at least have the first argument which is the state of the response. In addition it can take a message, a context, and a set of valid states. The message by convention should
60
76
  be a string but there are no restrictions. The context can be any object. The valid states should be an array of symbols
61
77
  that are the allowed states. An exception will be thrown if initialized with a type of response that is not in the valid states if a set of valid states was specified.
62
78
 
63
79
  ```ruby
64
- response = Response.new(:success, 'You win!', {an_important_value: 'some value'})
80
+ response = Response.new(:success, 'You win!', {an_important_value: 'some value'}, [:success, :failure])
65
81
  response.type # :success
66
82
  response.message # 'You win!'
67
83
  response.context # {an_important_value: 'some value'}
@@ -93,30 +109,6 @@ response.foo { puts 'Not going to work' }
93
109
  # exception => NoMethodError: undefined method `foo'
94
110
  ```
95
111
 
96
- ### Your service API
97
-
98
- Your service can now be used as such:
99
-
100
- ```ruby
101
- MyService.('Some param') do |response|
102
- response.success { puts 'I was successful.' }
103
- response.failure { puts 'I failed.' }
104
- end
105
- ```
106
-
107
- If your service or the response itself restrict the response to a specific set
108
- of states, you can ensure all states have been handled by placing a call
109
- to `unhandled_states` at the end of your response block. This will yield an array of
110
- unhandled states to the given block if there are any unhandled states.
111
-
112
- ```ruby
113
- MyService.('Some param') do |response|
114
- response.success { puts 'I was successful.' }
115
- response.failure { puts 'I failed.' }
116
- response.unhandled_states { |states| raise "You didn't handle #{states.join(', ')}" }
117
- end
118
- ```
119
-
120
112
  ## Contributing
121
113
 
122
114
  1. Fork it
@@ -4,7 +4,7 @@ module ResponseState
4
4
 
5
5
  def initialize(state, message=nil, context=nil, valid_states=nil)
6
6
  @valid_states = Array(valid_states || self.class.class_valid_states)
7
- raise "Invalid state of response: #{state}" unless (@valid_states.empty? || @valid_states.include?(state))
7
+ raise "Invalid state of response: #{state}" unless @valid_states.include? state
8
8
  @state = state
9
9
  @context = context
10
10
  @message = message
@@ -20,7 +20,6 @@ module ResponseState
20
20
  end
21
21
 
22
22
  def unhandled_states
23
- return if valid_states.empty?
24
23
  yield uncalled_states unless uncalled_states.empty?
25
24
  nil
26
25
  end
@@ -30,19 +29,15 @@ module ResponseState
30
29
  end
31
30
 
32
31
  def method_missing(method, *args, &block)
33
- if valid_state? method
34
- called_states << method unless called_states.include? method
35
- yield if method == state
36
- nil
37
- else
38
- super
39
- end
32
+ return super unless valid_state? method
33
+ called_states << method unless called_states.include? method
34
+ yield if method == state
35
+ nil
40
36
  end
41
37
 
42
38
  private
43
39
 
44
40
  def valid_state?(call_state)
45
- return true if valid_states.empty?
46
41
  return true if valid_states.include? call_state
47
42
  raise "Invalid state: #{call_state}"
48
43
  end
@@ -14,7 +14,7 @@ module ResponseState
14
14
  end
15
15
 
16
16
  def call(&block)
17
- yield send_state(:unimplemented, "A ResponseState::Service should implement the call method.\nThe call method should perform the relevant work of the service and yield a ResponseState::Response object.\n")
17
+ fail NotImplementedError, "A ResponseState::Service should implement the call method.\nThe call method should perform the relevant work of the service and yield a ResponseState::Response object.\n"
18
18
  end
19
19
 
20
20
  def send_state(state, message=nil, context=nil, valid_states=nil)
@@ -1,3 +1,3 @@
1
1
  module ResponseState
2
- VERSION = "0.3.0"
2
+ VERSION = '0.4.0'
3
3
  end
@@ -1,10 +1,8 @@
1
1
  require 'spec_helper'
2
-
3
- class MyResponse < ResponseState::Response
4
- valid_states :success, :failure
5
- end
2
+ require 'ostruct'
6
3
 
7
4
  class MyService < ResponseState::Service
5
+ response_states :success, :failure
8
6
  attr_reader :object, :pass
9
7
 
10
8
  def initialize(object, pass)
@@ -20,11 +18,11 @@ class MyService < ResponseState::Service
20
18
 
21
19
  def success_response
22
20
  return unless pass
23
- MyResponse.new(:success, 'Yay! It works.', object)
21
+ send_state :success, 'Yay! It works.', object
24
22
  end
25
23
 
26
24
  def failure_response
27
- MyResponse.new(:failure, 'Boo! It failed.')
25
+ send_state :failure, 'Boo! It failed.'
28
26
  end
29
27
  end
30
28
 
@@ -2,8 +2,11 @@ require 'spec_helper'
2
2
 
3
3
  module ResponseState
4
4
  describe Response do
5
- subject(:response) { Response.new(state) }
5
+ subject(:response) { Response.new(state, message, context, valid_states) }
6
6
  let(:state) { :success }
7
+ let(:message) { nil }
8
+ let(:context) { nil }
9
+ let(:valid_states) { [:success, :failure] }
7
10
  before { Response.instance_variable_set :@class_valid_states, nil }
8
11
 
9
12
  it 'yields when sent :success with a state of :success' do
@@ -15,19 +18,16 @@ module ResponseState
15
18
  end
16
19
 
17
20
  context 'when initialized with a message' do
18
- subject(:response) { Response.new(state, message) }
19
21
  let(:message) { double(:message) }
20
22
 
21
23
  specify { expect(response.message).to eq message }
22
24
 
23
25
  context 'when additionally initialized with a context' do
24
- subject(:response) { Response.new(state, message, context) }
25
26
  let(:context) { double(:context) }
26
27
 
27
28
  specify { expect(response.context).to eq context }
28
29
 
29
30
  context 'when additionally initiaized with a set of valid states' do
30
- subject(:response) { Response.new(state, message, context, valid_states) }
31
31
  let(:valid_states) { [:success, :failure] }
32
32
 
33
33
  it 'yields when sent :success with a state of :success' do
@@ -59,6 +59,7 @@ module ResponseState
59
59
 
60
60
  describe '.valid_states' do
61
61
  context 'when set to an array of states' do
62
+ let(:valid_states) { nil }
62
63
  before { Response.valid_states(:success, :failure) }
63
64
 
64
65
  context 'when the response state is :success' do
@@ -11,7 +11,7 @@ module ResponseState
11
11
 
12
12
  describe '.call' do
13
13
  it 'news up an instance and passes the block on to #call' do
14
- expect { |b| Service.(&b) }.to yield_control
14
+ expect { |b| Service.(&b) }.to raise_error(NotImplementedError, "A ResponseState::Service should implement the call method.\nThe call method should perform the relevant work of the service and yield a ResponseState::Response object.\n")
15
15
  end
16
16
  end
17
17
 
@@ -29,16 +29,8 @@ module ResponseState
29
29
  end
30
30
 
31
31
  describe '#call' do
32
- let(:yielded) { OpenStruct.new(response: nil) }
33
- let(:response) { yielded.response }
34
- before { service.call { |response| yielded.response = response} }
35
-
36
- it 'yields an unimplemented response' do
37
- expect(response.state).to eq :unimplemented
38
- end
39
-
40
- it 'yields with a response indicating instructions' do
41
- expect(response.message).to eq "A ResponseState::Service should implement the call method.\nThe call method should perform the relevant work of the service and yield a ResponseState::Response object.\n"
32
+ it 'raises an error' do
33
+ expect { service.call }.to raise_error(NotImplementedError, "A ResponseState::Service should implement the call method.\nThe call method should perform the relevant work of the service and yield a ResponseState::Response object.\n")
42
34
  end
43
35
  end
44
36
 
@@ -65,6 +57,7 @@ module ResponseState
65
57
 
66
58
  context 'given :success, "a message", {}' do
67
59
  let(:response) { service.send_state(:success, 'a message', {}) }
60
+ before { Service.response_states :success, :failure }
68
61
 
69
62
  it 'has a success state' do
70
63
  expect(response.state).to eq :success
@@ -79,20 +72,13 @@ module ResponseState
79
72
  end
80
73
 
81
74
  it 'has valid_states nil' do
82
- expect(response.valid_states).to eq []
83
- end
84
-
85
- context 'and the service has valid states set' do
86
- before { Service.response_states :success, :failure }
87
-
88
- it 'has valid_states [:success, :failure]' do
89
- expect(response.valid_states).to eq [:success, :failure]
90
- end
75
+ expect(response.valid_states).to eq [:success, :failure]
91
76
  end
92
77
  end
93
78
 
94
79
  context 'given :success, "a message"' do
95
80
  let(:response) { service.send_state(:success, 'a message') }
81
+ before { Service.response_states :success, :failure }
96
82
 
97
83
  it 'has a success state' do
98
84
  expect(response.state).to eq :success
@@ -107,12 +93,13 @@ module ResponseState
107
93
  end
108
94
 
109
95
  it 'has valid_states nil' do
110
- expect(response.valid_states).to eq []
96
+ expect(response.valid_states).to eq [:success, :failure]
111
97
  end
112
98
  end
113
99
 
114
100
  context 'given :success' do
115
101
  let(:response) { service.send_state(:success) }
102
+ before { Service.response_states :success, :failure }
116
103
 
117
104
  it 'has a success state' do
118
105
  expect(response.state).to eq :success
@@ -127,7 +114,7 @@ module ResponseState
127
114
  end
128
115
 
129
116
  it 'has valid_states nil' do
130
- expect(response.valid_states).to eq []
117
+ expect(response.valid_states).to eq [:success, :failure]
131
118
  end
132
119
  end
133
120
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: response_state
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - alexpeachey
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-20 00:00:00.000000000 Z
11
+ date: 2014-08-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler