response_state 0.2.1 → 0.3.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: 4e0cea61825ed6f590a0b2510a4a6a0f368e4b93
4
- data.tar.gz: d7e616a314b7073920d2be12b22724a4166f75cf
3
+ metadata.gz: 582d7c3101becc563d9c0696000bc50ab4d3646c
4
+ data.tar.gz: 67cb929a24c3261d6e3c1bc9adf5677d2ae5785e
5
5
  SHA512:
6
- metadata.gz: d530ace92af0e4e98345fbacaf69cb02047dfe6c6f11a2352794796d14b25a55b2badd9cb36cf345c9328c7d58866eb51c8b1b1d37427a44f1e587c14d690944
7
- data.tar.gz: 3cfc434592e2ab21ba32d25f5374cb1cbcaa604f9a8c73cae9597560fe9d7468f7633b3553d860c3ee56ab524050ca55112d871ffcab955128b943e1eef622e6
6
+ metadata.gz: 1bfae51a4b1628e3726a16217cb254abbe0cafcebb142f09920d0b8d2b3ddc8f2e9df2e3607ff22550137f735cf910454d7bc64693a06e3e4a6f80a96ec26939
7
+ data.tar.gz: 97eb8d8a09ea753479fb470ca659e50a64872f0a5b01e86c8749d2480f116cd921ea33ecdf686a67f7a5c2270698d6ddd59bec8ebd87d2cc8b060b1235b10a04
data/README.md CHANGED
@@ -39,6 +39,21 @@ You must implement a `call` method.
39
39
  Your call method should yield with a `ResponseState::Response`.
40
40
  The response can be generated with a helper method `send_state` in your service class.
41
41
 
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.
44
+
45
+ ```ruby
46
+ class MyService < ResponseState::Service
47
+ response_states :success, :failure
48
+
49
+ def call(&block)
50
+ # do some work
51
+ yield send_state(:success)
52
+ end
53
+ end
54
+ ```
55
+
56
+
42
57
  ### Response
43
58
 
44
59
  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
@@ -89,6 +104,19 @@ MyService.('Some param') do |response|
89
104
  end
90
105
  ```
91
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
+
92
120
  ## Contributing
93
121
 
94
122
  1. Fork it
@@ -1,6 +1,6 @@
1
1
  module ResponseState
2
2
  class Response
3
- attr_reader :state, :message, :context, :valid_states
3
+ attr_reader :state, :message, :context, :valid_states, :called_states
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)
@@ -8,6 +8,7 @@ module ResponseState
8
8
  @state = state
9
9
  @context = context
10
10
  @message = message
11
+ @called_states = []
11
12
  end
12
13
 
13
14
  def self.valid_states(*args)
@@ -18,9 +19,21 @@ module ResponseState
18
19
  @class_valid_states
19
20
  end
20
21
 
22
+ def unhandled_states
23
+ return if valid_states.empty?
24
+ yield uncalled_states unless uncalled_states.empty?
25
+ nil
26
+ end
27
+
28
+ def uncalled_states
29
+ valid_states - called_states
30
+ end
31
+
21
32
  def method_missing(method, *args, &block)
22
- if validate_state?(method)
33
+ if valid_state? method
34
+ called_states << method unless called_states.include? method
23
35
  yield if method == state
36
+ nil
24
37
  else
25
38
  super
26
39
  end
@@ -28,9 +41,9 @@ module ResponseState
28
41
 
29
42
  private
30
43
 
31
- def validate_state?(call_state)
44
+ def valid_state?(call_state)
32
45
  return true if valid_states.empty?
33
- return true if valid_states.include?(call_state)
46
+ return true if valid_states.include? call_state
34
47
  raise "Invalid state: #{call_state}"
35
48
  end
36
49
  end
@@ -1,6 +1,14 @@
1
1
  module ResponseState
2
2
  class Service
3
3
 
4
+ def self.response_states(*states)
5
+ @valid_states = Array(states)
6
+ end
7
+
8
+ def self.valid_states
9
+ @valid_states || []
10
+ end
11
+
4
12
  def self.call(*args, &block)
5
13
  self.new(*args).call(&block)
6
14
  end
@@ -10,6 +18,7 @@ module ResponseState
10
18
  end
11
19
 
12
20
  def send_state(state, message=nil, context=nil, valid_states=nil)
21
+ valid_states ||= self.class.valid_states
13
22
  ResponseState::Response.new(state, message, context, valid_states)
14
23
  end
15
24
  end
@@ -1,3 +1,3 @@
1
1
  module ResponseState
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -48,7 +48,7 @@ module ResponseState
48
48
 
49
49
  context 'when initialized with an invalid state' do
50
50
  let(:state) { :foo }
51
-
51
+
52
52
  it 'raises an exception' do
53
53
  expect { response }.to raise_exception('Invalid state of response: foo')
54
54
  end
@@ -79,7 +79,7 @@ module ResponseState
79
79
 
80
80
  context 'when initialized with an invalid state' do
81
81
  subject(:response) { Response.new(:foo) }
82
-
82
+
83
83
  it 'raises an exception' do
84
84
  expect { response }.to raise_exception('Invalid state of response: foo')
85
85
  end
@@ -92,7 +92,30 @@ module ResponseState
92
92
  expect(response.valid_states).to eq [:success, :failure]
93
93
  end
94
94
  end
95
+
96
+ describe '#unhandled_states' do
97
+ subject(:response) { Response.new(:success) }
98
+
99
+ context 'when only one state is called' do
100
+ before { response.success {} }
101
+
102
+ it 'yields' do
103
+ expect { |b| response.unhandled_states(&b) }.to yield_with_args([:failure])
104
+ end
105
+ end
106
+
107
+ context 'when all states are called' do
108
+ before do
109
+ response.success {}
110
+ response.failure {}
111
+ end
112
+
113
+ it 'does not yield' do
114
+ expect { |b| response.unhandled_states(&b) }.not_to yield_control
115
+ end
116
+ end
117
+ end
95
118
  end
96
119
  end
97
120
  end
98
- end
121
+ end
@@ -4,7 +4,10 @@ require 'ostruct'
4
4
  module ResponseState
5
5
  describe Service do
6
6
  subject(:service) { Service.new }
7
- before { Response.instance_variable_set :@class_valid_states, nil }
7
+ before do
8
+ Response.instance_variable_set :@class_valid_states, nil
9
+ Service.instance_variable_set :@valid_states, nil
10
+ end
8
11
 
9
12
  describe '.call' do
10
13
  it 'news up an instance and passes the block on to #call' do
@@ -12,6 +15,19 @@ module ResponseState
12
15
  end
13
16
  end
14
17
 
18
+ describe '.response_states' do
19
+ it 'sets the valid response states' do
20
+ Service.response_states :success, :failure
21
+ expect(Service.valid_states).to eq [:success, :failure]
22
+ end
23
+ end
24
+
25
+ describe '.valid_states' do
26
+ it 'defaults to []' do
27
+ expect(Service.valid_states).to eq []
28
+ end
29
+ end
30
+
15
31
  describe '#call' do
16
32
  let(:yielded) { OpenStruct.new(response: nil) }
17
33
  let(:response) { yielded.response }
@@ -65,6 +81,14 @@ module ResponseState
65
81
  it 'has valid_states nil' do
66
82
  expect(response.valid_states).to eq []
67
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
91
+ end
68
92
  end
69
93
 
70
94
  context 'given :success, "a message"' do
@@ -108,4 +132,4 @@ module ResponseState
108
132
  end
109
133
  end
110
134
  end
111
- end
135
+ end
metadata CHANGED
@@ -1,69 +1,69 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: response_state
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.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-05-21 00:00:00.000000000 Z
11
+ date: 2014-06-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.3'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.3'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: simplecov
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - '>='
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - '>='
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  description: Easy to use response state pattern
@@ -73,8 +73,8 @@ executables: []
73
73
  extensions: []
74
74
  extra_rdoc_files: []
75
75
  files:
76
- - .gitignore
77
- - .rspec
76
+ - ".gitignore"
77
+ - ".rspec"
78
78
  - Gemfile
79
79
  - LICENSE.txt
80
80
  - README.md
@@ -98,12 +98,12 @@ require_paths:
98
98
  - lib
99
99
  required_ruby_version: !ruby/object:Gem::Requirement
100
100
  requirements:
101
- - - '>='
101
+ - - ">="
102
102
  - !ruby/object:Gem::Version
103
103
  version: '0'
104
104
  required_rubygems_version: !ruby/object:Gem::Requirement
105
105
  requirements:
106
- - - '>='
106
+ - - ">="
107
107
  - !ruby/object:Gem::Version
108
108
  version: '0'
109
109
  requirements: []