servactory 1.8.6 → 1.8.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 63d469613e34fcd13e27dc8fb62fa6c36fc25cf9b9e35f5b21af30013ac75993
4
- data.tar.gz: e3edd5a0bfbd2b889e81be1aeeaa836f80c6e9925c65e5f4ed09ce2a35d44673
3
+ metadata.gz: 4195548749c897719881d14227c7526a0be6097b38123e8556470089e867958a
4
+ data.tar.gz: c096b1aacf6217243b1d292afad6cff733dfc955a3d720741de60c5a7d2420cc
5
5
  SHA512:
6
- metadata.gz: 774bb94eb10a8695cde168e8cd7bc224ebc65804e2092c617dacf471612d1b3980dbc2f6d80b0418f9885a4697587c9d2d12c4172b176d79de4d6114f5198aed
7
- data.tar.gz: 2987a6379e4404e1757e4cef6749ae3e5fd8b98712d23e630b1b0a5bde5cdac8232046089d59cc1e642b7a540a9def431dfc6708e3696a14b517ce22cd1025f7
6
+ metadata.gz: fff98fb471d49bb6ffda9d31d8daead860313cf12725db2251b22bf69d170bec272376bd71f1f2319ff373c962f6a06822f8e83a435140799d6698acb1454674
7
+ data.tar.gz: e333261e7b6a7c5fd8cfc339ac312845bcd89b373100db21d330f89119ea3994fa5c9d66066c8b9b5705e648567513cd1ddbda6907aab4389c6b05475f36ede2
data/README.md CHANGED
@@ -1,9 +1,21 @@
1
- # Servactory
2
-
3
- A set of tools for building reliable services of any complexity.
4
-
5
- [![Gem version](https://img.shields.io/gem/v/servactory?logo=rubygems&logoColor=fff)](https://rubygems.org/gems/servactory)
6
- [![Release Date](https://img.shields.io/github/release-date/afuno/servactory)](https://github.com/afuno/servactory/releases)
1
+ <p align="center">
2
+ <a href="https://servactory.com" target="_blank">
3
+ <picture>
4
+ <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/servactory/servactory/main/.github/logo-dark.svg">
5
+ <source media="(prefers-color-scheme: light)" srcset="https://raw.githubusercontent.com/servactory/servactory/main/.github/logo-light.svg">
6
+ <img alt="Servactory" src="https://raw.githubusercontent.com/servactory/servactory/main/.github/logo-light.svg" width="350" height="70" style="max-width: 100%;">
7
+ </picture>
8
+ </a>
9
+ </p>
10
+
11
+ <p align="center">
12
+ A set of tools for building reliable services of any complexity.
13
+ </p>
14
+
15
+ <p align="center">
16
+ <a href="https://rubygems.org/gems/servactory"><img src="https://img.shields.io/gem/v/servactory?logo=rubygems&logoColor=fff" alt="Gem version"></a>
17
+ <a href="https://github.com/afuno/servactory/releases"><img src="https://img.shields.io/github/release-date/afuno/servactory" alt="Release Date"></a>
18
+ </p>
7
19
 
8
20
  ## Documentation
9
21
 
@@ -29,7 +41,7 @@ class UserService::Authenticate < Servactory::Base
29
41
  private
30
42
 
31
43
  def call
32
- if (user = User.find_by(email: inputs.email)&.authenticate(inputs.password))
44
+ if (user = User.authenticate_by(email: inputs.email, password: inputs.password)).present?
33
45
  outputs.user = user
34
46
  else
35
47
  fail!(message: "Authentication failed")
@@ -28,6 +28,6 @@ en:
28
28
  checks:
29
29
  type:
30
30
  default_error: "[%{service_class_name}] Wrong type of output attribute `%{output_name}`, expected `%{expected_type}`, got `%{given_type}`"
31
- tools:
32
- conflicts:
33
- error: "[%{service_class_name}] Conflict between internal and output attributes `%{overlapping_attributes}`"
31
+ methods:
32
+ call:
33
+ not_used: Nothing to perform. Use `make` or create a `call` method.
@@ -0,0 +1,33 @@
1
+ ru:
2
+ servactory:
3
+ inputs:
4
+ checks:
5
+ inclusion:
6
+ default_error: "[%{service_class_name}] Неверное значение в `%{input_name}`, должно быть одним из `%{input_inclusion}`"
7
+ must:
8
+ default_error: "[%{service_class_name}] Инпут `%{input_name}` должен \"%{code}\""
9
+ syntax_error: "[%{service_class_name}] Синтаксическая ошибка внутри `%{code}` инпута `%{input_name}`"
10
+ required:
11
+ default_error:
12
+ default: "[%{service_class_name}] Обязательный инпут `%{input_name}` отсутствует"
13
+ for_array: "[%{service_class_name}] Обязательный элемент в инпуте-массиве `%{input_name}` отсутствует"
14
+ type:
15
+ default_error:
16
+ default: "[%{service_class_name}] Неверный тип инпута `%{input_name}`, ожидалось `%{expected_type}`, получено `%{given_type}`"
17
+ for_array: "[%{service_class_name}] Неверный тип инпута-массива `%{input_name}`, ожидалось `%{expected_type}`"
18
+ tools:
19
+ find_unnecessary:
20
+ error: "[%{service_class_name}] Неожиданные атрибуты: `%{unnecessary_attributes}`"
21
+ rules:
22
+ error: "[%{service_class_name}] Конфликт в опциях инпута `%{input_name}`: `%{conflict_code}`"
23
+ internals:
24
+ checks:
25
+ type:
26
+ default_error: "[%{service_class_name}] Неверный тип внутреннего атрибута `%{internal_name}`, ожидалось `%{expected_type}`, получено `%{given_type}`"
27
+ outputs:
28
+ checks:
29
+ type:
30
+ default_error: "[%{service_class_name}] Неверный тип выходящего атрибута `%{output_name}`, ожидалось `%{expected_type}`, получено `%{given_type}`"
31
+ methods:
32
+ call:
33
+ not_used: Нечего исполнять. Используйте `make` или создайте метод `call`.
@@ -8,10 +8,29 @@ module Servactory
8
8
  end
9
9
 
10
10
  module ClassMethods
11
+ def inherited(child) # rubocop:disable Metrics/AbcSize
12
+ super
13
+
14
+ child.config.input_error_class = config.input_error_class
15
+ child.config.internal_error_class = config.internal_error_class
16
+ child.config.output_error_class = config.output_error_class
17
+
18
+ child.config.failure_class = config.failure_class
19
+
20
+ child.config.input_option_helpers = config.input_option_helpers
21
+
22
+ child.config.aliases_for_make = config.aliases_for_make
23
+ child.config.shortcuts_for_make = config.shortcuts_for_make
24
+ end
25
+
26
+ def config
27
+ @config ||= Servactory::Configuration::Setup.new
28
+ end
29
+
11
30
  private
12
31
 
13
32
  def configuration(&block)
14
- @configuration_factory ||= Factory.new
33
+ @configuration_factory ||= Factory.new(config)
15
34
 
16
35
  @configuration_factory.instance_eval(&block)
17
36
  end
@@ -3,32 +3,36 @@
3
3
  module Servactory
4
4
  module Configuration
5
5
  class Factory
6
+ def initialize(config)
7
+ @config = config
8
+ end
9
+
6
10
  def input_error_class(input_error_class)
7
- Servactory.configuration.input_error_class = input_error_class
11
+ @config.input_error_class = input_error_class
8
12
  end
9
13
 
10
14
  def output_error_class(output_error_class)
11
- Servactory.configuration.output_error_class = output_error_class
15
+ @config.output_error_class = output_error_class
12
16
  end
13
17
 
14
18
  def internal_error_class(internal_error_class)
15
- Servactory.configuration.internal_error_class = internal_error_class
19
+ @config.internal_error_class = internal_error_class
16
20
  end
17
21
 
18
22
  def failure_class(failure_class)
19
- Servactory.configuration.failure_class = failure_class
23
+ @config.failure_class = failure_class
20
24
  end
21
25
 
22
26
  def input_option_helpers(input_option_helpers)
23
- Servactory.configuration.input_option_helpers.merge(input_option_helpers)
27
+ @config.input_option_helpers.merge(input_option_helpers)
24
28
  end
25
29
 
26
30
  def aliases_for_make(aliases_for_make)
27
- Servactory.configuration.aliases_for_make.merge(aliases_for_make)
31
+ @config.aliases_for_make.merge(aliases_for_make)
28
32
  end
29
33
 
30
34
  def shortcuts_for_make(shortcuts_for_make)
31
- Servactory.configuration.shortcuts_for_make.merge(shortcuts_for_make)
35
+ @config.shortcuts_for_make.merge(shortcuts_for_make)
32
36
  end
33
37
  end
34
38
  end
@@ -20,7 +20,7 @@ module Servactory
20
20
 
21
21
  def call(arguments = {})
22
22
  call!(arguments)
23
- rescue Servactory.configuration.failure_class => e
23
+ rescue config.failure_class => e
24
24
  Servactory::Result.failure_for(exception: e)
25
25
  end
26
26
  end
@@ -29,14 +29,14 @@ module Servactory
29
29
  alias out outputs
30
30
 
31
31
  def fail_input!(input_name, message:)
32
- raise Servactory.configuration.input_error_class.new(
32
+ raise self.class.config.input_error_class.new(
33
33
  input_name: input_name,
34
34
  message: message
35
35
  )
36
36
  end
37
37
 
38
38
  def fail!(message:, meta: nil)
39
- raise Servactory.configuration.failure_class.new(message: message, meta: meta)
39
+ raise self.class.config.failure_class.new(message: message, meta: meta)
40
40
  end
41
41
 
42
42
  private
@@ -75,6 +75,15 @@ module Servactory
75
75
  @collection_of_outputs = collection_of_outputs
76
76
  end
77
77
 
78
+ def call
79
+ raise self.class.config.failure_class.new(
80
+ message: I18n.t(
81
+ "servactory.methods.call.not_used",
82
+ service_class_name: self.class.name
83
+ )
84
+ )
85
+ end
86
+
78
87
  def service_storage
79
88
  @service_storage ||= { internals: {}, outputs: {} }
80
89
  end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Servactory
4
+ module DSL
5
+ def self.included(base)
6
+ base.include(Configuration::DSL)
7
+ base.include(Info::DSL)
8
+ base.include(Context::DSL)
9
+ base.include(Inputs::DSL)
10
+ base.include(Internals::DSL)
11
+ base.include(Outputs::DSL)
12
+ base.include(Methods::DSL)
13
+ end
14
+ end
15
+ end
@@ -21,6 +21,7 @@ module Servactory
21
21
  collection_of_inputs << Input.new(
22
22
  name,
23
23
  *helpers,
24
+ config: config,
24
25
  **options
25
26
  )
26
27
  end
@@ -8,7 +8,8 @@ module Servactory
8
8
  private_constant :ARRAY_DEFAULT_VALUE
9
9
 
10
10
  attr_reader :name,
11
- :internal_name
11
+ :internal_name,
12
+ :config
12
13
 
13
14
  # rubocop:disable Style/KeywordParametersOrder
14
15
  def initialize(
@@ -16,10 +17,12 @@ module Servactory
16
17
  *helpers,
17
18
  as: nil,
18
19
  type:,
20
+ config:,
19
21
  **options
20
22
  )
21
23
  @name = name
22
24
  @internal_name = as.present? ? as : name
25
+ @config = config
23
26
 
24
27
  options = apply_helpers_for_options(helpers: helpers, options: options) if helpers.present?
25
28
 
@@ -43,7 +46,7 @@ module Servactory
43
46
  prepared_options = {}
44
47
 
45
48
  helpers.each do |helper|
46
- found_helper = Servactory.configuration.input_option_helpers.find_by(name: helper)
49
+ found_helper = config.input_option_helpers.find_by(name: helper)
47
50
 
48
51
  next if found_helper.blank?
49
52
 
@@ -23,7 +23,7 @@ module Servactory
23
23
  unnecessary_attributes: unnecessary_attributes.join(", ")
24
24
  )
25
25
 
26
- raise Servactory.configuration.input_error_class.new(message: message_text)
26
+ raise @context.class.config.input_error_class.new(message: message_text)
27
27
  end
28
28
 
29
29
  private
@@ -35,7 +35,7 @@ module Servactory
35
35
  conflict_code: input.conflict_code
36
36
  )
37
37
 
38
- raise Servactory.configuration.input_error_class.new(
38
+ raise @context.class.config.input_error_class.new(
39
39
  message: message_text,
40
40
  input_name: input.name
41
41
  )
@@ -43,7 +43,12 @@ module Servactory
43
43
  end
44
44
  end
45
45
 
46
- def process_validation_classes(validation_class, input:, check_key:, check_options:)
46
+ def process_validation_classes(
47
+ validation_class,
48
+ input:,
49
+ check_key:,
50
+ check_options:
51
+ )
47
52
  validation_class.check(
48
53
  context: @context,
49
54
  input: input,
@@ -68,7 +73,7 @@ module Servactory
68
73
  def raise_errors
69
74
  return if (tmp_errors = errors.not_blank).empty?
70
75
 
71
- raise Servactory.configuration.input_error_class.new(message: tmp_errors.first)
76
+ raise @context.class.config.input_error_class.new(message: tmp_errors.first)
72
77
  end
73
78
  end
74
79
  end
@@ -17,13 +17,19 @@ module Servactory
17
17
  private_constant :DEFAULT_MESSAGE
18
18
 
19
19
  def self.check(context:, input:, value:, check_key:, **)
20
- return unless should_be_checked_for?(input, check_key)
20
+ return unless should_be_checked_for?(input, value, check_key)
21
21
 
22
22
  new(context: context, input: input, value: value).check
23
23
  end
24
24
 
25
- def self.should_be_checked_for?(input, check_key)
26
- check_key == :inclusion && input.inclusion_present?
25
+ def self.should_be_checked_for?(input, value, check_key)
26
+ check_key == :inclusion && (
27
+ input.required? || (
28
+ input.optional? && !input.default.nil?
29
+ ) || (
30
+ input.optional? && !value.nil?
31
+ )
32
+ )
27
33
  end
28
34
 
29
35
  ##########################################################################
@@ -9,7 +9,7 @@ module Servactory
9
9
  def raise_error_with(message, **attributes)
10
10
  message = message.call(**attributes) if message.is_a?(Proc)
11
11
 
12
- raise Servactory.configuration.internal_error_class.new(message: message)
12
+ raise @context.class.config.internal_error_class.new(message: message)
13
13
  end
14
14
  end
15
15
  end
@@ -68,13 +68,9 @@ module Servactory
68
68
  end
69
69
 
70
70
  def method_missing(name, *args, &block)
71
- if Servactory.configuration.aliases_for_make.include?(name)
72
- return method_missing_for_aliases_for_make(name, *args, &block)
73
- end
71
+ return method_missing_for_aliases_for_make(name, *args, &block) if config.aliases_for_make.include?(name)
74
72
 
75
- if Servactory.configuration.shortcuts_for_make.include?(name)
76
- return method_missing_for_shortcuts_for_make(name, *args, &block)
77
- end
73
+ return method_missing_for_shortcuts_for_make(name, *args, &block) if config.shortcuts_for_make.include?(name)
78
74
 
79
75
  super
80
76
  end
@@ -97,9 +93,7 @@ module Servactory
97
93
  end
98
94
 
99
95
  def respond_to_missing?(name, *)
100
- Servactory.configuration.aliases_for_make.include?(name) ||
101
- Servactory.configuration.shortcuts_for_make.include?(name) ||
102
- super
96
+ config.aliases_for_make.include?(name) || config.shortcuts_for_make.include?(name) || super
103
97
  end
104
98
 
105
99
  def next_position
@@ -14,7 +14,7 @@ module Servactory
14
14
  end
15
15
 
16
16
  def run!
17
- return try_to_use_call if @collection_of_stages.empty?
17
+ return use_call if @collection_of_stages.empty?
18
18
 
19
19
  @collection_of_stages.sorted_by_position.each do |stage|
20
20
  call_stage(stage)
@@ -23,8 +23,8 @@ module Servactory
23
23
 
24
24
  private
25
25
 
26
- def try_to_use_call
27
- @context.try(:send, :call)
26
+ def use_call
27
+ @context.send(:call)
28
28
  end
29
29
 
30
30
  def call_stage(stage)
@@ -9,7 +9,7 @@ module Servactory
9
9
  def raise_error_with(message, **attributes)
10
10
  message = message.call(**attributes) if message.is_a?(Proc)
11
11
 
12
- raise Servactory.configuration.output_error_class.new(message: message)
12
+ raise @context.class.config.output_error_class.new(message: message)
13
13
  end
14
14
  end
15
15
  end
@@ -4,7 +4,7 @@ module Servactory
4
4
  module VERSION
5
5
  MAJOR = 1
6
6
  MINOR = 8
7
- PATCH = 6
7
+ PATCH = 8
8
8
 
9
9
  STRING = [MAJOR, MINOR, PATCH].join(".")
10
10
  end
data/lib/servactory.rb CHANGED
@@ -4,32 +4,12 @@ require "zeitwerk"
4
4
 
5
5
  require "active_support/core_ext/string"
6
6
 
7
- # require "servactory/support/loader"
8
-
9
7
  loader = Zeitwerk::Loader.for_gem
10
8
  loader.inflector.inflect(
11
9
  "dsl" => "DSL"
12
10
  )
13
11
  loader.setup
14
12
 
15
- module Servactory
16
- module_function
17
-
18
- def configuration
19
- @configuration ||= Servactory::Configuration::Setup.new
20
- end
21
-
22
- def reset
23
- @configuration = Servactory::Configuration::Setup.new
24
- end
25
-
26
- def configure
27
- yield(configuration)
28
- end
29
- end
13
+ module Servactory; end
30
14
 
31
15
  require "servactory/engine" if defined?(Rails::Engine)
32
-
33
- # require_relative "servactory/exceptions"
34
-
35
- # require_relative "servactory/base"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: servactory
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.8.6
4
+ version: 1.8.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anton Sokolov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-07-04 00:00:00.000000000 Z
11
+ date: 2023-07-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -188,6 +188,7 @@ files:
188
188
  - README.md
189
189
  - Rakefile
190
190
  - config/locales/en.yml
191
+ - config/locales/ru.yml
191
192
  - lib/servactory.rb
192
193
  - lib/servactory/base.rb
193
194
  - lib/servactory/configuration/dsl.rb
@@ -199,6 +200,7 @@ files:
199
200
  - lib/servactory/context/workspace/inputs.rb
200
201
  - lib/servactory/context/workspace/internals.rb
201
202
  - lib/servactory/context/workspace/outputs.rb
203
+ - lib/servactory/dsl.rb
202
204
  - lib/servactory/engine.rb
203
205
  - lib/servactory/errors/base.rb
204
206
  - lib/servactory/errors/failure.rb
@@ -276,7 +278,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
276
278
  - !ruby/object:Gem::Version
277
279
  version: '0'
278
280
  requirements: []
279
- rubygems_version: 3.4.10
281
+ rubygems_version: 3.4.17
280
282
  signing_key:
281
283
  specification_version: 4
282
284
  summary: A set of tools for building reliable services of any complexity