state-handler 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/lib/state-handler/mixing.rb +15 -12
- data/lib/state-handler/version.rb +1 -1
- data/spec/lib/state-handler/mixing_spec.rb +38 -20
- metadata +7 -7
data/Gemfile.lock
CHANGED
data/lib/state-handler/mixing.rb
CHANGED
@@ -24,12 +24,6 @@ module StateHandler
|
|
24
24
|
@blocks[state].call
|
25
25
|
end
|
26
26
|
|
27
|
-
if self.class.patterns
|
28
|
-
self.class.patterns.each do |s, regex|
|
29
|
-
@blocks[s].call if @response.code.to_s =~ regex
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
27
|
if self.class.groups
|
34
28
|
self.class.groups.each do |group, states|
|
35
29
|
@blocks[group].call if states.include?(state) && @blocks[group]
|
@@ -48,7 +42,11 @@ module StateHandler
|
|
48
42
|
end
|
49
43
|
|
50
44
|
def state
|
51
|
-
self.class.
|
45
|
+
patterns = self.class.patterns
|
46
|
+
mapping = self.class.mapping
|
47
|
+
|
48
|
+
mapping.keys.each.find { |state| find_mapped(state) } ||
|
49
|
+
patterns.key(patterns.values.each.find { |regex| @response.code.to_s =~ regex })
|
52
50
|
end
|
53
51
|
|
54
52
|
def find_mapped(state)
|
@@ -63,7 +61,9 @@ module StateHandler
|
|
63
61
|
state = name.to_s.gsub(/\?/, '').to_sym
|
64
62
|
|
65
63
|
if name.to_s.end_with?('?')
|
66
|
-
|
64
|
+
unless self.class.mapping[state] || self.class.patterns[state]
|
65
|
+
raise StateHandler::UnexpectedState, "Got: #{state.inspect}"
|
66
|
+
end
|
67
67
|
find_mapped(state)
|
68
68
|
elsif block_given?
|
69
69
|
@blocks[state] = block
|
@@ -95,10 +95,9 @@ module StateHandler
|
|
95
95
|
end
|
96
96
|
|
97
97
|
def match(regexp)
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
def method_missing
|
98
|
+
state = regexp.values.first
|
99
|
+
self.patterns[state] = regexp.keys.first
|
100
|
+
add_to_group(state)
|
102
101
|
end
|
103
102
|
|
104
103
|
def code(*codes, &block)
|
@@ -119,6 +118,10 @@ module StateHandler
|
|
119
118
|
def create(state, value)
|
120
119
|
raise ArgumentError, "State '#{state}' already defined" if self.mapping[state]
|
121
120
|
self.mapping[state] = value
|
121
|
+
add_to_group(state)
|
122
|
+
end
|
123
|
+
|
124
|
+
def add_to_group(state)
|
122
125
|
if @current_group
|
123
126
|
self.groups[@current_group] ||= []
|
124
127
|
self.groups[@current_group] << state
|
@@ -28,10 +28,16 @@ describe StateHandler::Mixing do
|
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
31
|
+
def create(code, &block)
|
32
|
+
s = OpenStruct.new(:code => code)
|
33
|
+
DuplicateDeclarationResponse.new s
|
34
|
+
DummyResponse.new s, &block
|
35
|
+
end
|
36
|
+
|
31
37
|
describe "#exclude" do
|
32
38
|
it "should call valid block" do
|
33
39
|
expect {
|
34
|
-
|
40
|
+
create(400) do |r|
|
35
41
|
r.ex :bad_params do
|
36
42
|
raise ArgumentError
|
37
43
|
end
|
@@ -41,7 +47,7 @@ describe StateHandler::Mixing do
|
|
41
47
|
|
42
48
|
it "should not call block" do
|
43
49
|
expect {
|
44
|
-
|
50
|
+
create(401) do |r|
|
45
51
|
r.ex :bad_params do
|
46
52
|
raise ArgumentError
|
47
53
|
end
|
@@ -54,7 +60,7 @@ describe StateHandler::Mixing do
|
|
54
60
|
context "when handler not matched" do
|
55
61
|
it "should do nothing" do
|
56
62
|
expect {
|
57
|
-
|
63
|
+
create(4001) { }
|
58
64
|
}.should_not raise_error
|
59
65
|
end
|
60
66
|
end
|
@@ -62,25 +68,19 @@ describe StateHandler::Mixing do
|
|
62
68
|
context "when no handlers" do
|
63
69
|
it "should do nothing" do
|
64
70
|
expect {
|
65
|
-
|
71
|
+
create(401) {}
|
66
72
|
}.should_not raise_error
|
67
73
|
end
|
68
74
|
end
|
69
75
|
end
|
70
76
|
|
71
|
-
subject {
|
72
|
-
DuplicateDeclarationResponse.new(response_struct)
|
73
|
-
DummyResponse.new(response_struct)
|
74
|
-
}
|
75
|
-
|
76
77
|
describe "#exec" do
|
77
|
-
let(:response_struct) { OpenStruct.new(:code => 401) }
|
78
78
|
class ExecException < Exception
|
79
79
|
end
|
80
80
|
|
81
81
|
it "should call block from 'at' notation with multiply states" do
|
82
82
|
expect {
|
83
|
-
|
83
|
+
create(401) do |r|
|
84
84
|
r.at :bad_params, :success, :fake do
|
85
85
|
raise ExecException
|
86
86
|
end
|
@@ -90,11 +90,31 @@ describe StateHandler::Mixing do
|
|
90
90
|
end
|
91
91
|
|
92
92
|
describe "#group" do
|
93
|
-
|
93
|
+
context "when matched value with regex" do
|
94
|
+
it "should call block" do
|
95
|
+
expect {
|
96
|
+
create(500) do |r|
|
97
|
+
r.error do
|
98
|
+
raise ExecException
|
99
|
+
end
|
100
|
+
end
|
101
|
+
}.should raise_error(ExecException)
|
102
|
+
end
|
103
|
+
|
104
|
+
it "should call block from group :errors" do
|
105
|
+
expect {
|
106
|
+
create(500) do |r|
|
107
|
+
r.errors do
|
108
|
+
raise ExecException
|
109
|
+
end
|
110
|
+
end
|
111
|
+
}.should raise_error(ExecException)
|
112
|
+
end
|
113
|
+
end
|
94
114
|
|
95
115
|
it "should call block from excluded group :errors" do
|
96
116
|
expect {
|
97
|
-
|
117
|
+
create(200) do |r|
|
98
118
|
r.ex :errors do
|
99
119
|
raise ExecException
|
100
120
|
end
|
@@ -104,7 +124,7 @@ describe StateHandler::Mixing do
|
|
104
124
|
|
105
125
|
it "should call block mapped to group :success" do
|
106
126
|
expect {
|
107
|
-
|
127
|
+
create(200) do |r|
|
108
128
|
r.success do
|
109
129
|
raise ExecException
|
110
130
|
end
|
@@ -114,10 +134,9 @@ describe StateHandler::Mixing do
|
|
114
134
|
end
|
115
135
|
|
116
136
|
describe "#match" do
|
117
|
-
let(:response_struct) { OpenStruct.new(:code => 500) }
|
118
137
|
it "should call matched block" do
|
119
138
|
expect {
|
120
|
-
|
139
|
+
create(500) do |r|
|
121
140
|
r.bad_params do
|
122
141
|
raise ExecException
|
123
142
|
end
|
@@ -130,10 +149,9 @@ describe StateHandler::Mixing do
|
|
130
149
|
end
|
131
150
|
end
|
132
151
|
|
133
|
-
let(:response_struct) { OpenStruct.new(:code => 401) }
|
134
152
|
it "should call block" do
|
135
153
|
expect {
|
136
|
-
|
154
|
+
create(401) do |r|
|
137
155
|
r.bad_params do
|
138
156
|
raise ExecException
|
139
157
|
end
|
@@ -147,12 +165,12 @@ describe StateHandler::Mixing do
|
|
147
165
|
end
|
148
166
|
|
149
167
|
describe "#codes" do
|
150
|
-
|
168
|
+
subject { create(401) {} }
|
151
169
|
its(:bad_params?) { should be_true }
|
152
170
|
end
|
153
171
|
|
154
172
|
describe "#code" do
|
155
|
-
|
173
|
+
subject { create(200) {} }
|
156
174
|
|
157
175
|
its(:enabled?) { should be_true }
|
158
176
|
its(:false?) { should be_false }
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: state-handler
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2012-05-16 00:00:00.000000000Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
16
|
-
requirement: &
|
16
|
+
requirement: &70191862984340 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70191862984340
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec
|
27
|
-
requirement: &
|
27
|
+
requirement: &70191862983820 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70191862983820
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: timecop
|
38
|
-
requirement: &
|
38
|
+
requirement: &70191862983400 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70191862983400
|
47
47
|
description: State handler per object params
|
48
48
|
email: fatumka@gmail.com
|
49
49
|
executables: []
|