simple_service 1.4.1 → 2.1.0
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/.travis.yml +1 -2
- data/README.md +111 -123
- data/lib/simple_service.rb +78 -8
- data/lib/simple_service/configuration.rb +9 -0
- data/lib/simple_service/result.rb +56 -0
- data/lib/simple_service/version.rb +1 -1
- data/spec/simple_service_spec.rb +176 -1
- data/spec/support/basic_service.rb +19 -0
- data/spec/support/command_one.rb +21 -0
- data/spec/support/command_two.rb +15 -0
- data/spec/support/looping_service.rb +20 -0
- metadata +12 -28
- data/example/hello_world.rb +0 -29
- data/example/nested_organizer.rb +0 -29
- data/example/nested_services.rb +0 -29
- data/example/override_organizer_call_method.rb +0 -39
- data/lib/simple_service/command.rb +0 -28
- data/lib/simple_service/commands/validates_commands_not_empty.rb +0 -16
- data/lib/simple_service/commands/validates_commands_properly_inherit.rb +0 -24
- data/lib/simple_service/commands/validates_expected_keys.rb +0 -19
- data/lib/simple_service/context.rb +0 -32
- data/lib/simple_service/ensure_organizer_is_valid.rb +0 -22
- data/lib/simple_service/exceptions.rb +0 -10
- data/lib/simple_service/organizer.rb +0 -113
- data/lib/simple_service/service_base.rb +0 -169
- data/spec/lib/command_spec.rb +0 -111
- data/spec/lib/commands/validates_commands_not_empty_spec.rb +0 -28
- data/spec/lib/commands/validates_commands_properly_inherit_spec.rb +0 -34
- data/spec/lib/commands/validates_expected_keys_spec.rb +0 -60
- data/spec/lib/ensure_organizer_is_valid_spec.rb +0 -17
- data/spec/lib/organizer_spec.rb +0 -216
data/spec/lib/command_spec.rb
DELETED
@@ -1,111 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe SimpleService::Command do
|
4
|
-
|
5
|
-
class ValidCommand < SimpleService::Command
|
6
|
-
expects :foo, :bar
|
7
|
-
optional :stuff
|
8
|
-
returns :bar, :baz
|
9
|
-
def call
|
10
|
-
context.merge!(
|
11
|
-
bar: ['modified', self.stuff].compact.join(' '),
|
12
|
-
baz: 'blah'
|
13
|
-
)
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
class InvalidReturnCommand < SimpleService::Command
|
18
|
-
expects :foo
|
19
|
-
returns :foo, :baz
|
20
|
-
def call; true; end
|
21
|
-
end
|
22
|
-
|
23
|
-
|
24
|
-
class CallNotDefinedCommand < SimpleService::Command
|
25
|
-
end
|
26
|
-
|
27
|
-
describe '.call' do
|
28
|
-
|
29
|
-
it 'returns the correct keys when using class method' do
|
30
|
-
expect(
|
31
|
-
ValidCommand.call(foo: 'blah', bar: 'meh')
|
32
|
-
).to eql(bar: 'modified', baz: 'blah')
|
33
|
-
end
|
34
|
-
|
35
|
-
it 'provides optional arguments as getters' do
|
36
|
-
expect(
|
37
|
-
ValidCommand.call(foo: 'blah', bar: 'meh', stuff: 'something')
|
38
|
-
).to eql(bar: 'modified something', baz: 'blah')
|
39
|
-
end
|
40
|
-
|
41
|
-
end
|
42
|
-
|
43
|
-
describe '#call' do
|
44
|
-
|
45
|
-
context 'when #returns is not empty' do
|
46
|
-
it 'returns the correct keys from the context' do
|
47
|
-
expect(
|
48
|
-
ValidCommand.new(foo: 'blah', bar: 'meh').call
|
49
|
-
).to eql(bar: 'modified', baz: 'blah')
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
context 'raises error' do
|
54
|
-
|
55
|
-
it 'when command does not define an call method' do
|
56
|
-
expect {
|
57
|
-
CallNotDefinedCommand.new.call
|
58
|
-
}.to raise_error(SimpleService::CallNotDefinedError)
|
59
|
-
end
|
60
|
-
|
61
|
-
it 'when command attempts to return a key that doesnt exist' do
|
62
|
-
expect {
|
63
|
-
InvalidReturnCommand.new.call
|
64
|
-
}.to raise_error(SimpleService::ReturnKeyError)
|
65
|
-
end
|
66
|
-
|
67
|
-
end
|
68
|
-
|
69
|
-
end
|
70
|
-
|
71
|
-
describe 'context' do
|
72
|
-
|
73
|
-
it 'defines getters for each expected key' do
|
74
|
-
expect(
|
75
|
-
ValidCommand.new(foo: 'blah', bar: 'meh')
|
76
|
-
).to respond_to :foo
|
77
|
-
end
|
78
|
-
|
79
|
-
it 'defines setters for each expected key' do
|
80
|
-
command = ValidCommand.new(foo: 'blah', bar: 'meh')
|
81
|
-
command.foo = 'changed'
|
82
|
-
command.bar = 'changed'
|
83
|
-
|
84
|
-
expect(command.context).to eql({ foo: 'changed', bar: 'changed' })
|
85
|
-
end
|
86
|
-
|
87
|
-
it 'getter updates @context' do
|
88
|
-
command = ValidCommand.new(foo: 'blah', bar: 'meh')
|
89
|
-
command.foo = 'changed'
|
90
|
-
expect(command.context).to eql({ foo: 'changed', bar: 'meh'})
|
91
|
-
end
|
92
|
-
|
93
|
-
end
|
94
|
-
|
95
|
-
describe '.get_expects' do
|
96
|
-
|
97
|
-
it 'returns an array of expected keys' do
|
98
|
-
expect(ValidCommand.get_expects.sort).to eql([:foo, :bar].sort)
|
99
|
-
end
|
100
|
-
|
101
|
-
end
|
102
|
-
|
103
|
-
describe '.get_returns' do
|
104
|
-
|
105
|
-
it 'returns an array of keys to return' do
|
106
|
-
expect(ValidCommand.get_returns.sort).to eql([:baz, :bar].sort)
|
107
|
-
end
|
108
|
-
|
109
|
-
end
|
110
|
-
|
111
|
-
end
|
@@ -1,28 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe SimpleService::ValidatesCommandsNotEmpty do
|
4
|
-
|
5
|
-
class DummyCommand < SimpleService::Command
|
6
|
-
def call; true; end
|
7
|
-
end
|
8
|
-
|
9
|
-
context '#call' do
|
10
|
-
|
11
|
-
it 'raises error when commands are not defined' do
|
12
|
-
expect {
|
13
|
-
SimpleService::ValidatesCommandsNotEmpty.new(provided_commands: nil).call
|
14
|
-
}.to raise_error(
|
15
|
-
SimpleService::OrganizerCommandsNotDefinedError,
|
16
|
-
'This Organizer class does not contain any command definitions'
|
17
|
-
)
|
18
|
-
end
|
19
|
-
|
20
|
-
it 'does not raise error when commands are defined' do
|
21
|
-
expect {
|
22
|
-
SimpleService::ValidatesCommandsNotEmpty.new(provided_commands: [DummyCommand]).call
|
23
|
-
}.to_not raise_error
|
24
|
-
end
|
25
|
-
|
26
|
-
end
|
27
|
-
|
28
|
-
end
|
@@ -1,34 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe SimpleService::ValidatesCommandsProperlyInherit do
|
4
|
-
|
5
|
-
class ValidDummyCommand < SimpleService::Command
|
6
|
-
def call; true; end
|
7
|
-
end
|
8
|
-
|
9
|
-
class InvalidDummyCommand
|
10
|
-
def call; true; end
|
11
|
-
end
|
12
|
-
|
13
|
-
context '#call' do
|
14
|
-
it 'raises error when commands do not inherit from SimpleService::Command' do
|
15
|
-
expect {
|
16
|
-
SimpleService::ValidatesCommandsProperlyInherit.new(
|
17
|
-
provided_commands: [InvalidDummyCommand]
|
18
|
-
).call
|
19
|
-
}.to raise_error(
|
20
|
-
SimpleService::CommandParentClassInvalidError,
|
21
|
-
'InvalidDummyCommand - must inherit from SimpleService::Command'
|
22
|
-
)
|
23
|
-
end
|
24
|
-
|
25
|
-
it 'does not raises error when commands inherit from SimpleService::Command' do
|
26
|
-
expect {
|
27
|
-
SimpleService::ValidatesCommandsProperlyInherit.new(
|
28
|
-
provided_commands: [ValidDummyCommand]
|
29
|
-
).call
|
30
|
-
}.to_not raise_error
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
end
|
@@ -1,60 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe SimpleService::ValidatesExpectedKeys do
|
4
|
-
|
5
|
-
let(:valid_keys) {{
|
6
|
-
expected_keys: [:foo],
|
7
|
-
provided_keys: [:foo],
|
8
|
-
some_other_key: 'blah'
|
9
|
-
}}
|
10
|
-
|
11
|
-
context '#call' do
|
12
|
-
|
13
|
-
let(:with_valid_keys) {
|
14
|
-
SimpleService::ValidatesExpectedKeys.new(valid_keys)
|
15
|
-
}
|
16
|
-
|
17
|
-
let(:with_missing_keys) {
|
18
|
-
_keys = valid_keys.merge(expected_keys: [:foo, :baz])
|
19
|
-
SimpleService::ValidatesExpectedKeys.new(_keys)
|
20
|
-
}
|
21
|
-
|
22
|
-
let(:does_not_expect_any_keys) {
|
23
|
-
_keys = valid_keys.merge(expected_keys: [])
|
24
|
-
SimpleService::ValidatesExpectedKeys.new(_keys)
|
25
|
-
}
|
26
|
-
|
27
|
-
context 'when all arguments are valid' do
|
28
|
-
|
29
|
-
it 'does not raise error' do
|
30
|
-
expect {
|
31
|
-
with_valid_keys.call
|
32
|
-
}.to_not raise_error
|
33
|
-
end
|
34
|
-
|
35
|
-
end
|
36
|
-
|
37
|
-
context 'when there are expected keys missing from provided keys' do
|
38
|
-
|
39
|
-
it 'raises an error' do
|
40
|
-
expect { with_missing_keys.call }.to raise_error(
|
41
|
-
SimpleService::ExpectedKeyError,
|
42
|
-
'keys required by the organizer but not found in the context: baz'
|
43
|
-
)
|
44
|
-
end
|
45
|
-
|
46
|
-
end
|
47
|
-
|
48
|
-
context 'no expected keys are given' do
|
49
|
-
|
50
|
-
it 'does not raise error' do
|
51
|
-
expect {
|
52
|
-
does_not_expect_any_keys.call
|
53
|
-
}.to_not raise_error
|
54
|
-
end
|
55
|
-
|
56
|
-
end
|
57
|
-
|
58
|
-
end
|
59
|
-
|
60
|
-
end
|
@@ -1,17 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe SimpleService::EnsureOrganizerIsValid do
|
4
|
-
|
5
|
-
context '#call' do
|
6
|
-
|
7
|
-
class FooCommand < SimpleService::Command
|
8
|
-
def call; true; end
|
9
|
-
end
|
10
|
-
|
11
|
-
class BadInheritanceCommand
|
12
|
-
def call; true; end
|
13
|
-
end
|
14
|
-
|
15
|
-
end
|
16
|
-
|
17
|
-
end
|
data/spec/lib/organizer_spec.rb
DELETED
@@ -1,216 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe SimpleService::Organizer do
|
4
|
-
|
5
|
-
context 'classes with .expects, .optional, and .returns' do
|
6
|
-
|
7
|
-
class TestCommandOne < SimpleService::Command
|
8
|
-
expects :foo
|
9
|
-
optional :blah
|
10
|
-
returns :foo, :bar
|
11
|
-
def call
|
12
|
-
context.merge!(bar: ['bar', self.blah].compact.join(' '))
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
class TestCommandTwo < SimpleService::Command
|
17
|
-
expects :foo, :bar
|
18
|
-
optional :blarg
|
19
|
-
returns :foo, :bar, :baz
|
20
|
-
def call
|
21
|
-
context.merge!(baz: ['baz', self.blarg].compact.join(' '))
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
class TestOrganizer < SimpleService::Organizer
|
26
|
-
expects :foo
|
27
|
-
optional :blah, :blarg
|
28
|
-
returns :foo, :bar, :baz
|
29
|
-
commands TestCommandOne, TestCommandTwo
|
30
|
-
end
|
31
|
-
|
32
|
-
describe '.call' do
|
33
|
-
it 'returns the correct hash' do
|
34
|
-
expect(
|
35
|
-
TestOrganizer.call(foo: 'foo')
|
36
|
-
).to eql(foo: 'foo', bar: 'bar', baz: 'baz', success: true)
|
37
|
-
end
|
38
|
-
|
39
|
-
it 'returns the correct hash when optional arguments provided' do
|
40
|
-
expect(
|
41
|
-
TestOrganizer.call(foo: 'foo', blah: 'blah', blarg: 'blarg')
|
42
|
-
).to eql(foo: 'foo', bar: 'bar blah', baz: 'baz blarg', success: true)
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
describe '#call' do
|
47
|
-
it 'returns the correct hash' do
|
48
|
-
expect(
|
49
|
-
TestOrganizer.new(foo: 'foo').call
|
50
|
-
).to eql(foo: 'foo', bar: 'bar', baz: 'baz', success: true)
|
51
|
-
end
|
52
|
-
|
53
|
-
it 'accepts string keys in context hash' do
|
54
|
-
expect(
|
55
|
-
TestOrganizer.new('foo' => 'foo').call
|
56
|
-
).to eql(foo: 'foo', bar: 'bar', baz: 'baz', success: true)
|
57
|
-
end
|
58
|
-
|
59
|
-
end
|
60
|
-
|
61
|
-
end
|
62
|
-
|
63
|
-
context 'classes with only expects' do
|
64
|
-
|
65
|
-
class TestCommandThree < SimpleService::Command
|
66
|
-
expects :foo
|
67
|
-
def call
|
68
|
-
context.merge!(bar: 'bar')
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
class TestCommandFour < SimpleService::Command
|
73
|
-
expects :foo, :bar
|
74
|
-
def call
|
75
|
-
context.merge!(baz: 'baz')
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
class TestOrganizerTwo < SimpleService::Organizer
|
80
|
-
expects :foo
|
81
|
-
commands TestCommandThree, TestCommandFour
|
82
|
-
end
|
83
|
-
|
84
|
-
describe '#call' do
|
85
|
-
it 'returns the entire context' do
|
86
|
-
expect(
|
87
|
-
TestOrganizerTwo.new(foo: 'foo', extra: 'extra').call
|
88
|
-
).to eql(foo: 'foo', bar: 'bar', baz: 'baz', extra: 'extra', success: true)
|
89
|
-
end
|
90
|
-
|
91
|
-
end
|
92
|
-
|
93
|
-
end
|
94
|
-
|
95
|
-
context 'service using getters and setters' do
|
96
|
-
|
97
|
-
class GetterSetterCommand < SimpleService::Command
|
98
|
-
expects :foo, :bar
|
99
|
-
returns :baz
|
100
|
-
def call
|
101
|
-
self.baz = self.foo
|
102
|
-
end
|
103
|
-
end
|
104
|
-
|
105
|
-
class GetterSetterOrganizer < SimpleService::Organizer
|
106
|
-
expects :foo, :bar
|
107
|
-
returns :baz
|
108
|
-
commands GetterSetterCommand
|
109
|
-
end
|
110
|
-
|
111
|
-
it 'returns the correct hash' do
|
112
|
-
expect(
|
113
|
-
GetterSetterOrganizer.new(foo: 'baz', bar: 'bar').call
|
114
|
-
).to eql({ baz: 'baz', success: true })
|
115
|
-
end
|
116
|
-
|
117
|
-
describe '.get_expects' do
|
118
|
-
|
119
|
-
it 'returns an array of expected keys' do
|
120
|
-
expect(GetterSetterOrganizer.get_expects.sort).to eql [:bar, :foo]
|
121
|
-
end
|
122
|
-
|
123
|
-
end
|
124
|
-
|
125
|
-
end
|
126
|
-
|
127
|
-
context 'service with command that calls failure!' do
|
128
|
-
|
129
|
-
class FailAndReturnErrorMessage < SimpleService::Command
|
130
|
-
def call
|
131
|
-
failure!('something went wrong and we need to abort')
|
132
|
-
end
|
133
|
-
end
|
134
|
-
|
135
|
-
class ShouldNotRun < SimpleService::Command
|
136
|
-
def call
|
137
|
-
raise 'should not have gotten here'
|
138
|
-
end
|
139
|
-
end
|
140
|
-
|
141
|
-
class FailAndReturn < SimpleService::Organizer
|
142
|
-
returns :something_not_set
|
143
|
-
commands FailAndReturnErrorMessage, ShouldNotRun
|
144
|
-
end
|
145
|
-
|
146
|
-
it 'returns a message' do
|
147
|
-
expect(FailAndReturn.call[:message]).to eql(
|
148
|
-
'something went wrong and we need to abort')
|
149
|
-
end
|
150
|
-
|
151
|
-
it 'returns success as false' do
|
152
|
-
expect(FailAndReturn.call[:success]).to eql(false)
|
153
|
-
end
|
154
|
-
|
155
|
-
it 'does not raise an exception' do
|
156
|
-
expect { FailAndReturn.call }.to_not raise_error
|
157
|
-
end
|
158
|
-
|
159
|
-
end
|
160
|
-
|
161
|
-
context 'service with command that calls success!' do
|
162
|
-
|
163
|
-
class SuccessAndReturnMessage < SimpleService::Command
|
164
|
-
returns :something
|
165
|
-
def call
|
166
|
-
context[:something] = 'yes!'
|
167
|
-
success!('success called prior to all commands being run')
|
168
|
-
end
|
169
|
-
end
|
170
|
-
|
171
|
-
class SuccessCalledThisShouldNotRun < SimpleService::Command
|
172
|
-
def call
|
173
|
-
raise 'should not have gotten here'
|
174
|
-
end
|
175
|
-
end
|
176
|
-
|
177
|
-
class SucceedAndReturn < SimpleService::Organizer
|
178
|
-
returns :something
|
179
|
-
commands SuccessAndReturnMessage, SuccessCalledThisShouldNotRun
|
180
|
-
end
|
181
|
-
|
182
|
-
it 'returns a message' do
|
183
|
-
expect(SucceedAndReturn.call[:message]).to eql(
|
184
|
-
'success called prior to all commands being run')
|
185
|
-
end
|
186
|
-
|
187
|
-
it 'returns success as true' do
|
188
|
-
expect(SucceedAndReturn.call[:success]).to eql(true)
|
189
|
-
end
|
190
|
-
|
191
|
-
it 'does not raise an exception' do
|
192
|
-
expect { SucceedAndReturn.call }.to_not raise_error
|
193
|
-
end
|
194
|
-
end
|
195
|
-
|
196
|
-
context 'when arguments are not a hash' do
|
197
|
-
|
198
|
-
class DoNothingCommand < SimpleService::Command
|
199
|
-
def call
|
200
|
-
'do nothing'
|
201
|
-
end
|
202
|
-
end
|
203
|
-
|
204
|
-
class DoNothingOrganizer < SimpleService::Organizer
|
205
|
-
commands DoNothingCommand
|
206
|
-
end
|
207
|
-
|
208
|
-
it 'raises an error' do
|
209
|
-
expect { DoNothingOrganizer.new('not a hash').call }.to raise_error(
|
210
|
-
SimpleService::InvalidArgumentError,
|
211
|
-
'Hash required as argument, but was given a String'
|
212
|
-
)
|
213
|
-
end
|
214
|
-
end
|
215
|
-
|
216
|
-
end
|