simple-service 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
@@ -0,0 +1,69 @@
1
+ require "spec_helper"
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
+
31
+ describe Simple::Service::Context do
32
+ let(:context) { Simple::Service::Context.new }
33
+
34
+ before do
35
+ context.one = 1
36
+ end
37
+
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
45
+ it "returns a value if set" do
46
+ expect(context.one).to eq(1)
47
+ end
48
+
49
+ it "returns nil if not set" do
50
+ expect(context.two).to be_nil
51
+ end
52
+ end
53
+
54
+ describe "context writing" do
55
+ it "sets a value if it does not exist yet" do
56
+ context.two = 2
57
+ expect(context.two).to eq(2)
58
+ end
59
+
60
+ it "raises a ReadOnly error if the value exists and is not equal" do
61
+ expect { context.one = 2 }.to raise_error(::Simple::Service::ContextReadOnlyError)
62
+ end
63
+
64
+ it "sets the value if it exists and is equal" do
65
+ context.one = 1
66
+ expect(context.one).to eq(1)
67
+ end
68
+ end
69
+ end
@@ -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
@@ -0,0 +1,7 @@
1
+ require "spec_helper"
2
+
3
+ describe "Simple::Service::VERSION" do
4
+ it "returns a version number" do
5
+ expect(Simple::Service::VERSION).to match(/\d\.\d\.\d/)
6
+ end
7
+ end
@@ -0,0 +1,38 @@
1
+ ENV["RACK_ENV"] = "test"
2
+ ENV["RAILS_ENV"] = "test"
3
+
4
+ require "byebug"
5
+ require "rspec"
6
+
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
19
+
20
+ require "simple/service"
21
+
22
+ Dir.glob("./spec/support/**/*.rb").sort.each { |path| load path }
23
+
24
+ RSpec.configure do |config|
25
+ config.run_all_when_everything_filtered = true
26
+ config.filter_run focus: (ENV["CI"] != "true")
27
+ config.expect_with(:rspec) { |c| c.syntax = :expect }
28
+ config.order = "random"
29
+ config.example_status_persistence_file_path = ".rspec.data"
30
+
31
+ config.backtrace_exclusion_patterns << /spec\/support/
32
+ config.backtrace_exclusion_patterns << /spec\/helpers/
33
+ config.backtrace_exclusion_patterns << /spec_helper/
34
+
35
+ # config.around(:each) do |example|
36
+ # example.run
37
+ # end
38
+ end
@@ -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 ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple-service
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.3
5
+ platform: ruby
6
+ authors:
7
+ - radiospiel
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-11-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: expectation
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1'
27
+ description: Pretty simple and somewhat abstract service description
28
+ email: eno@radiospiel.org
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - ".gitignore"
34
+ - ".rubocop.yml"
35
+ - ".tm_properties"
36
+ - Gemfile
37
+ - Makefile
38
+ - README.md
39
+ - Rakefile
40
+ - VERSION
41
+ - bin/bundle
42
+ - bin/console
43
+ - bin/rake
44
+ - bin/rspec
45
+ - lib/simple-service.rb
46
+ - lib/simple/service.rb
47
+ - lib/simple/service/action.rb
48
+ - lib/simple/service/action/comment.rb
49
+ - lib/simple/service/action/indie_hash.rb
50
+ - lib/simple/service/action/method_reflection.rb
51
+ - lib/simple/service/action/parameter.rb
52
+ - lib/simple/service/context.rb
53
+ - lib/simple/service/errors.rb
54
+ - lib/simple/service/version.rb
55
+ - log/.gitkeep
56
+ - scripts/release
57
+ - scripts/release.rb
58
+ - scripts/stats
59
+ - scripts/watch
60
+ - simple-service.gemspec
61
+ - spec/simple/service/action_invoke2_spec.rb
62
+ - spec/simple/service/action_invoke_spec.rb
63
+ - spec/simple/service/action_spec.rb
64
+ - spec/simple/service/context_spec.rb
65
+ - spec/simple/service/service_spec.rb
66
+ - spec/simple/service/version_spec.rb
67
+ - spec/spec_helper.rb
68
+ - spec/support/spec_services.rb
69
+ homepage: http://github.com/radiospiel/simple-service
70
+ licenses: []
71
+ metadata: {}
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - "~>"
79
+ - !ruby/object:Gem::Version
80
+ version: '2.5'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubygems_version: 3.0.4
88
+ signing_key:
89
+ specification_version: 4
90
+ summary: Pretty simple and somewhat abstract service description
91
+ test_files:
92
+ - spec/simple/service/action_invoke2_spec.rb
93
+ - spec/simple/service/action_invoke_spec.rb
94
+ - spec/simple/service/action_spec.rb
95
+ - spec/simple/service/context_spec.rb
96
+ - spec/simple/service/service_spec.rb
97
+ - spec/simple/service/version_spec.rb
98
+ - spec/spec_helper.rb
99
+ - spec/support/spec_services.rb