servitium 1.2.20
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +6 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +59 -0
- data/Rakefile +18 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/generators/servitium/USAGE +9 -0
- data/lib/generators/servitium/service_generator.rb +17 -0
- data/lib/generators/servitium/templates/context.rb +13 -0
- data/lib/generators/servitium/templates/service.rb +13 -0
- data/lib/generators/servitium/templates/service_test.rb +11 -0
- data/lib/servitium/capture_exceptions_mixin.rb +26 -0
- data/lib/servitium/context.rb +65 -0
- data/lib/servitium/context_failure.rb +12 -0
- data/lib/servitium/context_model.rb +93 -0
- data/lib/servitium/error.rb +6 -0
- data/lib/servitium/i18n.rb +21 -0
- data/lib/servitium/rails/railtie.rb +11 -0
- data/lib/servitium/rails.rb +4 -0
- data/lib/servitium/scoped_attributes.rb +32 -0
- data/lib/servitium/service.rb +270 -0
- data/lib/servitium/service_job.rb +21 -0
- data/lib/servitium/sub_contexts.rb +111 -0
- data/lib/servitium/transactional_mixin.rb +26 -0
- data/lib/servitium/version.rb +5 -0
- data/lib/servitium.rb +20 -0
- data/lib/tasks/servitium_tasks.rake +18 -0
- data/servitium.gemspec +45 -0
- metadata +286 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 583d111f0348dae63479a5dbb6d1dea714e17592eb03c9a8a0a07c84874af455
|
4
|
+
data.tar.gz: 259ab3b3110de03c89a3b50e95aa983433a6267aae0e2ee7ddaf093314dd3f1f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e240db38b1ba8c97a2fe898d77546d13474781ff8d5d2e1780eb75279bb5e45ed5afe2e4597de5e38526180a812a9eb1c7cfee90a9b923f9c994bf3dbb301f36
|
7
|
+
data.tar.gz: 700799659b4f0ae4106a8e22c5b9bcbacc93e01f119351fa19f5bbae9bd4af4e018185963e2451f092dd4a5f92d39e6ef2f706927f375b82721d8035a3b314b6
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at tom@degrunt.nl. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [https://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: https://contributor-covenant.org
|
74
|
+
[version]: https://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Tom de Grunt
|
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,59 @@
|
|
1
|
+
# Servitium
|
2
|
+
|
3
|
+
An implementation of the command pattern for Ruby
|
4
|
+
|
5
|
+
## Features
|
6
|
+
|
7
|
+
- Context validation
|
8
|
+
- Callbacks
|
9
|
+
|
10
|
+
Context:
|
11
|
+
|
12
|
+
- before_validation
|
13
|
+
- after_validation
|
14
|
+
|
15
|
+
Services:
|
16
|
+
|
17
|
+
- before_perform
|
18
|
+
- around_perform
|
19
|
+
- after_perform
|
20
|
+
|
21
|
+
- Transations
|
22
|
+
By default transactions are disabled, but you can include the following in your ApplicationService
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
transactional true
|
26
|
+
```
|
27
|
+
|
28
|
+
In case transactions are enabled after_commit will triger after after_peform
|
29
|
+
|
30
|
+
- Asynchronous execution
|
31
|
+
Instead of calling perform you can use perform_later to execute a service asynchronously, this uses ActiveJob.
|
32
|
+
|
33
|
+
## Installation
|
34
|
+
|
35
|
+
Add this line to your application's Gemfile:
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
gem 'servitium'
|
39
|
+
```
|
40
|
+
|
41
|
+
And then execute:
|
42
|
+
|
43
|
+
$ bundle install
|
44
|
+
|
45
|
+
Or install it yourself as:
|
46
|
+
|
47
|
+
$ gem install servitium
|
48
|
+
|
49
|
+
## Usage
|
50
|
+
|
51
|
+
See tests for usage examples.
|
52
|
+
|
53
|
+
## License
|
54
|
+
|
55
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
56
|
+
|
57
|
+
## Code of Conduct
|
58
|
+
|
59
|
+
Everyone interacting in the Servitium project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/servitium/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'bundler/gem_tasks'
|
4
|
+
require 'rake/testtask'
|
5
|
+
|
6
|
+
Rake::TestTask.new(:test) do |t|
|
7
|
+
t.libs << 'test'
|
8
|
+
t.libs << 'lib'
|
9
|
+
t.test_files = FileList['test/**/*_test.rb']
|
10
|
+
end
|
11
|
+
|
12
|
+
Rake.add_rakelib 'lib/tasks'
|
13
|
+
|
14
|
+
task default: :test
|
15
|
+
|
16
|
+
# Adds the Auxilium semver task
|
17
|
+
spec = Gem::Specification.find_by_name 'auxilium'
|
18
|
+
load "#{spec.gem_dir}/lib/tasks/semver.rake"
|
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'servitium'
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require 'irb'
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rails/generators'
|
4
|
+
|
5
|
+
module Servitium
|
6
|
+
class ServiceGenerator < ::Rails::Generators::NamedBase
|
7
|
+
desc 'Generates a service and context'
|
8
|
+
|
9
|
+
source_root File.expand_path('templates', __dir__)
|
10
|
+
|
11
|
+
def copy_initializer_file
|
12
|
+
template 'service.rb', "app/services/#{file_name}_service.rb"
|
13
|
+
template 'context.rb', "app/services/#{file_name}_context.rb"
|
14
|
+
template 'service_test.rb', "test/services/#{file_name}_service_test.rb"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'test_helper'
|
4
|
+
|
5
|
+
class <%= name %>ServiceTest < ActiveSupport::TestCase
|
6
|
+
test 'service context result is success' do
|
7
|
+
context = <%=name%>Service.perform(some: 'some')
|
8
|
+
assert context.success?
|
9
|
+
assert_equal 'emos', context.some
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Servitium
|
4
|
+
module CaptureExceptionsMixin
|
5
|
+
class << self
|
6
|
+
def included(base)
|
7
|
+
base.extend ClassMethods
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
def capture_exceptions(value = nil)
|
13
|
+
@capture_exceptions = value if value
|
14
|
+
@capture_exceptions = nil unless defined?(@capture_exceptions)
|
15
|
+
if @capture_exceptions.nil?
|
16
|
+
@capture_exceptions = if superclass < Servitium::Service
|
17
|
+
superclass.capture_exceptions
|
18
|
+
else
|
19
|
+
false
|
20
|
+
end
|
21
|
+
end
|
22
|
+
@capture_exceptions
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Servitium
|
4
|
+
class Context
|
5
|
+
extend ActiveModel::Callbacks
|
6
|
+
|
7
|
+
include Servitium::ContextModel
|
8
|
+
|
9
|
+
define_model_callbacks :initialize
|
10
|
+
|
11
|
+
attr_reader :errors
|
12
|
+
|
13
|
+
define_callbacks :perform
|
14
|
+
|
15
|
+
def initialize(*args)
|
16
|
+
@success = true
|
17
|
+
@called = false
|
18
|
+
@errors = ActiveModel::Errors.new(self)
|
19
|
+
@subcontexts = {}
|
20
|
+
|
21
|
+
run_callbacks :initialize do
|
22
|
+
super(*args)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def success?
|
27
|
+
@called && @success
|
28
|
+
end
|
29
|
+
|
30
|
+
def failure?
|
31
|
+
!success?
|
32
|
+
end
|
33
|
+
|
34
|
+
alias fail? failure?
|
35
|
+
alias failed? failure?
|
36
|
+
|
37
|
+
def fail!(attr, message = :invalid, options = {})
|
38
|
+
@success = false
|
39
|
+
merge_errors!(attr, message, options)
|
40
|
+
raise ContextFailure, self
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def merge_errors!(attr, message = :invalid, options = {})
|
46
|
+
return unless attr
|
47
|
+
|
48
|
+
if attr.is_a? String
|
49
|
+
errors.add(:base, attr)
|
50
|
+
elsif attr.is_a? ActiveModel::Errors
|
51
|
+
errors.merge!(attr)
|
52
|
+
elsif attr.is_a? Symbol
|
53
|
+
errors.add(attr, message, **options)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
class << self
|
58
|
+
include Servitium::I18n
|
59
|
+
|
60
|
+
def human_attribute_name(attribute, options = {})
|
61
|
+
t(".#{attribute}", default: super)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Servitium
|
4
|
+
module ContextModel
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
class_methods do
|
8
|
+
include Servitium::SubContexts
|
9
|
+
end
|
10
|
+
|
11
|
+
included do
|
12
|
+
include ActiveAttr::Model
|
13
|
+
include Servitium::I18n
|
14
|
+
|
15
|
+
delegate :input_attributes, :output_attributes, :other_attributes, to: :class
|
16
|
+
|
17
|
+
validate :validate_subcontexts
|
18
|
+
attr_accessor :supercontext
|
19
|
+
|
20
|
+
def initialize(*args)
|
21
|
+
@errors = ActiveModel::Errors.new(self)
|
22
|
+
@subcontexts = {}
|
23
|
+
super(*args)
|
24
|
+
end
|
25
|
+
|
26
|
+
def inspect
|
27
|
+
message = super
|
28
|
+
message += " (success: #{success?}, valid: #{@errors.size.zero?}, errors: #{@errors.full_messages.join(', ')})"
|
29
|
+
message
|
30
|
+
end
|
31
|
+
|
32
|
+
remove_method :model_name
|
33
|
+
|
34
|
+
def model_name
|
35
|
+
@model_name ||= ActiveModel::Name.new(self.class, nil, self.class.name.gsub('Context', ''))
|
36
|
+
end
|
37
|
+
|
38
|
+
def id
|
39
|
+
SecureRandom.uuid
|
40
|
+
end
|
41
|
+
|
42
|
+
# Convert the context model's attributes into a Hash, including the values of the subcontexts
|
43
|
+
# @return [Hash]
|
44
|
+
def attributes_hash
|
45
|
+
attributes.map do |key, value|
|
46
|
+
if value.is_a?(ContextModel)
|
47
|
+
[key, value.attributes_hash]
|
48
|
+
elsif value.is_a?(Array) && value.first.is_a?(ContextModel)
|
49
|
+
[key, value.map(&:attributes_hash)]
|
50
|
+
else
|
51
|
+
[key, value]
|
52
|
+
end
|
53
|
+
end.to_h
|
54
|
+
end
|
55
|
+
|
56
|
+
def _destroy; end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
def validate_subcontexts
|
61
|
+
@subcontexts.each do |key, value|
|
62
|
+
errors.add(key, 'invalid') unless [*value].find_all { |v| v.respond_to?(:invalid?) && v.invalid? }.empty?
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
class << self
|
67
|
+
attr_reader :inbound_scope_used, :outbound_scope_used
|
68
|
+
|
69
|
+
def input(&block)
|
70
|
+
@inbound_scope_used = true
|
71
|
+
Servitium::ScopedAttributes.new(self, input_attributes, :in).call(block)
|
72
|
+
end
|
73
|
+
|
74
|
+
def output(&block)
|
75
|
+
@outbound_scope_used = true
|
76
|
+
Servitium::ScopedAttributes.new(self, output_attributes, :out).call(block)
|
77
|
+
end
|
78
|
+
|
79
|
+
def input_attributes
|
80
|
+
@input_attributes ||= []
|
81
|
+
end
|
82
|
+
|
83
|
+
def output_attributes
|
84
|
+
@output_attributes ||= []
|
85
|
+
end
|
86
|
+
|
87
|
+
def other_attributes
|
88
|
+
@other_attributes ||= attributes.keys - (input_attributes + output_attributes)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Servitium
|
4
|
+
module I18n
|
5
|
+
def t(key, passed_options = {})
|
6
|
+
@service_scope ||= nil
|
7
|
+
|
8
|
+
unless @service_scope.present?
|
9
|
+
parts = (is_a?(Class) ? self : self.class).to_s.underscore.gsub('/', '.').split('.')
|
10
|
+
parts[-1] = "#{parts.last.gsub('_service', '').pluralize}.service" if parts.last.end_with?('_service')
|
11
|
+
parts[-1] = "#{parts.last.gsub('_context', '').pluralize}.context" if parts.last.end_with?('_context')
|
12
|
+
@service_scope = parts.compact.join('.')
|
13
|
+
end
|
14
|
+
|
15
|
+
options = { scope: @service_scope }
|
16
|
+
options[:default] = ::I18n.t(key) unless key.start_with?('.')
|
17
|
+
|
18
|
+
::I18n.t(key, **options.merge(passed_options))
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Servitium
|
4
|
+
class ScopedAttributes
|
5
|
+
attr_reader :context, :store, :validation_context
|
6
|
+
|
7
|
+
def initialize(context, store, validation_context = nil)
|
8
|
+
@context = context
|
9
|
+
@store = store
|
10
|
+
@validation_context = validation_context
|
11
|
+
end
|
12
|
+
|
13
|
+
def attribute(name, *args)
|
14
|
+
store << name.to_s
|
15
|
+
context.send(:attribute, name, *args)
|
16
|
+
end
|
17
|
+
|
18
|
+
def validates(name, options = {})
|
19
|
+
options[:on] ||= validation_context if validation_context
|
20
|
+
context.send(:validates, name, options)
|
21
|
+
end
|
22
|
+
|
23
|
+
def validate(name, options = {}, &block)
|
24
|
+
options[:on] ||= validation_context if validation_context
|
25
|
+
context.send(:validate, name, options, &block)
|
26
|
+
end
|
27
|
+
|
28
|
+
def call(block)
|
29
|
+
instance_exec(&block)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|