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/simple_service_spec.rb
CHANGED
@@ -1,5 +1,180 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
+
require_relative 'support/basic_service'
|
3
|
+
require_relative 'support/looping_service'
|
2
4
|
|
3
|
-
describe SimpleService do
|
5
|
+
RSpec.describe SimpleService do
|
4
6
|
|
7
|
+
let(:params) do
|
8
|
+
{
|
9
|
+
foo: 'asdf',
|
10
|
+
bar: 'qwer',
|
11
|
+
command_one_success: true,
|
12
|
+
command_two_success: true,
|
13
|
+
}
|
14
|
+
end
|
15
|
+
|
16
|
+
let(:result) do
|
17
|
+
BasicService.call(params)
|
18
|
+
end
|
19
|
+
|
20
|
+
before do
|
21
|
+
SimpleService.configure do |config|
|
22
|
+
config.verbose_tracking = false
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context '#call is successful' do
|
27
|
+
it 'returns a result object' do
|
28
|
+
expect(result).to be_a SimpleService::Result
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'returns success' do
|
32
|
+
expect(result.success?).to eq true
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'returns the correct result value' do
|
36
|
+
expect(result.value).to eq(
|
37
|
+
combined_foo_bar: 'combined modified FOO modified BAR'
|
38
|
+
)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context '#call that fails on step one' do
|
43
|
+
let(:result) do
|
44
|
+
BasicService.call(params.merge(command_one_success: false))
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'returns a result object' do
|
48
|
+
expect(result).to be_a SimpleService::Result
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'returns failure' do
|
52
|
+
expect(result.success?).to eq false
|
53
|
+
expect(result.failure?).to eq true
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'returns the correct result value' do
|
57
|
+
expect(result.value).to eq(
|
58
|
+
message: 'stuff went wrong with command one'
|
59
|
+
)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
context '#call that fails on step two' do
|
64
|
+
it 'returns the correct result value' do
|
65
|
+
result = BasicService.call(params.merge(command_two_success: false))
|
66
|
+
expect(result.value).to eq(
|
67
|
+
message: 'stuff went wrong with command two'
|
68
|
+
)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
context 'LoopingService' do
|
73
|
+
it 'returns a result object' do
|
74
|
+
expect(LoopingService.call(count: 0)).to be_a SimpleService::Result
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'returns a result object' do
|
78
|
+
expect(LoopingService.call(count: 0).value).to eq(count: 3)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
context 'record commands' do
|
83
|
+
it 'records normal command execution' do
|
84
|
+
expect(result.recorded_commands).to eq(
|
85
|
+
[
|
86
|
+
{
|
87
|
+
class_name: 'BasicService',
|
88
|
+
command_name: :command_one,
|
89
|
+
success: true,
|
90
|
+
},
|
91
|
+
{
|
92
|
+
class_name: 'BasicService',
|
93
|
+
command_name: :command_two,
|
94
|
+
success: true,
|
95
|
+
},
|
96
|
+
{
|
97
|
+
class_name: 'CommandOne',
|
98
|
+
command_name: :modify_foo_bar,
|
99
|
+
success: true,
|
100
|
+
},
|
101
|
+
{
|
102
|
+
class_name: 'CommandTwo',
|
103
|
+
command_name: :combine_foo_bar,
|
104
|
+
success: true,
|
105
|
+
},
|
106
|
+
]
|
107
|
+
)
|
108
|
+
end
|
109
|
+
|
110
|
+
it 'records verbose command execution' do
|
111
|
+
SimpleService.configure do |config|
|
112
|
+
config.verbose_tracking = true
|
113
|
+
end
|
114
|
+
|
115
|
+
expect(result.recorded_commands).to eq(
|
116
|
+
[
|
117
|
+
{
|
118
|
+
class_name: 'BasicService',
|
119
|
+
command_name: :command_one,
|
120
|
+
received_args: {
|
121
|
+
foo: 'asdf',
|
122
|
+
bar: 'qwer',
|
123
|
+
command_one_success: true,
|
124
|
+
command_two_success: true
|
125
|
+
},
|
126
|
+
success: {
|
127
|
+
foo: 'FOO',
|
128
|
+
bar: 'qwer',
|
129
|
+
command_one_success: true,
|
130
|
+
command_two_success: true,
|
131
|
+
},
|
132
|
+
},
|
133
|
+
{
|
134
|
+
class_name: 'BasicService',
|
135
|
+
command_name: :command_two,
|
136
|
+
received_args: {
|
137
|
+
foo: 'FOO',
|
138
|
+
bar: 'qwer',
|
139
|
+
command_one_success: true,
|
140
|
+
command_two_success: true
|
141
|
+
},
|
142
|
+
success: {
|
143
|
+
foo: 'FOO',
|
144
|
+
bar: 'BAR',
|
145
|
+
command_one_success: true,
|
146
|
+
command_two_success: true,
|
147
|
+
},
|
148
|
+
},
|
149
|
+
{
|
150
|
+
class_name: 'CommandOne',
|
151
|
+
command_name: :modify_foo_bar,
|
152
|
+
received_args: {
|
153
|
+
foo: 'FOO',
|
154
|
+
bar: 'BAR',
|
155
|
+
command_one_success: true,
|
156
|
+
command_two_success: true,
|
157
|
+
},
|
158
|
+
success: {
|
159
|
+
modified_foo: 'modified FOO',
|
160
|
+
modified_bar: 'modified BAR',
|
161
|
+
command_two_success: true,
|
162
|
+
},
|
163
|
+
},
|
164
|
+
{
|
165
|
+
class_name: 'CommandTwo',
|
166
|
+
command_name: :combine_foo_bar,
|
167
|
+
received_args: {
|
168
|
+
modified_foo: 'modified FOO',
|
169
|
+
modified_bar: 'modified BAR',
|
170
|
+
command_two_success: true,
|
171
|
+
},
|
172
|
+
success: {
|
173
|
+
combined_foo_bar: 'combined modified FOO modified BAR',
|
174
|
+
},
|
175
|
+
},
|
176
|
+
]
|
177
|
+
)
|
178
|
+
end
|
179
|
+
end
|
5
180
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require_relative 'command_one'
|
2
|
+
require_relative 'command_two'
|
3
|
+
|
4
|
+
class BasicService
|
5
|
+
include SimpleService
|
6
|
+
|
7
|
+
command :command_one
|
8
|
+
command :command_two
|
9
|
+
commands CommandOne, CommandTwo
|
10
|
+
|
11
|
+
def command_one(**kwargs)
|
12
|
+
success(kwargs.merge(foo: 'FOO'))
|
13
|
+
end
|
14
|
+
|
15
|
+
def command_two(**kwargs)
|
16
|
+
success(kwargs.merge(bar: 'BAR'))
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
@@ -0,0 +1,21 @@
|
|
1
|
+
class CommandOne
|
2
|
+
include SimpleService
|
3
|
+
|
4
|
+
command :modify_foo_bar
|
5
|
+
|
6
|
+
def modify_foo_bar(foo:, bar:, command_one_success:, command_two_success:)
|
7
|
+
modified_foo = "modified #{foo}"
|
8
|
+
modified_bar = "modified #{bar}"
|
9
|
+
|
10
|
+
if command_one_success
|
11
|
+
success(
|
12
|
+
modified_foo: modified_foo,
|
13
|
+
modified_bar: modified_bar,
|
14
|
+
command_two_success: command_two_success,
|
15
|
+
)
|
16
|
+
else
|
17
|
+
failure(message: 'stuff went wrong with command one')
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class CommandTwo
|
2
|
+
include SimpleService
|
3
|
+
|
4
|
+
command :combine_foo_bar
|
5
|
+
|
6
|
+
def combine_foo_bar(modified_foo:, modified_bar:, command_two_success:)
|
7
|
+
combined_foo_bar = "combined #{modified_foo} #{modified_bar}"
|
8
|
+
|
9
|
+
if command_two_success
|
10
|
+
success(combined_foo_bar: combined_foo_bar)
|
11
|
+
else
|
12
|
+
failure(message: 'stuff went wrong with command two')
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class LoopingService
|
2
|
+
include SimpleService
|
3
|
+
|
4
|
+
commands :add_one
|
5
|
+
|
6
|
+
def call(kwargs)
|
7
|
+
count = kwargs
|
8
|
+
|
9
|
+
3.times do
|
10
|
+
count = super(count)
|
11
|
+
end
|
12
|
+
|
13
|
+
count
|
14
|
+
end
|
15
|
+
|
16
|
+
def add_one(count:)
|
17
|
+
success(count: count + 1)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_service
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jarrod Spillers
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-10-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -110,30 +110,17 @@ files:
|
|
110
110
|
- LICENSE.txt
|
111
111
|
- README.md
|
112
112
|
- Rakefile
|
113
|
-
- example/hello_world.rb
|
114
|
-
- example/nested_organizer.rb
|
115
|
-
- example/nested_services.rb
|
116
|
-
- example/override_organizer_call_method.rb
|
117
113
|
- lib/simple_service.rb
|
118
|
-
- lib/simple_service/
|
119
|
-
- lib/simple_service/
|
120
|
-
- lib/simple_service/commands/validates_commands_properly_inherit.rb
|
121
|
-
- lib/simple_service/commands/validates_expected_keys.rb
|
122
|
-
- lib/simple_service/context.rb
|
123
|
-
- lib/simple_service/ensure_organizer_is_valid.rb
|
124
|
-
- lib/simple_service/exceptions.rb
|
125
|
-
- lib/simple_service/organizer.rb
|
126
|
-
- lib/simple_service/service_base.rb
|
114
|
+
- lib/simple_service/configuration.rb
|
115
|
+
- lib/simple_service/result.rb
|
127
116
|
- lib/simple_service/version.rb
|
128
117
|
- simple_service.gemspec
|
129
|
-
- spec/lib/command_spec.rb
|
130
|
-
- spec/lib/commands/validates_commands_not_empty_spec.rb
|
131
|
-
- spec/lib/commands/validates_commands_properly_inherit_spec.rb
|
132
|
-
- spec/lib/commands/validates_expected_keys_spec.rb
|
133
|
-
- spec/lib/ensure_organizer_is_valid_spec.rb
|
134
|
-
- spec/lib/organizer_spec.rb
|
135
118
|
- spec/simple_service_spec.rb
|
136
119
|
- spec/spec_helper.rb
|
120
|
+
- spec/support/basic_service.rb
|
121
|
+
- spec/support/command_one.rb
|
122
|
+
- spec/support/command_two.rb
|
123
|
+
- spec/support/looping_service.rb
|
137
124
|
homepage: https://github.com/jspillers/simple_service
|
138
125
|
licenses:
|
139
126
|
- MIT
|
@@ -160,12 +147,9 @@ specification_version: 4
|
|
160
147
|
summary: A minimal service object composer with support for individual commands and
|
161
148
|
top level organizer objects
|
162
149
|
test_files:
|
163
|
-
- spec/lib/command_spec.rb
|
164
|
-
- spec/lib/commands/validates_commands_not_empty_spec.rb
|
165
|
-
- spec/lib/commands/validates_commands_properly_inherit_spec.rb
|
166
|
-
- spec/lib/commands/validates_expected_keys_spec.rb
|
167
|
-
- spec/lib/ensure_organizer_is_valid_spec.rb
|
168
|
-
- spec/lib/organizer_spec.rb
|
169
150
|
- spec/simple_service_spec.rb
|
170
151
|
- spec/spec_helper.rb
|
171
|
-
|
152
|
+
- spec/support/basic_service.rb
|
153
|
+
- spec/support/command_one.rb
|
154
|
+
- spec/support/command_two.rb
|
155
|
+
- spec/support/looping_service.rb
|
data/example/hello_world.rb
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'simple_service'
|
3
|
-
|
4
|
-
class ConcatName < SimpleService::Command
|
5
|
-
expects :first_name, :last_name
|
6
|
-
returns :name
|
7
|
-
|
8
|
-
def call
|
9
|
-
self.name = "#{first_name} #{last_name}"
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
class CreateHelloString < SimpleService::Command
|
14
|
-
expects :name
|
15
|
-
returns :hello
|
16
|
-
|
17
|
-
def call
|
18
|
-
self.hello = "#{name}, say hello world!"
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
class SayHello < SimpleService::Organizer
|
23
|
-
expects :first_name, :last_name
|
24
|
-
returns :hello
|
25
|
-
commands ConcatName, CreateHelloString
|
26
|
-
end
|
27
|
-
|
28
|
-
result = SayHello.new(first_name: 'Ruby', last_name: 'Gem').call
|
29
|
-
puts result[:hello]
|
data/example/nested_organizer.rb
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'simple_service'
|
3
|
-
|
4
|
-
class Increment < SimpleService::Command
|
5
|
-
expects :counter
|
6
|
-
returns :counter
|
7
|
-
|
8
|
-
def call
|
9
|
-
self.counter += 1
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
class IncrementCounter < SimpleService::Organizer
|
14
|
-
expects :counter
|
15
|
-
returns :counter
|
16
|
-
commands Increment, Increment, Increment
|
17
|
-
end
|
18
|
-
|
19
|
-
class ReallyIncrementThatCounter < SimpleService::Organizer
|
20
|
-
expects :counter
|
21
|
-
returns :counter
|
22
|
-
commands IncrementCounter, IncrementCounter, IncrementCounter
|
23
|
-
end
|
24
|
-
|
25
|
-
result = IncrementCounter.new(counter: 0).call
|
26
|
-
puts "IncrementCounter: #{result[:counter]}"
|
27
|
-
|
28
|
-
result = ReallyIncrementThatCounter.new(counter: 0).call
|
29
|
-
puts "ReallyIncrementThatCounter: #{result[:counter]}"
|
data/example/nested_services.rb
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'simple_service'
|
3
|
-
|
4
|
-
class Increment < SimpleService::Command
|
5
|
-
expects :counter
|
6
|
-
returns :counter
|
7
|
-
|
8
|
-
def execute
|
9
|
-
self.counter += 1
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
class IncrementCounter < SimpleService::Organizer
|
14
|
-
expects :counter
|
15
|
-
returns :counter
|
16
|
-
commands Increment, Increment, Increment
|
17
|
-
end
|
18
|
-
|
19
|
-
class ReallyIncrementThatCounter < SimpleService::Organizer
|
20
|
-
expects :counter
|
21
|
-
returns :counter
|
22
|
-
commands IncrementCounter, IncrementCounter, IncrementCounter
|
23
|
-
end
|
24
|
-
|
25
|
-
result = IncrementCounter.new(counter: 0).execute
|
26
|
-
puts "IncrementCounter: #{result[:counter]}"
|
27
|
-
|
28
|
-
result = ReallyIncrementThatCounter.new(counter: 0).execute
|
29
|
-
puts "ReallyIncrementThatCounter: #{result[:counter]}"
|
@@ -1,39 +0,0 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'simple_service'
|
3
|
-
|
4
|
-
class ConcatAnotherName < SimpleService::Command
|
5
|
-
expects :first_name, :last_name
|
6
|
-
returns :name
|
7
|
-
|
8
|
-
def call
|
9
|
-
self.name = "#{first_name} #{last_name}"
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
class CreateAnotherHelloString < SimpleService::Command
|
14
|
-
expects :name
|
15
|
-
returns :hello
|
16
|
-
|
17
|
-
def call
|
18
|
-
self.hello ||= ''
|
19
|
-
self.hello += "#{name}, say hello world!"
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
class SayHelloMultipleTimes < SimpleService::Organizer
|
24
|
-
expects :first_name, :last_name, :num_times
|
25
|
-
returns :hello
|
26
|
-
commands ConcatAnotherName, CreateAnotherHelloString
|
27
|
-
|
28
|
-
# overriding the #call method on the organizer allows
|
29
|
-
# you to do things like loop and call the service commands
|
30
|
-
# multiple times
|
31
|
-
def call
|
32
|
-
num_times.times do
|
33
|
-
super
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
result = SayHelloMultipleTimes.new(first_name: 'Ruby', last_name: 'Gem', num_times: 3).call
|
39
|
-
puts result[:hello]
|