convenient_service 0.16.0 → 0.17.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (114) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +30 -0
  3. data/README.md +1 -1
  4. data/convenient_service.gemspec +24 -5
  5. data/lib/convenient_service/common/plugins/can_have_user_provided_entity/commands/find_or_create_entity.rb +7 -2
  6. data/lib/convenient_service/common/plugins/can_have_user_provided_entity/exceptions.rb +4 -4
  7. data/lib/convenient_service/common/plugins/has_around_callbacks/exceptions.rb +2 -2
  8. data/lib/convenient_service/common/plugins/has_around_callbacks/middleware.rb +2 -2
  9. data/lib/convenient_service/common/plugins/has_instance_proxy/commands/create_instance_proxy_class.rb +83 -0
  10. data/lib/convenient_service/common/plugins/has_instance_proxy/commands.rb +3 -0
  11. data/lib/convenient_service/common/plugins/has_instance_proxy/concern.rb +22 -0
  12. data/lib/convenient_service/common/plugins/has_instance_proxy/entities/instance_proxy.rb +76 -0
  13. data/lib/convenient_service/common/plugins/has_instance_proxy/entities.rb +3 -0
  14. data/lib/convenient_service/common/plugins/has_instance_proxy/middleware.rb +20 -0
  15. data/lib/convenient_service/common/plugins/has_instance_proxy.rb +6 -0
  16. data/lib/convenient_service/common/plugins.rb +1 -0
  17. data/lib/convenient_service/core/concern/class_methods.rb +7 -2
  18. data/lib/convenient_service/core/concern/instance_methods.rb +7 -2
  19. data/lib/convenient_service/core/entities/config/commands/track_method_missing_commit_trigger.rb +1 -1
  20. data/lib/convenient_service/core/entities/config/entities/method_middlewares/entities/caller/commands/define_method_middlewares_caller.rb +2 -2
  21. data/lib/convenient_service/core/entities/config/entities/method_middlewares/entities/middlewares/base/commands/create_observable_middleware.rb +3 -0
  22. data/lib/convenient_service/core/entities/config/exceptions.rb +4 -4
  23. data/lib/convenient_service/core/entities/config.rb +1 -1
  24. data/lib/convenient_service/dependencies/extractions/active_support_backtrace_cleaner/backtrace_cleaner.rb +28 -8
  25. data/lib/convenient_service/dependencies/extractions/active_support_backtrace_cleaner.rb +3 -3
  26. data/lib/convenient_service/dependencies/extractions/b.rb +2 -0
  27. data/lib/convenient_service/dependencies/extractions/ce.rb +16 -0
  28. data/lib/convenient_service/dependencies/extractions.rb +1 -0
  29. data/lib/convenient_service/dependencies.rb +6 -1
  30. data/lib/convenient_service/examples/standard/request_params.rb +0 -1
  31. data/lib/convenient_service/examples/standard/v1/request_params.rb +0 -1
  32. data/lib/convenient_service/exception.rb +98 -10
  33. data/lib/convenient_service/feature/configs/standard.rb +8 -0
  34. data/lib/convenient_service/feature/plugins/can_have_entries/commands/define_entries.rb +49 -0
  35. data/lib/convenient_service/feature/plugins/can_have_entries/commands/define_entry.rb +18 -4
  36. data/lib/convenient_service/feature/plugins/can_have_entries/commands.rb +1 -0
  37. data/lib/convenient_service/feature/plugins/can_have_entries/concern.rb +13 -4
  38. data/lib/convenient_service/feature/plugins/can_have_entries/exceptions.rb +14 -4
  39. data/lib/convenient_service/logger.rb +4 -1
  40. data/lib/convenient_service/rspec/helpers/classes/stub_service/entities/result_spec.rb +0 -8
  41. data/lib/convenient_service/rspec/helpers/classes/wrap_method/entities/wrapped_method.rb +5 -5
  42. data/lib/convenient_service/rspec/helpers/classes/wrap_method/exceptions.rb +2 -2
  43. data/lib/convenient_service/rspec/matchers/classes/results/base/entities/validator/commands/validate_result_step.rb +1 -1
  44. data/lib/convenient_service/rspec/matchers/classes/results/base/exceptions.rb +2 -2
  45. data/lib/convenient_service/rspec/primitive_helpers/classes/ignoring_exception/exceptions.rb +2 -2
  46. data/lib/convenient_service/rspec/primitive_helpers/classes/ignoring_exception.rb +1 -1
  47. data/lib/convenient_service/rspec/primitive_matchers/classes/delegate_to/entities/matcher/entities/chainings/sub_matchers/arguments/commands/apply_stub_to_track_delegations.rb +4 -0
  48. data/lib/convenient_service/rspec/primitive_matchers/classes/delegate_to/entities/matcher/entities/chainings_collection/exceptions.rb +7 -7
  49. data/lib/convenient_service/rspec/primitive_matchers/classes/delegate_to/entities/matcher/entities/chainings_collection.rb +3 -3
  50. data/lib/convenient_service/service/plugins/can_have_fallbacks/concern.rb +2 -2
  51. data/lib/convenient_service/service/plugins/can_have_fallbacks/exceptions.rb +4 -4
  52. data/lib/convenient_service/service/plugins/can_have_fallbacks/middleware.rb +1 -1
  53. data/lib/convenient_service/service/plugins/can_have_steps/concern.rb +3 -3
  54. data/lib/convenient_service/service/plugins/can_have_steps/entities/method/commands/define_method_in_container.rb +2 -2
  55. data/lib/convenient_service/service/plugins/can_have_steps/entities/method/concern/instance_methods.rb +1 -1
  56. data/lib/convenient_service/service/plugins/can_have_steps/entities/method/entities/callers/alias.rb +2 -2
  57. data/lib/convenient_service/service/plugins/can_have_steps/entities/method/entities/callers/proc.rb +1 -1
  58. data/lib/convenient_service/service/plugins/can_have_steps/entities/method/entities/callers/raw.rb +1 -1
  59. data/lib/convenient_service/service/plugins/can_have_steps/entities/method/entities/callers/reassignment.rb +2 -2
  60. data/lib/convenient_service/service/plugins/can_have_steps/entities/method/entities/callers/usual.rb +2 -2
  61. data/lib/convenient_service/service/plugins/can_have_steps/entities/method/entities/directions/input.rb +2 -2
  62. data/lib/convenient_service/service/plugins/can_have_steps/entities/method/entities/directions/output.rb +1 -1
  63. data/lib/convenient_service/service/plugins/can_have_steps/entities/method/exceptions.rb +26 -26
  64. data/lib/convenient_service/service/plugins/can_have_steps/entities/step/concern/instance_methods.rb +1 -1
  65. data/lib/convenient_service/service/plugins/can_have_steps/entities/step/exceptions.rb +2 -2
  66. data/lib/convenient_service/service/plugins/can_have_steps/entities/step/plugins/can_be_result_step/can_be_executed/exceptions.rb +2 -2
  67. data/lib/convenient_service/service/plugins/can_have_steps/entities/step/plugins/can_be_result_step/can_be_executed/middleware.rb +1 -1
  68. data/lib/convenient_service/service/plugins/can_have_steps/entities/step/plugins/raises_on_not_result_return_value/exceptions.rb +2 -2
  69. data/lib/convenient_service/service/plugins/can_have_steps/entities/step/plugins/raises_on_not_result_return_value/middleware.rb +1 -1
  70. data/lib/convenient_service/service/plugins/has_inspect/concern.rb +0 -1
  71. data/lib/convenient_service/service/plugins/has_j_send_result/entities/result/plugins/has_j_send_status_and_attributes/concern/instance_methods.rb +7 -0
  72. data/lib/convenient_service/service/plugins/has_j_send_result/entities/result/plugins/has_j_send_status_and_attributes/entities/data/concern/instance_methods.rb +1 -1
  73. data/lib/convenient_service/service/plugins/has_j_send_result/entities/result/plugins/has_j_send_status_and_attributes/entities/status/concern/instance_methods.rb +14 -0
  74. data/lib/convenient_service/service/plugins/has_j_send_result/entities/result/plugins/has_j_send_status_and_attributes/exceptions.rb +2 -2
  75. data/lib/convenient_service/service/plugins/has_j_send_result/entities/result/plugins/raises_on_not_checked_result_status/exceptions.rb +2 -2
  76. data/lib/convenient_service/service/plugins/has_j_send_result/entities/result/plugins/raises_on_not_checked_result_status/middleware.rb +1 -1
  77. data/lib/convenient_service/service/plugins/has_j_send_result/exceptions.rb +2 -2
  78. data/lib/convenient_service/service/plugins/has_j_send_result.rb +39 -0
  79. data/lib/convenient_service/service/plugins/has_j_send_result_short_syntax/error/exceptions.rb +6 -6
  80. data/lib/convenient_service/service/plugins/has_j_send_result_short_syntax/error/middleware.rb +3 -3
  81. data/lib/convenient_service/service/plugins/has_j_send_result_short_syntax/failure/exceptions.rb +6 -6
  82. data/lib/convenient_service/service/plugins/has_j_send_result_short_syntax/failure/middleware.rb +3 -3
  83. data/lib/convenient_service/service/plugins/has_j_send_result_short_syntax/success/commands/refute_kwargs_contain_j_send_and_extra_keys.rb +1 -1
  84. data/lib/convenient_service/service/plugins/has_j_send_result_short_syntax/success/exceptions.rb +2 -2
  85. data/lib/convenient_service/service/plugins/has_j_send_result_status_check_short_syntax/concern.rb +0 -8
  86. data/lib/convenient_service/service/plugins/has_result/concern.rb +1 -1
  87. data/lib/convenient_service/service/plugins/has_result/exceptions.rb +2 -2
  88. data/lib/convenient_service/service/plugins/raises_on_double_result/exceptions.rb +2 -2
  89. data/lib/convenient_service/service/plugins/raises_on_double_result/middleware.rb +1 -1
  90. data/lib/convenient_service/service/plugins/raises_on_not_result_return_value/exceptions.rb +2 -2
  91. data/lib/convenient_service/service/plugins/raises_on_not_result_return_value/middleware.rb +1 -1
  92. data/lib/convenient_service/support/abstract_method/exceptions.rb +2 -2
  93. data/lib/convenient_service/support/abstract_method.rb +1 -1
  94. data/lib/convenient_service/support/backtrace_cleaner.rb +123 -0
  95. data/lib/convenient_service/support/cache/exceptions.rb +2 -2
  96. data/lib/convenient_service/support/cache.rb +1 -1
  97. data/lib/convenient_service/support/castable/exceptions.rb +4 -4
  98. data/lib/convenient_service/support/castable.rb +1 -1
  99. data/lib/convenient_service/support/command.rb +3 -3
  100. data/lib/convenient_service/support/counter.rb +6 -6
  101. data/lib/convenient_service/support/dependency_container/commands/assert_valid_container.rb +1 -1
  102. data/lib/convenient_service/support/dependency_container/commands/assert_valid_method.rb +1 -1
  103. data/lib/convenient_service/support/dependency_container/commands/assert_valid_scope.rb +1 -1
  104. data/lib/convenient_service/support/dependency_container/exceptions.rb +8 -8
  105. data/lib/convenient_service/support/dependency_container/export.rb +1 -1
  106. data/lib/convenient_service/support/finite_loop.rb +6 -6
  107. data/lib/convenient_service/support.rb +1 -0
  108. data/lib/convenient_service/utils/array/exceptions.rb +2 -2
  109. data/lib/convenient_service/utils/array/merge.rb +1 -1
  110. data/lib/convenient_service/utils/hash/assert_valid_keys.rb +1 -1
  111. data/lib/convenient_service/version.rb +1 -1
  112. data/lib/convenient_service.rb +98 -0
  113. metadata +22 -41
  114. data/lib/convenient_service/examples/standard/v1/gemfile/services/print_shell_command.rb +0 -47
@@ -7,15 +7,17 @@ module ConvenientService
7
7
  # @internal
8
8
  # NOTE:
9
9
  # Copied from `rails/rails` without any logic modification.
10
- # Version: v7.0.4.3.
10
+ # Version: v7.1.2.
11
11
  # Wrapped in a namespace `ConvenientService::Dependencies::Extractions::ActiveSupportBacktraceCleaner`.
12
12
  #
13
- # - https://api.rubyonrails.org/v7.0.4.3/classes/ActiveSupport/BacktraceCleaner.html
14
- # - https://github.com/rails/rails/blob/v7.0.4.3/activesupport/lib/active_support/backtrace_cleaner.rb
13
+ # - https://api.rubyonrails.org/v7.1.2/classes/ActiveSupport/BacktraceCleaner.html
14
+ # - https://github.com/rails/rails/blob/v7.1.2/activesupport/lib/active_support/backtrace_cleaner.rb
15
15
  # - https://github.com/marian13/rails/blob/main/activesupport/lib/active_support/backtrace_cleaner.rb
16
16
  # - https://github.com/rails/rails
17
17
  #
18
18
  module ActiveSupportBacktraceCleaner
19
+ # = Backtrace Cleaner
20
+ #
19
21
  # Backtraces often include many lines that are not relevant for the context
20
22
  # under review. This makes it hard to find the signal amongst the backtrace
21
23
  # noise, and adds debugging time. With a BacktraceCleaner, filters and
@@ -34,7 +36,7 @@ module ConvenientService
34
36
  # bc.add_silencer { |line| /puma|rubygems/.match?(line) } # skip any lines from puma or rubygems
35
37
  # bc.clean(exception.backtrace) # perform the cleanup
36
38
  #
37
- # To reconfigure an existing BacktraceCleaner (like the default one in Rails)
39
+ # To reconfigure an existing BacktraceCleaner (like the default one in \Rails)
38
40
  # and show as much data as possible, you can always call
39
41
  # BacktraceCleaner#remove_silencers!, which will restore the
40
42
  # backtrace to a pristine state. If you need to reconfigure an existing
@@ -67,11 +69,29 @@ module ConvenientService
67
69
  end
68
70
  alias :filter :clean
69
71
 
72
+ # Returns the frame with all filters applied.
73
+ # returns +nil+ if the frame was silenced.
74
+ def clean_frame(frame, kind = :silent)
75
+ frame = frame.to_s
76
+ @filters.each do |f|
77
+ frame = f.call(frame.to_s)
78
+ end
79
+
80
+ case kind
81
+ when :silent
82
+ frame unless @silencers.any? { |s| s.call(frame) }
83
+ when :noise
84
+ frame if @silencers.any? { |s| s.call(frame) }
85
+ else
86
+ frame
87
+ end
88
+ end
89
+
70
90
  # Adds a filter from the block provided. Each line in the backtrace will be
71
91
  # mapped against this filter.
72
92
  #
73
93
  # # Will turn "/my/rails/root/app/models/person.rb" into "/app/models/person.rb"
74
- # backtrace_cleaner.add_filter { |line| line.gsub(Rails.root, '') }
94
+ # backtrace_cleaner.add_filter { |line| line.gsub(Rails.root.to_s, '') }
75
95
  def add_filter(&block)
76
96
  @filters << block
77
97
  end
@@ -121,7 +141,7 @@ module ConvenientService
121
141
 
122
142
  def filter_backtrace(backtrace)
123
143
  @filters.each do |f|
124
- backtrace = backtrace.map { |line| f.call(line) }
144
+ backtrace = backtrace.map { |line| f.call(line.to_s) }
125
145
  end
126
146
 
127
147
  backtrace
@@ -129,7 +149,7 @@ module ConvenientService
129
149
 
130
150
  def silence(backtrace)
131
151
  @silencers.each do |s|
132
- backtrace = backtrace.reject { |line| s.call(line) }
152
+ backtrace = backtrace.reject { |line| s.call(line.to_s) }
133
153
  end
134
154
 
135
155
  backtrace
@@ -138,7 +158,7 @@ module ConvenientService
138
158
  def noise(backtrace)
139
159
  backtrace.select do |line|
140
160
  @silencers.any? do |s|
141
- s.call(line)
161
+ s.call(line.to_s)
142
162
  end
143
163
  end
144
164
  end
@@ -4,11 +4,11 @@
4
4
  # @internal
5
5
  # NOTE:
6
6
  # Copied from `rails/rails` without any logic modification.
7
- # Version: v7.0.4.3.
7
+ # Version: v7.1.2.
8
8
  # Wrapped in a namespace `ConvenientService::Dependencies::Extractions::ActiveSupportBacktraceCleaner`.
9
9
  #
10
- # - https://api.rubyonrails.org/v7.0.4.3/classes/ActiveSupport/BacktraceCleaner.html
11
- # - https://github.com/rails/rails/blob/v7.0.4.3/activesupport/lib/active_support/backtrace_cleaner.rb
10
+ # - https://api.rubyonrails.org/v7.1.2/classes/ActiveSupport/BacktraceCleaner.html
11
+ # - https://github.com/rails/rails/blob/v7.1.2/activesupport/lib/active_support/backtrace_cleaner.rb
12
12
  # - https://github.com/marian13/rails/blob/main/activesupport/lib/active_support/backtrace_cleaner.rb
13
13
  # - https://github.com/rails/rails
14
14
  #
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  ##
2
4
  # Works in a similar way as `p`, but for `byebug`.
3
5
  # @see https://ruby-doc.org/core-2.7.0/Kernel.html#method-i-p
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ ##
4
+ # @note `ce` is short for `catch_exception`.
5
+ #
6
+ # @example
7
+ # ce { some_code }
8
+ # e = ce { some_code }
9
+ #
10
+ def ce
11
+ yield
12
+
13
+ nil
14
+ rescue => exception
15
+ exception
16
+ end
@@ -7,4 +7,5 @@
7
7
  # https://github.com/marian13/convenient_service/wiki/Docs:-Dependencies
8
8
  #
9
9
  require_relative "extractions/active_support_concern"
10
+ require_relative "extractions/active_support_backtrace_cleaner"
10
11
  require_relative "extractions/ruby_middleware"
@@ -135,7 +135,7 @@ module ConvenientService
135
135
  # @return [Boolean]
136
136
  # @note Expected to be called from app entry points like `initializers` in Rails.
137
137
  #
138
- # @see https://marian13.github.io/convenient_service_docs/troubleshooting/i18n_translate_wrong_number_of_arguments
138
+ # @see https://userdocs.convenientservice.org/troubleshooting/i18n_translate_wrong_number_of_arguments
139
139
  #
140
140
  def require_has_j_send_result_params_validations_using_active_model_validations
141
141
  ##
@@ -244,6 +244,11 @@ module ConvenientService
244
244
  require_relative "dependencies/extractions/byebug_syntax_highlighting" unless ruby.jruby?
245
245
 
246
246
  require_relative "dependencies/extractions/b" unless ruby.jruby?
247
+
248
+ ##
249
+ #
250
+ #
251
+ require_relative "dependencies/extractions/ce"
247
252
  end
248
253
 
249
254
  ##
@@ -6,7 +6,6 @@ require_relative "request_params/services"
6
6
  require_relative "request_params/utils"
7
7
 
8
8
  ##
9
- # @since 0.3.0
10
9
  #
11
10
  # @internal
12
11
  # Usage example:
@@ -6,7 +6,6 @@ require_relative "request_params/services"
6
6
  require_relative "request_params/utils"
7
7
 
8
8
  ##
9
- # @since 0.3.0
10
9
  #
11
10
  # @internal
12
11
  # Usage example:
@@ -1,16 +1,104 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- ##
4
- # Base class for all `ConvenientService` exceptions.
5
- # Can be used as a catch-all solution, for example:
6
- #
7
- # begin
8
- # any_service.result
9
- # rescue ConvenientService::Exception => exception
10
- # puts exception.message
11
- # end
12
- #
13
3
  module ConvenientService
4
+ ##
5
+ # Base class for all `ConvenientService` exceptions.
6
+ #
7
+ # @example Can be used as a catch-all solution.
8
+ #
9
+ # begin
10
+ # any_service.result
11
+ # rescue ConvenientService::Exception => exception
12
+ # puts exception.message
13
+ # end
14
+ #
14
15
  class Exception < ::StandardError
16
+ class << self
17
+ ##
18
+ # Creates `ConvenientService` exception instance.
19
+ # In contract to `StandardError.new`, may accept `kwargs`.
20
+ # In such a case the descendant must implement `initialize_with_kwargs` to specify the logic of how to generate a `message`.
21
+ # Without arguments behavior is also changed, it calls `initialize_without_arguments` instead of immediately setting `nil` as `message`.
22
+ #
23
+ # @note `initialize_with_kwargs` or `initialize_without_arguments` must call `initialize(message)`, otherwise exception instance won't be properly set up.
24
+ #
25
+ # @overload new
26
+ # Calls `initialize_without_arguments` under the hood to generate the exception message.
27
+ #
28
+ # @api private
29
+ #
30
+ # @return [ConvenientService::Exception]
31
+ #
32
+ # @example Usage.
33
+ # module Exceptions
34
+ # class Foo < ::ConvenientService::Exception
35
+ # def initialize_without_arguments
36
+ # # message = ...
37
+ # initialize(message)
38
+ # end
39
+ # end
40
+ # end
41
+ #
42
+ # ::ConvenientService.raise Foo.new
43
+ #
44
+ # @overload new(message)
45
+ # Calls `initialize(message)` under the hood to set the exception message.
46
+ #
47
+ # @api private
48
+ #
49
+ # @param message [String]
50
+ #
51
+ # @return [ConvenientService::Exception]
52
+ #
53
+ # @example Usage.
54
+ # module Exceptions
55
+ # class Foo < ::ConvenientService::Exception
56
+ # end
57
+ # end
58
+ #
59
+ # ::ConvenientService.raise Foo.new("some message")
60
+ #
61
+ # @overload new(**kwargs)
62
+ # Calls `initialize_with_kwargs(**kwargs)` under the hood to generate the exception message.
63
+ #
64
+ # @api private
65
+ #
66
+ # @param kwargs [Hash{Symbol => Object}]
67
+ # @return [ConvenientService::Exception]
68
+ #
69
+ # @example Usage.
70
+ # module Exceptions
71
+ # class Foo < ::ConvenientService::Exception
72
+ # def initialize_with_kwargs(**kwargs)
73
+ # # message = ...
74
+ # initialize(message)
75
+ # end
76
+ # end
77
+ # end
78
+ #
79
+ # ::ConvenientService.raise Foo.new(foo: :bar, baz: :qux)
80
+ #
81
+ def new(message = nil, **kwargs)
82
+ if message
83
+ super(message)
84
+ elsif kwargs.any?
85
+ allocate.tap { |exception| exception.initialize_with_kwargs(**kwargs) }
86
+ else
87
+ allocate.tap(&:initialize_without_arguments)
88
+ end
89
+ end
90
+ end
91
+
92
+ ##
93
+ # @internal
94
+ # NOTE: Two exceptions are equal when their classes, messages, and backtraces are equal.
95
+ # - https://blog.arkency.com/2015/01/ruby-exceptions-equality
96
+ #
97
+ # TODO: Compare skipping backtraces? Why?
98
+ #
99
+ # def ==(other)
100
+ # # ...
101
+ # end
102
+ ##
15
103
  end
16
104
  end
@@ -14,6 +14,14 @@ module ConvenientService
14
14
 
15
15
  concerns do
16
16
  use ConvenientService::Plugins::Feature::CanHaveEntries::Concern
17
+
18
+ use ConvenientService::Plugins::Common::HasInstanceProxy::Concern
19
+ end
20
+
21
+ middlewares :new, scope: :class do
22
+ use ConvenientService::Plugins::Common::NormalizesEnv::Middleware
23
+
24
+ use ConvenientService::Plugins::Common::HasInstanceProxy::Middleware
17
25
  end
18
26
  end
19
27
  end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConvenientService
4
+ module Feature
5
+ module Plugins
6
+ module CanHaveEntries
7
+ module Commands
8
+ class DefineEntries < Support::Command
9
+ ##
10
+ # @!attribute [r] feature_class
11
+ # @return [Class]
12
+ #
13
+ attr_reader :feature_class
14
+
15
+ ##
16
+ # @!attribute [r] names
17
+ # @return [Array<String, Symbol>]
18
+ #
19
+ attr_reader :names
20
+
21
+ ##
22
+ # @!attribute [r] body
23
+ # @return [Proc, nil]
24
+ #
25
+ attr_reader :body
26
+
27
+ ##
28
+ # @param feature_class [Class]
29
+ # @param names [Array<String, Symbol>]
30
+ # @param body [Proc, nil]
31
+ #
32
+ def initialize(feature_class:, names:, body:)
33
+ @feature_class = feature_class
34
+ @names = names
35
+ @body = body
36
+ end
37
+
38
+ ##
39
+ # @return [Array<String, Symbol>]
40
+ #
41
+ def call
42
+ names.map { |name| Commands::DefineEntry.call(feature_class: feature_class, name: name, body: body) }
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -20,14 +20,14 @@ module ConvenientService
20
20
 
21
21
  ##
22
22
  # @!attribute [r] body
23
- # @return [Proc]
23
+ # @return [Proc, nil]
24
24
  #
25
25
  attr_reader :body
26
26
 
27
27
  ##
28
28
  # @param feature_class [Class]
29
29
  # @param name [String, Symbol]
30
- # @param body [Proc]
30
+ # @param body [Proc, nil]
31
31
  #
32
32
  def initialize(feature_class:, name:, body:)
33
33
  @feature_class = feature_class
@@ -44,13 +44,27 @@ module ConvenientService
44
44
  # That is why `feature_class.class_exec` wrapper is required.
45
45
  #
46
46
  feature_class.class_exec(name) do |name|
47
- define_singleton_method(name) { |*args, **kwargs, &block| new.public_send(name, *args, **kwargs, &block) }
47
+ define_singleton_method(name) { |*args, **kwargs, &block| new.entry(name, *args, **kwargs, &block) }
48
48
  end
49
49
 
50
50
  if body
51
51
  feature_class.define_method(name, &body)
52
52
  else
53
- feature_class.define_method(name) { raise ::ConvenientService::Feature::Plugins::CanHaveEntries::Exceptions::NotDefinedEntryMethod.new(name: __method__, feature: self) }
53
+ feature_class.define_method(name) { |*args, **kwargs, &block| ::ConvenientService.raise ::ConvenientService::Feature::Plugins::CanHaveEntries::Exceptions::NotDefinedEntryMethod.new(name: __method__, feature: self) }
54
+ end
55
+
56
+ ##
57
+ # NOTE: Just `feature_class.instance_proxy_class.define_method` does NOT create a closure for `name`.
58
+ # That is why `feature_class.instance_proxy_class.class_exec` wrapper is required.
59
+ #
60
+ feature_class.instance_proxy_class.class_exec(name) do |name|
61
+ define_method(name) do |*args, **kwargs, &block|
62
+ instance_proxy_target.entry(name, *args, **kwargs, &block)
63
+ end
64
+ end
65
+
66
+ feature_class.instance_proxy_class.define_method(:entry) do |*args, **kwargs, &block|
67
+ instance_proxy_target.entry(*args, **kwargs, &block)
54
68
  end
55
69
 
56
70
  name
@@ -1,3 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "commands/define_entries"
3
4
  require_relative "commands/define_entry"
@@ -7,14 +7,23 @@ module ConvenientService
7
7
  module Concern
8
8
  include Support::Concern
9
9
 
10
+ instance_methods do
11
+ ##
12
+ # @return [Object] Can be any type.
13
+ #
14
+ def entry(...)
15
+ public_send(...)
16
+ end
17
+ end
18
+
10
19
  class_methods do
11
20
  ##
12
- # @param name [String, Symbol]
21
+ # @param names [Array<String, Symbol>]
13
22
  # @param body [Proc, nil]
14
- # @return [String, Symbol]
23
+ # @return [Array<String, Symbol>]
15
24
  #
16
- def entry(name, &body)
17
- Commands::DefineEntry.call(feature_class: self, name: name, body: body)
25
+ def entry(*names, &body)
26
+ Commands::DefineEntries.call(feature_class: self, names: names, body: body)
18
27
  end
19
28
  end
20
29
  end
@@ -6,18 +6,28 @@ module ConvenientService
6
6
  module CanHaveEntries
7
7
  module Exceptions
8
8
  class NotDefinedEntryMethod < ::ConvenientService::Exception
9
- def initialize(name:, feature:)
9
+ def initialize_with_kwargs(name:, feature:)
10
10
  message = <<~TEXT
11
11
  Entry for `#{name}` is registered inside `#{feature.class}` feature, but its corresponding method is NOT defined.
12
12
 
13
- Did you forget to define it? For example:
13
+ Did you forget to define it? For example, using method form:
14
14
 
15
15
  class #{feature.class}
16
16
  entry :#{name}
17
17
 
18
18
  # ...
19
19
 
20
- def #{name}
20
+ def #{name}(...)
21
+ # ...
22
+ end
23
+
24
+ # ...
25
+ end
26
+
27
+ Or using block form:
28
+
29
+ class #{feature.class}
30
+ entry :#{name} do |*args, **kwargs, &block|
21
31
  # ...
22
32
  end
23
33
 
@@ -25,7 +35,7 @@ module ConvenientService
25
35
  end
26
36
  TEXT
27
37
 
28
- super(message)
38
+ initialize(message)
29
39
  end
30
40
  end
31
41
  end
@@ -14,6 +14,9 @@ module ConvenientService
14
14
  ##
15
15
  # @internal
16
16
  # TODO: Custom matcher to track log messages.
17
+ # TODO: Fallback for logging methods. Just like with `#clean` in backtrace cleaner.
18
+ # TODO: Highlight vertical bars.
19
+ # TODO: Highlight tag brackets.
17
20
  #
18
21
  class Logger < ::Logger
19
22
  include ::Singleton
@@ -92,7 +95,7 @@ module ConvenientService
92
95
  #
93
96
  # NOTE: Early return is NOT used, since it is NOT possible to return custom values from `writer` methods.
94
97
  #
95
- # NOTE: `Warning.warn` is chosen over `Kernel.warn` in order to have a simple to track delegation from RSpec.
98
+ # NOTE: `Warning.warn` is chosen over `Kernel.warn` in order to have a simple way to track delegation from RSpec.
96
99
  # - https://ruby-doc.org/core-2.7.0/Warning.html#method-i-warn
97
100
  #
98
101
  # TODO: Tag/prefix for all Convenient Service logs.
@@ -16,7 +16,6 @@ module ConvenientService
16
16
  # @param service_class [Class]
17
17
  # @param chain [Hash]
18
18
  # @return [void]
19
- # @since 0.1.0
20
19
  #
21
20
  def initialize(status:, service_class: nil, chain: {})
22
21
  @status = status
@@ -27,7 +26,6 @@ module ConvenientService
27
26
  ##
28
27
  # @param service_class [Class]
29
28
  # @return [ConvenientService::RSpec::Helpers::Classes::StubService::Entities::ResultSpec]
30
- # @since 0.1.0
31
29
  #
32
30
  def for(service_class)
33
31
  self.class.new(status: status, service_class: service_class, chain: chain)
@@ -36,7 +34,6 @@ module ConvenientService
36
34
  ##
37
35
  # @param data [Hash]
38
36
  # @return [ConvenientService::RSpec::Helpers::Classes::StubService::Entities::ResultSpec]
39
- # @since 0.1.0
40
37
  #
41
38
  def with_data(data)
42
39
  chain[:data] = data
@@ -47,7 +44,6 @@ module ConvenientService
47
44
  ##
48
45
  # @param message [String]
49
46
  # @return [ConvenientService::RSpec::Helpers::Classes::StubService::Entities::ResultSpec]
50
- # @since 0.1.0
51
47
  #
52
48
  def with_message(message)
53
49
  chain[:message] = message
@@ -58,7 +54,6 @@ module ConvenientService
58
54
  ##
59
55
  # @param code [String]
60
56
  # @return [ConvenientService::RSpec::Helpers::Classes::StubService::Entities::ResultSpec]
61
- # @since 0.1.0
62
57
  #
63
58
  def with_code(code)
64
59
  chain[:code] = code
@@ -69,7 +64,6 @@ module ConvenientService
69
64
  ##
70
65
  # @param data [Hash]
71
66
  # @return [ConvenientService::RSpec::Helpers::Classes::StubService::Entities::ResultSpec]
72
- # @since 0.1.0
73
67
  #
74
68
  def and_data(data)
75
69
  chain[:data] = data
@@ -80,7 +74,6 @@ module ConvenientService
80
74
  ##
81
75
  # @param message [String]
82
76
  # @return [ConvenientService::RSpec::Helpers::Classes::StubService::Entities::ResultSpec]
83
- # @since 0.1.0
84
77
  #
85
78
  def and_message(message)
86
79
  chain[:message] = message
@@ -91,7 +84,6 @@ module ConvenientService
91
84
  ##
92
85
  # @param code [String]
93
86
  # @return [ConvenientService::RSpec::Helpers::Classes::StubService::Entities::ResultSpec]
94
- # @since 0.1.0
95
87
  #
96
88
  def and_code(code)
97
89
  chain[:code] = code
@@ -132,7 +132,7 @@ module ConvenientService
132
132
  # @raise [ConvenientService::RSpec::Helpers::Classes::WrapMethod::Exceptions::ChainAttributePreliminaryAccess]
133
133
  #
134
134
  def chain_value
135
- raise Exceptions::ChainAttributePreliminaryAccess.new(attribute: :value) unless chain_called?
135
+ ::ConvenientService.raise Exceptions::ChainAttributePreliminaryAccess.new(attribute: :value) unless chain_called?
136
136
 
137
137
  @chain_value
138
138
  end
@@ -142,7 +142,7 @@ module ConvenientService
142
142
  # @raise [ConvenientService::RSpec::Helpers::Classes::WrapMethod::Exceptions::ChainAttributePreliminaryAccess]
143
143
  #
144
144
  def chain_args
145
- raise Exceptions::ChainAttributePreliminaryAccess.new(attribute: :args) unless chain_called?
145
+ ::ConvenientService.raise Exceptions::ChainAttributePreliminaryAccess.new(attribute: :args) unless chain_called?
146
146
 
147
147
  @chain_arguments.args
148
148
  end
@@ -152,7 +152,7 @@ module ConvenientService
152
152
  # @raise [ConvenientService::RSpec::Helpers::Classes::WrapMethod::Exceptions::ChainAttributePreliminaryAccess]
153
153
  #
154
154
  def chain_kwargs
155
- raise Exceptions::ChainAttributePreliminaryAccess.new(attribute: :kwargs) unless chain_called?
155
+ ::ConvenientService.raise Exceptions::ChainAttributePreliminaryAccess.new(attribute: :kwargs) unless chain_called?
156
156
 
157
157
  @chain_arguments.kwargs
158
158
  end
@@ -162,7 +162,7 @@ module ConvenientService
162
162
  # @raise [ConvenientService::RSpec::Helpers::Classes::WrapMethod::Exceptions::ChainAttributePreliminaryAccess]
163
163
  #
164
164
  def chain_block
165
- raise Exceptions::ChainAttributePreliminaryAccess.new(attribute: :block) unless chain_called?
165
+ ::ConvenientService.raise Exceptions::ChainAttributePreliminaryAccess.new(attribute: :block) unless chain_called?
166
166
 
167
167
  @chain_arguments.block
168
168
  end
@@ -172,7 +172,7 @@ module ConvenientService
172
172
  # @raise [ConvenientService::RSpec::Helpers::Classes::WrapMethod::Exceptions::ChainAttributePreliminaryAccess]
173
173
  #
174
174
  def chain_exception
175
- raise Exceptions::ChainAttributePreliminaryAccess.new(attribute: :exception) unless chain_called?
175
+ ::ConvenientService.raise Exceptions::ChainAttributePreliminaryAccess.new(attribute: :exception) unless chain_called?
176
176
 
177
177
  @chain_exception
178
178
  end
@@ -11,12 +11,12 @@ module ConvenientService
11
11
  # @param attribute [Symbol]
12
12
  # @return [void]
13
13
  #
14
- def initialize(attribute:)
14
+ def initialize_with_kwargs(attribute:)
15
15
  message = <<~TEXT
16
16
  Chain attribute `#{attribute}` is accessed before the chain is called.
17
17
  TEXT
18
18
 
19
- super(message)
19
+ initialize(message)
20
20
  end
21
21
  end
22
22
  end
@@ -76,7 +76,7 @@ module ConvenientService
76
76
  when :result then match_result_method_step?
77
77
  when ::Symbol then match_method_step?
78
78
  when nil then match_without_step?
79
- else raise Exceptions::InvalidStep.new(step: step)
79
+ else ::ConvenientService.raise Exceptions::InvalidStep.new(step: step)
80
80
  end
81
81
  end
82
82
 
@@ -14,7 +14,7 @@ module ConvenientService
14
14
  # @param step [ConvenientService::Service, Symbol]
15
15
  # @return [void]
16
16
  #
17
- def initialize(step:)
17
+ def initialize_with_kwargs(step:)
18
18
  message = <<~TEXT
19
19
  Step `#{step}` is NOT valid.
20
20
 
@@ -29,7 +29,7 @@ module ConvenientService
29
29
  be_success.without_step
30
30
  TEXT
31
31
 
32
- super(message)
32
+ initialize(message)
33
33
  end
34
34
  end
35
35
  end
@@ -11,12 +11,12 @@ module ConvenientService
11
11
  # @param exception [StandardError]
12
12
  # @return [void]
13
13
  #
14
- def initialize(exception:)
14
+ def initialize_with_kwargs(exception:)
15
15
  message = <<~TEXT
16
16
  Exception `#{exception}` is NOT raised. That is why it is NOT ignored.
17
17
  TEXT
18
18
 
19
- super(message)
19
+ initialize(message)
20
20
  end
21
21
  end
22
22
  end