servactory 1.8.7 → 1.9.0

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: e1be37829a79065ae8dcecc340fbecb58a1fcd15739221b38cf57474e371f3e5
4
- data.tar.gz: beed9c07d4ed8772da258084c320f0f278e5170169971abe4497a3863eb59399
3
+ metadata.gz: 8d3dde0507ee0b98c8ab6f6950d76928e1dd378020ac82ed17a05b73c8ad7bc2
4
+ data.tar.gz: edadf08439ecfbdae91640959ba142b055b5c74551c0e48519d0a50fe97a83b0
5
5
  SHA512:
6
- metadata.gz: 03a6c5da26c496b0d96a7ae51cbd85e1cc70e318222db1561add7d91fa27f0278c160129735b3e65ac9165eb440a5ec3fe33c4f2390e7e33c95997521c185203
7
- data.tar.gz: 73772169c25d6d2eeb45a4032bb47ce0a6f47dad6e05102a2f2512fa33eaf9c13bdcdcc665e84f7e2660c169ba683919921f497708967b2bbd3b5b221a28a7b9
6
+ metadata.gz: 42008a44ef24107e0c4bc9077a6b60efb0d7398d01cb437a553cce74a41ec74f11cc9109e815f5d010c693f9f75f6adc4ecb783fcb67e9b0417a687d4ea5d294
7
+ data.tar.gz: bc500cfae97cc2876f38882ef78bff775d4da7a18bb720a3bb5f3738db3f58d647b72010fd78ba6947a43b5ebee7275c61655683befac7765213069a3e0620a4
data/README.md CHANGED
@@ -41,7 +41,7 @@ class UserService::Authenticate < Servactory::Base
41
41
  private
42
42
 
43
43
  def call
44
- if (user = User.find_by(email: inputs.email)&.authenticate(inputs.password))
44
+ if (user = User.authenticate_by(email: inputs.email, password: inputs.password)).present?
45
45
  outputs.user = user
46
46
  else
47
47
  fail!(message: "Authentication failed")
@@ -28,9 +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}`"
34
31
  methods:
35
32
  call:
36
- not_used: Nothing to perform. Use `make` or create a `call` method
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
@@ -76,7 +76,7 @@ module Servactory
76
76
  end
77
77
 
78
78
  def call
79
- raise Servactory.configuration.failure_class.new(
79
+ raise self.class.config.failure_class.new(
80
80
  message: I18n.t(
81
81
  "servactory.methods.call.not_used",
82
82
  service_class_name: self.class.name
@@ -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
@@ -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
@@ -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
@@ -3,11 +3,27 @@
3
3
  module Servactory
4
4
  module TestKit
5
5
  class Result
6
- def initialize(**attributes)
7
- attributes.each_pair do |method_name, method_return|
8
- define_singleton_method(method_name) { method_return }
6
+ def self.as_success(attributes = {})
7
+ Servactory::Result.success_for(context: new(attributes))
8
+ end
9
+
10
+ def self.as_failure(exception: nil)
11
+ Servactory::Result.failure_for(exception: exception)
12
+ end
13
+
14
+ def initialize(attributes = {})
15
+ attributes.each_pair do |name, value|
16
+ service_storage[:outputs].merge!({ name => value })
9
17
  end
10
18
  end
19
+
20
+ private_class_method :new
21
+
22
+ private
23
+
24
+ def service_storage
25
+ @service_storage ||= { outputs: {} }
26
+ end
11
27
  end
12
28
  end
13
29
  end
@@ -3,8 +3,8 @@
3
3
  module Servactory
4
4
  module VERSION
5
5
  MAJOR = 1
6
- MINOR = 8
7
- PATCH = 7
6
+ MINOR = 9
7
+ PATCH = 0
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.7
4
+ version: 1.9.0
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-13 00:00:00.000000000 Z
11
+ date: 2023-07-20 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