slayer 0.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 +7 -0
- data/.gitignore +11 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +87 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/slayer/composer.rb +80 -0
- data/lib/slayer/errors.rb +21 -0
- data/lib/slayer/result.rb +23 -0
- data/lib/slayer/service.rb +52 -0
- data/lib/slayer/string_ext.rb +11 -0
- data/lib/slayer/version.rb +3 -0
- data/lib/slayer.rb +6 -0
- data/slayer.gemspec +24 -0
- data/slayer_logo.png +0 -0
- metadata +103 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 731adbde67b9dfe482a51fb05b76e3f77cd0f615
|
4
|
+
data.tar.gz: 1e482c6f28b9d9173252cdce74f18995ca77f0b3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: eb418185d91671b33a0433cd19c63e42e2d0dcb828b47cfbf0a477855d59ebda1a645c5b23eec0adb29cac1429fdb9f7185097295b3df01a2ef375303a77b2ed
|
7
|
+
data.tar.gz: 0e823344bfcb291cbee00d09a96c5fb5023868ea5166ddd46a13214f968dff6d178e6e276f308baaa1617fda79ff96fc3d88b5a820ec944bea2609472f8b4c9a
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Wyatt Kirby
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+

|
2
|
+
|
3
|
+
# Slayer: A Service Layer
|
4
|
+
|
5
|
+
Slayer is intended to operate as a minimal service layer for your ruby application. To achieve this, Slayer provides base classes for writing small, composable services, which should behave as [pure functions](https://en.wikipedia.org/wiki/Pure_function).
|
6
|
+
|
7
|
+
Slayer Services should implement `call`, which will `pass` or `fail` the service based on input. Services return a `Slayer::Result` which has a predictable interface for determining `success?` or `failure?`, a `message`, and a `result`.
|
8
|
+
|
9
|
+
Services are composed by Composers, which describe the order and arguments for running a number of Services in sequence. If any of these Services fail, those run before it are rolled back by calling `rollback` on the service, passing the service its `result` object, and the original parameters.
|
10
|
+
|
11
|
+
Composers also return a `Slayer::Result` object, which has as its `result` parameter a hash of the result objects from its composed services.
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Add this line to your application's Gemfile:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
gem 'slayer'
|
19
|
+
```
|
20
|
+
|
21
|
+
And then execute:
|
22
|
+
|
23
|
+
$ bundle
|
24
|
+
|
25
|
+
Or install it yourself as:
|
26
|
+
|
27
|
+
$ gem install slayer
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
# A service that passes when given the string "foo"
|
33
|
+
# and fails if given anything else.
|
34
|
+
class FooService < Slayer::Service
|
35
|
+
def call(foo:)
|
36
|
+
if foo == "foo"
|
37
|
+
pass! result: foo, message: "Passing FooService"
|
38
|
+
else
|
39
|
+
fail! result: foo, message: "Failing FooService"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# A placeholder service that always passes with returned result
|
45
|
+
# as the argument passed into it.
|
46
|
+
class BarService < Slayer::Service
|
47
|
+
def call(bar:)
|
48
|
+
pass! result: bar, message: "Passing BarService"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
# A simple composer which composes the FooService and BarService
|
53
|
+
class FooBarComposer < Slayer::Composer
|
54
|
+
compose FooService, BarService
|
55
|
+
|
56
|
+
def call
|
57
|
+
pass! result: @results, message: "Yay!"
|
58
|
+
end
|
59
|
+
|
60
|
+
def foo_service_args
|
61
|
+
return { foo: @composer_params[:foo] }
|
62
|
+
end
|
63
|
+
|
64
|
+
def bar_service_args
|
65
|
+
return { bar: foo_service_results.message }
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
|
70
|
+
result = FooBarComposer.call(foo: "Jim", bar: "Joe")
|
71
|
+
result.success? # => true
|
72
|
+
```
|
73
|
+
|
74
|
+
## Development
|
75
|
+
|
76
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
77
|
+
|
78
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
79
|
+
|
80
|
+
## Contributing
|
81
|
+
|
82
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/apsislabs/slayer.
|
83
|
+
|
84
|
+
|
85
|
+
## License
|
86
|
+
|
87
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "slayer"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
module Slayer
|
2
|
+
class Composer < Service
|
3
|
+
attr_accessor :called
|
4
|
+
attr_accessor :results
|
5
|
+
attr_accessor :called_services
|
6
|
+
attr_accessor :composer_params
|
7
|
+
|
8
|
+
class << self
|
9
|
+
def compose(*services)
|
10
|
+
@services = services.flatten
|
11
|
+
end
|
12
|
+
|
13
|
+
def services
|
14
|
+
@services ||= []
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# Locate Results from Magic Methods
|
19
|
+
def method_missing(method_sym, *arguments, &block)
|
20
|
+
if method_sym.to_s =~ /^(.*)_results$/
|
21
|
+
results = @results[$1.to_sym]
|
22
|
+
return results unless results.nil?
|
23
|
+
end
|
24
|
+
|
25
|
+
super
|
26
|
+
end
|
27
|
+
|
28
|
+
def run!(**args, &block)
|
29
|
+
@composer_params = args
|
30
|
+
@called_services = []
|
31
|
+
@results = {}
|
32
|
+
|
33
|
+
# Attempt to run each Service, if any fail,
|
34
|
+
# call rollback on all those already run in
|
35
|
+
# reverse order.
|
36
|
+
begin
|
37
|
+
self.class.services.each do |service|
|
38
|
+
service_sym = service_to_sym(service)
|
39
|
+
service_args = service_to_args(service)
|
40
|
+
|
41
|
+
# Run the service then add it to called_services
|
42
|
+
@results[service_sym] = service.new.run!(service_args)
|
43
|
+
@called_services << service
|
44
|
+
end
|
45
|
+
rescue ServiceFailure
|
46
|
+
@called_services.reverse_each do |service|
|
47
|
+
service_args = service_to_args(service)
|
48
|
+
service_result = service_results(service)
|
49
|
+
|
50
|
+
# Pass the original args and result object
|
51
|
+
service.rollback(service_args, service_result)
|
52
|
+
end
|
53
|
+
|
54
|
+
raise
|
55
|
+
end
|
56
|
+
|
57
|
+
call
|
58
|
+
@called = true
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
# Convert a Service to an underscored symbol
|
64
|
+
def service_to_sym(service)
|
65
|
+
service.name.underscore.to_sym
|
66
|
+
end
|
67
|
+
|
68
|
+
# Convert a Service to a call to an args method
|
69
|
+
def service_to_args(service)
|
70
|
+
service_sym = service_to_sym(service)
|
71
|
+
self.method("#{service_sym}_args").call
|
72
|
+
end
|
73
|
+
|
74
|
+
# Convert a Service to its result object
|
75
|
+
def service_results(service)
|
76
|
+
service_sym = service_to_sym(service)
|
77
|
+
return @results[service_sym]
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Slayer
|
2
|
+
class ServiceFailure < StandardError
|
3
|
+
attr_reader :result
|
4
|
+
|
5
|
+
def initialize(result)
|
6
|
+
@result = result
|
7
|
+
super
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class ServiceNotImplemented < StandardError
|
12
|
+
def initialize(message = nil)
|
13
|
+
message ||= %q(
|
14
|
+
Service implementation must call `fail!` or `pass!`,
|
15
|
+
or return a <Slayer::Result> object
|
16
|
+
)
|
17
|
+
|
18
|
+
super message
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Slayer
|
2
|
+
class Result
|
3
|
+
attr_reader :result, :message
|
4
|
+
|
5
|
+
def initialize(result, message)
|
6
|
+
@result = result
|
7
|
+
@message = message
|
8
|
+
end
|
9
|
+
|
10
|
+
def success?
|
11
|
+
!failure?
|
12
|
+
end
|
13
|
+
|
14
|
+
def failure?
|
15
|
+
@failure || false
|
16
|
+
end
|
17
|
+
|
18
|
+
def fail!
|
19
|
+
@failure = true
|
20
|
+
raise ServiceFailure, self
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module Slayer
|
2
|
+
class Service
|
3
|
+
attr_accessor :result
|
4
|
+
|
5
|
+
# Internal: Service Class Methods
|
6
|
+
class << self
|
7
|
+
def call(*args, &block)
|
8
|
+
# Run the Service and capture the result
|
9
|
+
result = new.tap { |s|
|
10
|
+
s.run(*args, &block)
|
11
|
+
}.result
|
12
|
+
|
13
|
+
# Throw an exception if we don't return a result
|
14
|
+
raise ServiceNotImplemented unless result.is_a? Result
|
15
|
+
return result
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# Run the Service, rescue from Failures
|
20
|
+
def run(*args, &block)
|
21
|
+
begin
|
22
|
+
run!(*args, &block)
|
23
|
+
rescue ServiceFailure
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
# Run the Service
|
28
|
+
def run!(*args, &block)
|
29
|
+
call(*args, &block)
|
30
|
+
end
|
31
|
+
|
32
|
+
# Fail the Service
|
33
|
+
def fail! result:, message:
|
34
|
+
@result = Result.new(result, message)
|
35
|
+
@result.fail!
|
36
|
+
end
|
37
|
+
|
38
|
+
# Pass the Service
|
39
|
+
def pass! result:, message:
|
40
|
+
@result = Result.new(result, message)
|
41
|
+
end
|
42
|
+
|
43
|
+
# Call the service
|
44
|
+
def call
|
45
|
+
raise NotImplementedError, "Services must define method `#call`."
|
46
|
+
end
|
47
|
+
|
48
|
+
# Do nothing
|
49
|
+
def rollback
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/lib/slayer.rb
ADDED
data/slayer.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'slayer/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "slayer"
|
8
|
+
spec.version = Slayer::VERSION
|
9
|
+
spec.authors = ["Wyatt Kirby"]
|
10
|
+
spec.email = ["kirby.wa@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{A service layer}
|
13
|
+
spec.homepage = "http://www.apsis.io"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
24
|
+
end
|
data/slayer_logo.png
ADDED
Binary file
|
metadata
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: slayer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Wyatt Kirby
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-02-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.12'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.12'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- kirby.wa@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".travis.yml"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- bin/console
|
69
|
+
- bin/setup
|
70
|
+
- lib/slayer.rb
|
71
|
+
- lib/slayer/composer.rb
|
72
|
+
- lib/slayer/errors.rb
|
73
|
+
- lib/slayer/result.rb
|
74
|
+
- lib/slayer/service.rb
|
75
|
+
- lib/slayer/string_ext.rb
|
76
|
+
- lib/slayer/version.rb
|
77
|
+
- slayer.gemspec
|
78
|
+
- slayer_logo.png
|
79
|
+
homepage: http://www.apsis.io
|
80
|
+
licenses:
|
81
|
+
- MIT
|
82
|
+
metadata: {}
|
83
|
+
post_install_message:
|
84
|
+
rdoc_options: []
|
85
|
+
require_paths:
|
86
|
+
- lib
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
requirements: []
|
98
|
+
rubyforge_project:
|
99
|
+
rubygems_version: 2.6.8
|
100
|
+
signing_key:
|
101
|
+
specification_version: 4
|
102
|
+
summary: A service layer
|
103
|
+
test_files: []
|