simple_service 1.3.1 → 1.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/simple_service/organizer.rb +30 -1
- data/lib/simple_service/service_base.rb +16 -4
- data/lib/simple_service/version.rb +1 -1
- data/spec/lib/command_spec.rb +18 -0
- data/spec/lib/organizer_spec.rb +10 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7e5f8250d7c8ab14b2d10bea01d8c411f45971b3
|
4
|
+
data.tar.gz: 142559ae98f856791cd1a42abaf2a6159fc74f0b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5993861fe94469b479220a9621d331381878e709108be079127d39e83062344fb08d390b62983cae2390988d4a864c24051cbe9fe9f242dbeed035a0bb073a68
|
7
|
+
data.tar.gz: 8041a351c27b68708f3e2bb9daf1aa6d392224d0bfd60d7a45847cc0a49cd257d2fe41d045fcef37cb44caac5c15339c4d2cca65f3f9893e987094eeaaedb122
|
@@ -25,8 +25,25 @@ module SimpleService
|
|
25
25
|
def call
|
26
26
|
with_validation do |_commands|
|
27
27
|
_commands.each do |command|
|
28
|
+
|
29
|
+
# halt further command calls if success has been set to false
|
30
|
+
# in a previously called command
|
28
31
|
break if context[:success] == false
|
29
|
-
|
32
|
+
|
33
|
+
# if command class defines "expects" then only feed the command
|
34
|
+
# those keys, otherwise just give it the entire context
|
35
|
+
_context = if command.get_expects.any?
|
36
|
+
{}.tap do |c|
|
37
|
+
command.get_expects.each {|key| c[key] = context[key] }
|
38
|
+
end
|
39
|
+
else
|
40
|
+
context
|
41
|
+
end
|
42
|
+
|
43
|
+
# instantiate and call the command
|
44
|
+
new_context = command.new(_context).call
|
45
|
+
|
46
|
+
# update the master context with the results of the command
|
30
47
|
@context.merge!(new_context)
|
31
48
|
end
|
32
49
|
end
|
@@ -40,6 +57,10 @@ module SimpleService
|
|
40
57
|
|
41
58
|
private
|
42
59
|
|
60
|
+
def failed?
|
61
|
+
!successful?
|
62
|
+
end
|
63
|
+
|
43
64
|
def successful?
|
44
65
|
context[:success] == true
|
45
66
|
end
|
@@ -47,10 +68,18 @@ module SimpleService
|
|
47
68
|
def with_validation
|
48
69
|
add_validation_keys_to_context unless skip_validation
|
49
70
|
|
71
|
+
# ensure that the organizer and commands are setup correctly
|
72
|
+
# by injecting an internal service organizer into the stack of
|
73
|
+
# commands to be executed. Only include this if skip_validation is
|
74
|
+
# not set. Since both user defined and internal services use this code
|
75
|
+
# the skip_validation avoids an infinite loop
|
50
76
|
_commands = skip_validation ? commands : [EnsureOrganizerIsValid] + commands
|
51
77
|
|
52
78
|
yield(_commands)
|
53
79
|
|
80
|
+
# cleanup context keys that are used by the validation so the final
|
81
|
+
# return is clean even if the user does not define "returns" in their
|
82
|
+
# organizer
|
54
83
|
remove_validation_keys_from_context unless skip_validation
|
55
84
|
end
|
56
85
|
|
@@ -6,10 +6,18 @@ module SimpleService
|
|
6
6
|
@expects = args
|
7
7
|
end
|
8
8
|
|
9
|
+
def get_expects
|
10
|
+
@expects || []
|
11
|
+
end
|
12
|
+
|
9
13
|
def returns(*args)
|
10
14
|
@returns = args
|
11
15
|
end
|
12
16
|
|
17
|
+
def get_returns
|
18
|
+
@returns || []
|
19
|
+
end
|
20
|
+
|
13
21
|
def skip_validation(skip=true)
|
14
22
|
@skip_validation = skip
|
15
23
|
end
|
@@ -57,7 +65,7 @@ module SimpleService
|
|
57
65
|
|
58
66
|
# only automatically set context[:success] on Organizers and only if its not already set
|
59
67
|
# by a command calling #failure!
|
60
|
-
if !_context.has_key?(:success) &&
|
68
|
+
if !_context.has_key?(:success) && organizer?
|
61
69
|
_context[:success] = true
|
62
70
|
end
|
63
71
|
|
@@ -65,7 +73,7 @@ module SimpleService
|
|
65
73
|
end
|
66
74
|
|
67
75
|
def find_specified_return_keys
|
68
|
-
if returns.nil? || returns.empty?
|
76
|
+
if returns.nil? || returns.empty? || (organizer? && failed?)
|
69
77
|
context
|
70
78
|
else
|
71
79
|
returns.inject({}) do |to_return, return_param|
|
@@ -82,11 +90,11 @@ module SimpleService
|
|
82
90
|
end
|
83
91
|
|
84
92
|
def expects
|
85
|
-
self.class.
|
93
|
+
self.class.get_expects
|
86
94
|
end
|
87
95
|
|
88
96
|
def returns
|
89
|
-
self.class.
|
97
|
+
self.class.get_returns
|
90
98
|
end
|
91
99
|
|
92
100
|
def skip_validation
|
@@ -97,6 +105,10 @@ module SimpleService
|
|
97
105
|
(expects + returns + ['message', 'success']).uniq
|
98
106
|
end
|
99
107
|
|
108
|
+
def organizer?
|
109
|
+
self.class.ancestors.include?(SimpleService::Organizer)
|
110
|
+
end
|
111
|
+
|
100
112
|
def failure!(message = nil)
|
101
113
|
context[:success] = false
|
102
114
|
context[:message] = message || 'There was a problem'
|
data/spec/lib/command_spec.rb
CHANGED
@@ -24,11 +24,13 @@ describe SimpleService::Command do
|
|
24
24
|
end
|
25
25
|
|
26
26
|
describe '.call' do
|
27
|
+
|
27
28
|
it 'returns the correct keys when using class method' do
|
28
29
|
expect(
|
29
30
|
ValidCommand.call(foo: 'blah', bar: 'meh')
|
30
31
|
).to eql(bar: 'modified', baz: 'blah')
|
31
32
|
end
|
33
|
+
|
32
34
|
end
|
33
35
|
|
34
36
|
describe '#call' do
|
@@ -83,4 +85,20 @@ describe SimpleService::Command do
|
|
83
85
|
|
84
86
|
end
|
85
87
|
|
88
|
+
describe '.get_expects' do
|
89
|
+
|
90
|
+
it 'returns an array of expected keys' do
|
91
|
+
expect(ValidCommand.get_expects.sort).to eql([:foo, :bar].sort)
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
|
96
|
+
describe '.get_returns' do
|
97
|
+
|
98
|
+
it 'returns an array of keys to return' do
|
99
|
+
expect(ValidCommand.get_returns.sort).to eql([:baz, :bar].sort)
|
100
|
+
end
|
101
|
+
|
102
|
+
end
|
103
|
+
|
86
104
|
end
|
data/spec/lib/organizer_spec.rb
CHANGED
@@ -105,9 +105,17 @@ describe SimpleService::Organizer do
|
|
105
105
|
).to eql({ baz: 'baz', success: true })
|
106
106
|
end
|
107
107
|
|
108
|
+
describe '.get_expects' do
|
109
|
+
|
110
|
+
it 'returns an array of expected keys' do
|
111
|
+
expect(GetterSetterOrganizer.get_expects.sort).to eql [:bar, :foo]
|
112
|
+
end
|
113
|
+
|
114
|
+
end
|
115
|
+
|
108
116
|
end
|
109
117
|
|
110
|
-
context 'service with command that calls
|
118
|
+
context 'service with command that calls failure!' do
|
111
119
|
|
112
120
|
class FailAndReturnErrorMessage < SimpleService::Command
|
113
121
|
def call
|
@@ -122,6 +130,7 @@ describe SimpleService::Organizer do
|
|
122
130
|
end
|
123
131
|
|
124
132
|
class FailAndReturn < SimpleService::Organizer
|
133
|
+
returns :something_not_set
|
125
134
|
commands FailAndReturnErrorMessage, ShouldNotRun
|
126
135
|
end
|
127
136
|
|