simple-service 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +4 -0
- data/.tm_properties +1 -1
- data/Makefile +12 -0
- data/README.md +2 -0
- data/VERSION +1 -1
- data/lib/simple/service.rb +66 -51
- data/lib/simple/service/action.rb +95 -94
- data/lib/simple/service/action/comment.rb +1 -1
- data/lib/simple/service/action/method_reflection.rb +2 -2
- data/lib/simple/service/action/parameter.rb +2 -6
- data/lib/simple/service/context.rb +69 -27
- data/lib/simple/service/errors.rb +54 -0
- data/lib/simple/service/version.rb +7 -2
- data/scripts/stats +2 -1
- data/spec/simple/service/action_invoke2_spec.rb +166 -0
- data/spec/simple/service/action_invoke_spec.rb +266 -0
- data/spec/simple/service/action_spec.rb +51 -0
- data/spec/simple/service/context_spec.rb +39 -7
- data/spec/simple/service/service_spec.rb +105 -0
- data/spec/simple/service/version_spec.rb +7 -0
- data/spec/spec_helper.rb +15 -2
- data/spec/support/spec_services.rb +50 -0
- metadata +15 -5
- data/lib/simple.rb +0 -2
- data/spec/support/004_simplecov.rb +0 -13
@@ -0,0 +1,51 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Simple::Service::Action do
|
4
|
+
let(:service) { SpecService }
|
5
|
+
|
6
|
+
let(:service1) { ::Simple::Service.action(service, :service1) }
|
7
|
+
let(:service2) { ::Simple::Service.action(service, :service2) }
|
8
|
+
let(:service3) { ::Simple::Service.action(service, :service3) }
|
9
|
+
|
10
|
+
describe "attributes" do
|
11
|
+
describe "#service" do
|
12
|
+
it "returns the service module" do
|
13
|
+
expect(service1.service).to eq(service)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "#name" do
|
18
|
+
it "returns the action name" do
|
19
|
+
expect(service1.name).to eq(:service1)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "full_name" do
|
24
|
+
it "returns the full_name" do
|
25
|
+
expect(service1.full_name).to eq("SpecService#service1")
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "to_s" do
|
30
|
+
it "returns the full_name" do
|
31
|
+
expect(service1.to_s).to eq("SpecService#service1")
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe "#short_description" do
|
36
|
+
it "returns the short_description as per source code" do
|
37
|
+
expect(service1.short_description).to eq("This is service1")
|
38
|
+
expect(service2.short_description).to eq("This is service2 (no full description)")
|
39
|
+
expect(service3.short_description).to eq(nil)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe "#full_description" do
|
44
|
+
it "returns the full_description as per source code" do
|
45
|
+
expect(service1.full_description).to eq("Service 1 has a full description")
|
46
|
+
expect(service2.full_description).to eq(nil)
|
47
|
+
expect(service3.full_description).to eq(nil)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -1,13 +1,47 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
|
+
describe Simple::Service do
|
4
|
+
describe ".with_context" do
|
5
|
+
it "merges the current context for the duration of the block" do
|
6
|
+
block_called = false
|
7
|
+
|
8
|
+
Simple::Service.with_context(a: "a") do
|
9
|
+
expect(Simple::Service.context.a).to eq("a")
|
10
|
+
|
11
|
+
# overwrite value
|
12
|
+
Simple::Service.with_context(a: "b") do
|
13
|
+
expect(Simple::Service.context.a).to eq("b")
|
14
|
+
block_called = true
|
15
|
+
end
|
16
|
+
|
17
|
+
# overwrite value w/nil
|
18
|
+
Simple::Service.with_context(a: nil) do
|
19
|
+
expect(Simple::Service.context.a).to be_nil
|
20
|
+
Simple::Service.context.a = "c"
|
21
|
+
expect(Simple::Service.context.a).to eq("c")
|
22
|
+
end
|
23
|
+
expect(Simple::Service.context.a).to eq("a")
|
24
|
+
end
|
25
|
+
|
26
|
+
expect(block_called).to eq(true)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
3
31
|
describe Simple::Service::Context do
|
4
32
|
let(:context) { Simple::Service::Context.new }
|
5
|
-
|
33
|
+
|
6
34
|
before do
|
7
35
|
context.one = 1
|
8
36
|
end
|
9
37
|
|
10
|
-
describe "
|
38
|
+
describe "invalid identifier" do
|
39
|
+
it "raises an error" do
|
40
|
+
expect { context.one! }.to raise_error(NoMethodError)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe "context reading" do
|
11
45
|
it "returns a value if set" do
|
12
46
|
expect(context.one).to eq(1)
|
13
47
|
end
|
@@ -17,16 +51,14 @@ describe Simple::Service::Context do
|
|
17
51
|
end
|
18
52
|
end
|
19
53
|
|
20
|
-
describe "writing" do
|
54
|
+
describe "context writing" do
|
21
55
|
it "sets a value if it does not exist yet" do
|
22
|
-
context.two = 2
|
56
|
+
context.two = 2
|
23
57
|
expect(context.two).to eq(2)
|
24
58
|
end
|
25
59
|
|
26
60
|
it "raises a ReadOnly error if the value exists and is not equal" do
|
27
|
-
expect {
|
28
|
-
context.one = 2
|
29
|
-
}.to raise_error(::Simple::Service::Context::ReadOnlyError)
|
61
|
+
expect { context.one = 2 }.to raise_error(::Simple::Service::ContextReadOnlyError)
|
30
62
|
end
|
31
63
|
|
32
64
|
it "sets the value if it exists and is equal" do
|
@@ -0,0 +1,105 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe "Simple::Service" do
|
4
|
+
context "when running against a NoService module" do
|
5
|
+
let(:service) { NoServiceModule }
|
6
|
+
|
7
|
+
describe ".service?" do
|
8
|
+
it "returns false on a NoService module" do
|
9
|
+
expect(Simple::Service.service?(service)).to eq(false)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe ".actions" do
|
14
|
+
it "raises an argument error" do
|
15
|
+
expect { Simple::Service.actions(service) }.to raise_error(ArgumentError)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe ".action" do
|
20
|
+
it "raises an argument error" do
|
21
|
+
expect { Simple::Service.action(service, :service1) }.to raise_error(ArgumentError)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe ".invoke" do
|
26
|
+
it "raises an argument error" do
|
27
|
+
::Simple::Service.with_context do
|
28
|
+
expect { Simple::Service.invoke(service, :service1, {}, {}, context: nil) }.to raise_error(::ArgumentError)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# running against a proper service module
|
35
|
+
let(:service) { SpecService }
|
36
|
+
|
37
|
+
describe ".service?" do
|
38
|
+
it "returns true" do
|
39
|
+
expect(Simple::Service.service?(service)).to eq(true)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe ".actions" do
|
44
|
+
it "returns a Hash of actions on a Service module" do
|
45
|
+
actions = Simple::Service.actions(service)
|
46
|
+
expect(actions).to be_a(Hash)
|
47
|
+
expect(actions.keys).to contain_exactly(:service1, :service2, :service3)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe ".action" do
|
52
|
+
context "with an existing name" do
|
53
|
+
it "returns a Action object" do
|
54
|
+
action = Simple::Service.action(service, :service1)
|
55
|
+
expect(action.service).to eq(service)
|
56
|
+
expect(action.name).to eq(:service1)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
describe "Simple::Service::NoSuchAction" do
|
61
|
+
it "does not inherit from Simple::Service::ArgumentError" do
|
62
|
+
expect(Simple::Service::NoSuchAction < Simple::Service::ArgumentError).to be_falsey
|
63
|
+
end
|
64
|
+
|
65
|
+
it "inherits from ArgumentError" do
|
66
|
+
expect(Simple::Service::NoSuchAction < ::ArgumentError).to eq true
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
context "with an unknown name" do
|
71
|
+
it "raises a NoSuchAction error" do
|
72
|
+
expect do
|
73
|
+
Simple::Service.action(service, :no_such_service)
|
74
|
+
end.to raise_error(Simple::Service::NoSuchAction, /No such action :no_such_service/)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
describe ".invoke" do
|
80
|
+
let(:positionals) { { a: "my_a", b: "my_b" } }
|
81
|
+
let(:named) { { d: "my_d" } }
|
82
|
+
|
83
|
+
context "when context is not set" do
|
84
|
+
it "raises a ContextMissingError" do
|
85
|
+
action = Simple::Service.actions(service)[:service1]
|
86
|
+
expect(action).not_to receive(:invoke)
|
87
|
+
|
88
|
+
expect do
|
89
|
+
Simple::Service.invoke(service, :service1, *positionals, named_args: named)
|
90
|
+
end.to raise_error(::Simple::Service::ContextMissingError)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
context "when context is not set" do
|
95
|
+
it "properly delegates call to action object" do
|
96
|
+
action = Simple::Service.actions(service)[:service1]
|
97
|
+
expect(action).to receive(:invoke).with(*positionals, named_args: named)
|
98
|
+
|
99
|
+
::Simple::Service.with_context do
|
100
|
+
Simple::Service.invoke(service, :service1, *positionals, named_args: named)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -4,10 +4,23 @@ ENV["RAILS_ENV"] = "test"
|
|
4
4
|
require "byebug"
|
5
5
|
require "rspec"
|
6
6
|
|
7
|
-
|
7
|
+
require "simplecov"
|
8
|
+
|
9
|
+
SimpleCov.start do
|
10
|
+
# return true to remove src from coverage
|
11
|
+
add_filter do |src|
|
12
|
+
next true if src.filename =~ /\/spec\//
|
13
|
+
|
14
|
+
false
|
15
|
+
end
|
16
|
+
|
17
|
+
# minimum_coverage 90
|
18
|
+
end
|
8
19
|
|
9
20
|
require "simple/service"
|
10
21
|
|
22
|
+
Dir.glob("./spec/support/**/*.rb").sort.each { |path| load path }
|
23
|
+
|
11
24
|
RSpec.configure do |config|
|
12
25
|
config.run_all_when_everything_filtered = true
|
13
26
|
config.filter_run focus: (ENV["CI"] != "true")
|
@@ -16,8 +29,8 @@ RSpec.configure do |config|
|
|
16
29
|
config.example_status_persistence_file_path = ".rspec.data"
|
17
30
|
|
18
31
|
config.backtrace_exclusion_patterns << /spec\/support/
|
32
|
+
config.backtrace_exclusion_patterns << /spec\/helpers/
|
19
33
|
config.backtrace_exclusion_patterns << /spec_helper/
|
20
|
-
config.backtrace_exclusion_patterns << /database_cleaner/
|
21
34
|
|
22
35
|
# config.around(:each) do |example|
|
23
36
|
# example.run
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# rubocop:disable Naming/UncommunicativeMethodParamName
|
2
|
+
|
3
|
+
module NoServiceModule
|
4
|
+
end
|
5
|
+
|
6
|
+
module SpecService
|
7
|
+
include Simple::Service
|
8
|
+
|
9
|
+
# This is service1
|
10
|
+
#
|
11
|
+
# Service 1 has a full description
|
12
|
+
def service1(a, b, c = "speed-of-light", d:, e: 2.781); end
|
13
|
+
|
14
|
+
# This is service2 (no full description)
|
15
|
+
def service2
|
16
|
+
"service2 return"
|
17
|
+
end
|
18
|
+
|
19
|
+
def service3
|
20
|
+
nil
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def not_a_service; end
|
26
|
+
end
|
27
|
+
|
28
|
+
module InvokeTestService
|
29
|
+
include Simple::Service
|
30
|
+
|
31
|
+
def no_params
|
32
|
+
"service2 return"
|
33
|
+
end
|
34
|
+
|
35
|
+
def positional_params(a, b, c = "speed-of-light", e = 2.781)
|
36
|
+
[a, b, c, e]
|
37
|
+
end
|
38
|
+
|
39
|
+
def named_params(a:, b:, c: "speed-of-light", e: 2.781)
|
40
|
+
[a, b, c, e]
|
41
|
+
end
|
42
|
+
|
43
|
+
def mixed_optional_params(a, b = "default-b", c = "speed-of-light", e: 2.781)
|
44
|
+
[a, b, c, e]
|
45
|
+
end
|
46
|
+
|
47
|
+
def variadic_params(a, b = "queen bee", *args, e: 2.781)
|
48
|
+
[a, b, args, e]
|
49
|
+
end
|
50
|
+
end
|
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: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- radiospiel
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-11-
|
11
|
+
date: 2019-11-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: expectation
|
@@ -43,13 +43,13 @@ files:
|
|
43
43
|
- bin/rake
|
44
44
|
- bin/rspec
|
45
45
|
- lib/simple-service.rb
|
46
|
-
- lib/simple.rb
|
47
46
|
- lib/simple/service.rb
|
48
47
|
- lib/simple/service/action.rb
|
49
48
|
- lib/simple/service/action/comment.rb
|
50
49
|
- lib/simple/service/action/method_reflection.rb
|
51
50
|
- lib/simple/service/action/parameter.rb
|
52
51
|
- lib/simple/service/context.rb
|
52
|
+
- lib/simple/service/errors.rb
|
53
53
|
- lib/simple/service/version.rb
|
54
54
|
- log/.gitkeep
|
55
55
|
- scripts/release
|
@@ -57,9 +57,14 @@ files:
|
|
57
57
|
- scripts/stats
|
58
58
|
- scripts/watch
|
59
59
|
- simple-service.gemspec
|
60
|
+
- spec/simple/service/action_invoke2_spec.rb
|
61
|
+
- spec/simple/service/action_invoke_spec.rb
|
62
|
+
- spec/simple/service/action_spec.rb
|
60
63
|
- spec/simple/service/context_spec.rb
|
64
|
+
- spec/simple/service/service_spec.rb
|
65
|
+
- spec/simple/service/version_spec.rb
|
61
66
|
- spec/spec_helper.rb
|
62
|
-
- spec/support/
|
67
|
+
- spec/support/spec_services.rb
|
63
68
|
homepage: http://github.com/radiospiel/simple-service
|
64
69
|
licenses: []
|
65
70
|
metadata: {}
|
@@ -83,6 +88,11 @@ signing_key:
|
|
83
88
|
specification_version: 4
|
84
89
|
summary: Pretty simple and somewhat abstract service description
|
85
90
|
test_files:
|
91
|
+
- spec/simple/service/action_invoke2_spec.rb
|
92
|
+
- spec/simple/service/action_invoke_spec.rb
|
93
|
+
- spec/simple/service/action_spec.rb
|
86
94
|
- spec/simple/service/context_spec.rb
|
95
|
+
- spec/simple/service/service_spec.rb
|
96
|
+
- spec/simple/service/version_spec.rb
|
87
97
|
- spec/spec_helper.rb
|
88
|
-
- spec/support/
|
98
|
+
- spec/support/spec_services.rb
|
data/lib/simple.rb
DELETED