convenient_service 0.5.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -0
- data/LICENSE.txt +1 -1
- data/README.md +1 -1
- data/ROADMAP.md +14 -3
- data/Taskfile.yml +30 -0
- data/convenient_service.gemspec +3 -1
- data/lib/convenient_service/aliases.rb +1 -0
- data/lib/convenient_service/common/plugins/assigns_attributes_in_constructor/using_active_model_attribute_assignment/concern.rb +0 -2
- data/lib/convenient_service/common/plugins/assigns_attributes_in_constructor/using_dry_initializer/concern.rb +0 -2
- data/lib/convenient_service/core/entities/config/entities/method_middlewares/entities/container/commands/resolve_methods_middlewares_callers.rb +1 -24
- data/lib/convenient_service/dependencies.rb +7 -0
- data/lib/convenient_service/examples/dry/gemfile/dry_service/config.rb +3 -1
- data/lib/convenient_service/examples/rails/gemfile/rails_service/config.rb +3 -1
- data/lib/convenient_service/examples/standard/request_params/constants.rb +15 -0
- data/lib/convenient_service/examples/standard/request_params/entities/description.rb +40 -0
- data/lib/convenient_service/examples/standard/request_params/entities/format.rb +40 -0
- data/lib/convenient_service/examples/standard/request_params/entities/id.rb +47 -0
- data/lib/convenient_service/examples/standard/request_params/entities/logger.rb +21 -0
- data/lib/convenient_service/examples/standard/request_params/entities/request.rb +23 -0
- data/lib/convenient_service/examples/standard/request_params/entities/source.rb +40 -0
- data/lib/convenient_service/examples/standard/request_params/entities/tag.rb +40 -0
- data/lib/convenient_service/examples/standard/request_params/entities/title.rb +40 -0
- data/lib/convenient_service/examples/standard/request_params/entities.rb +11 -0
- data/lib/convenient_service/examples/standard/request_params/services/apply_default_param_values.rb +26 -0
- data/lib/convenient_service/examples/standard/request_params/services/cast_params.rb +38 -0
- data/lib/convenient_service/examples/standard/request_params/services/extract_params_from_body.rb +70 -0
- data/lib/convenient_service/examples/standard/request_params/services/extract_params_from_path.rb +62 -0
- data/lib/convenient_service/examples/standard/request_params/services/filter_out_unpermitted_params.rb +26 -0
- data/lib/convenient_service/examples/standard/request_params/services/log_request_params.rb +54 -0
- data/lib/convenient_service/examples/standard/request_params/services/merge_params.rb +26 -0
- data/lib/convenient_service/examples/standard/request_params/services/prepare.rb +65 -0
- data/lib/convenient_service/examples/standard/request_params/services/validate_casted_params.rb +94 -0
- data/lib/convenient_service/examples/standard/request_params/services/validate_uncasted_params.rb +72 -0
- data/lib/convenient_service/examples/standard/request_params/services.rb +13 -0
- data/lib/convenient_service/examples/standard/request_params/utils/array/wrap.rb +46 -0
- data/lib/convenient_service/examples/standard/request_params/utils/array.rb +21 -0
- data/lib/convenient_service/examples/standard/request_params/utils/http/request/parse_body.rb +42 -0
- data/lib/convenient_service/examples/standard/request_params/utils/http/request/parse_path.rb +40 -0
- data/lib/convenient_service/examples/standard/request_params/utils/http/request.rb +28 -0
- data/lib/convenient_service/examples/standard/request_params/utils/http.rb +3 -0
- data/lib/convenient_service/examples/standard/request_params/utils/integer/safe_parse.rb +31 -0
- data/lib/convenient_service/examples/standard/request_params/utils/integer.rb +25 -0
- data/lib/convenient_service/examples/standard/request_params/utils/json/safe_parse.rb +40 -0
- data/lib/convenient_service/examples/standard/request_params/utils/json.rb +21 -0
- data/lib/convenient_service/examples/standard/request_params/utils/object/blank.rb +34 -0
- data/lib/convenient_service/examples/standard/request_params/utils/object/present.rb +31 -0
- data/lib/convenient_service/examples/standard/request_params/utils/object.rb +26 -0
- data/lib/convenient_service/examples/standard/request_params/utils.rb +7 -0
- data/lib/convenient_service/examples/standard/request_params.rb +48 -0
- data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/commands/generate_printable_method.rb +50 -0
- data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/commands.rb +3 -0
- data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/sub_matchers/arguments/commands/apply_stub_to_track_delegations.rb +78 -0
- data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/sub_matchers/arguments/commands/generate_printable_arguments.rb +100 -0
- data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/sub_matchers/arguments/commands.rb +4 -0
- data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/sub_matchers/arguments.rb +95 -0
- data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/sub_matchers/base.rb +87 -0
- data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/sub_matchers/return_its_value.rb +129 -0
- data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/sub_matchers/with_any_arguments.rb +37 -0
- data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/sub_matchers/with_concrete_arguments.rb +37 -0
- data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/sub_matchers/without_arguments.rb +37 -0
- data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/values/base.rb +41 -0
- data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/values/with_calling_original.rb +30 -0
- data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/values/without_calling_original.rb +30 -0
- data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings.rb +12 -0
- data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings_collection/errors.rb +57 -0
- data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings_collection.rb +171 -0
- data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/delegation.rb +79 -0
- data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities.rb +5 -0
- data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher.rb +276 -0
- data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities.rb +3 -0
- data/lib/convenient_service/rspec/matchers/custom/delegate_to.rb +58 -234
- data/lib/convenient_service/rspec/matchers/custom/singleton_prepend_module.rb +79 -0
- data/lib/convenient_service/rspec/matchers/custom.rb +1 -0
- data/lib/convenient_service/rspec/matchers/singleton_prepend_module.rb +13 -0
- data/lib/convenient_service/rspec/matchers.rb +2 -0
- data/lib/convenient_service/service/plugins/has_result_steps/concern.rb +25 -0
- data/lib/convenient_service/service/plugins/has_result_steps/entities/method/commands/cast_method.rb +3 -0
- data/lib/convenient_service/service/plugins/has_result_steps/entities/method/commands/cast_method_caller.rb +8 -1
- data/lib/convenient_service/service/plugins/has_result_steps/entities/method/commands/cast_method_direction.rb +3 -0
- data/lib/convenient_service/service/plugins/has_result_steps/entities/method/commands/cast_method_key.rb +8 -1
- data/lib/convenient_service/service/plugins/has_result_steps/entities/method/commands/cast_method_name.rb +9 -5
- data/lib/convenient_service/service/plugins/has_result_steps/entities/method/concern/instance_methods.rb +4 -0
- data/lib/convenient_service/service/plugins/has_result_steps/entities/method/entities/callers/base.rb +4 -0
- data/lib/convenient_service/service/plugins/has_result_steps/entities/method/entities/callers/reassignment/commands/define_method_in_container.rb +74 -0
- data/lib/convenient_service/service/plugins/has_result_steps/entities/method/entities/callers/reassignment/commands.rb +3 -0
- data/lib/convenient_service/service/plugins/has_result_steps/entities/method/entities/callers/reassignment.rb +50 -0
- data/lib/convenient_service/service/plugins/has_result_steps/entities/method/entities/callers.rb +1 -0
- data/lib/convenient_service/service/plugins/has_result_steps/entities/method/entities/values/reassignment.rb +43 -0
- data/lib/convenient_service/service/plugins/has_result_steps/entities/method/entities/values.rb +3 -0
- data/lib/convenient_service/service/plugins/has_result_steps/entities/method/entities.rb +1 -0
- data/lib/convenient_service/service/plugins/has_result_steps/entities/method/errors.rb +22 -0
- data/lib/convenient_service/service/plugins/has_result_steps/entities/step/concern/instance_methods.rb +13 -2
- data/lib/convenient_service/support/arguments/null_arguments.rb +28 -0
- data/lib/convenient_service/support/arguments.rb +87 -0
- data/lib/convenient_service/support/dependency_container/commands/assert_valid_scope.rb +32 -0
- data/lib/convenient_service/support/dependency_container/commands/import_method.rb +13 -23
- data/lib/convenient_service/support/dependency_container/commands.rb +1 -0
- data/lib/convenient_service/support/dependency_container/constants.rb +4 -2
- data/lib/convenient_service/support/dependency_container/entities/method.rb +2 -9
- data/lib/convenient_service/support/dependency_container/entities/namespace_collection.rb +0 -9
- data/lib/convenient_service/support/dependency_container/errors.rb +25 -0
- data/lib/convenient_service/support/dependency_container/export.rb +8 -0
- data/lib/convenient_service/support/dependency_container/import.rb +3 -1
- data/lib/convenient_service/support/version/null_version.rb +7 -0
- data/lib/convenient_service/support/version.rb +11 -1
- data/lib/convenient_service/support.rb +1 -0
- data/lib/convenient_service/utils/object/instance_variable_delete.rb +41 -0
- data/lib/convenient_service/utils/object/instance_variable_fetch.rb +4 -0
- data/lib/convenient_service/utils/object.rb +9 -0
- data/lib/convenient_service/utils/proc/display.rb +43 -0
- data/lib/convenient_service/utils/proc.rb +5 -0
- data/lib/convenient_service/version.rb +1 -1
- metadata +101 -4
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
##
|
4
|
+
#
|
5
|
+
#
|
6
|
+
module ConvenientService
|
7
|
+
module Examples
|
8
|
+
module Standard
|
9
|
+
module RequestParams
|
10
|
+
module Utils
|
11
|
+
module Integer
|
12
|
+
##
|
13
|
+
# TODO: Specs.
|
14
|
+
#
|
15
|
+
class SafeParse < Support::Command
|
16
|
+
attr_reader :object
|
17
|
+
|
18
|
+
def initialize(object)
|
19
|
+
@object = object
|
20
|
+
end
|
21
|
+
|
22
|
+
def call
|
23
|
+
::Kernel.Integer(object, exception: false)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
##
|
4
|
+
# @internal
|
5
|
+
# NOTE: `require_relative "integer/safe_parse"` does NOT work. Why?
|
6
|
+
#
|
7
|
+
require_relative "integer/safe_parse"
|
8
|
+
|
9
|
+
module ConvenientService
|
10
|
+
module Examples
|
11
|
+
module Standard
|
12
|
+
module RequestParams
|
13
|
+
module Utils
|
14
|
+
module Integer
|
15
|
+
class << self
|
16
|
+
def safe_parse(string)
|
17
|
+
SafeParse.call(string)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
##
|
4
|
+
# Tries to parse a JSON string and return the corresponding JSON object (Ruby hash, array, etc).
|
5
|
+
# Returns default value when fails to parse (default value is set to `nil` by default).
|
6
|
+
#
|
7
|
+
# IMPORTANT: `JSON::ParseError` is not the only exception that can be raised by `JSON.parse`.
|
8
|
+
# Check this link (`Ctrl + f' for `error'):
|
9
|
+
# https://github.com/ruby/ruby/blob/master/ext/json/lib/json.rb
|
10
|
+
#
|
11
|
+
module ConvenientService
|
12
|
+
module Examples
|
13
|
+
module Standard
|
14
|
+
module RequestParams
|
15
|
+
module Utils
|
16
|
+
module JSON
|
17
|
+
class SafeParse < Support::Command
|
18
|
+
attr_reader :json_string, :default_value
|
19
|
+
|
20
|
+
def initialize(json_string, default_value: nil)
|
21
|
+
@json_string = json_string
|
22
|
+
@default_value = default_value
|
23
|
+
end
|
24
|
+
|
25
|
+
def call
|
26
|
+
return default_value unless json_string.instance_of?(::String)
|
27
|
+
|
28
|
+
begin
|
29
|
+
::JSON.parse(json_string)
|
30
|
+
rescue
|
31
|
+
default_value
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "json/safe_parse"
|
4
|
+
|
5
|
+
module ConvenientService
|
6
|
+
module Examples
|
7
|
+
module Standard
|
8
|
+
module RequestParams
|
9
|
+
module Utils
|
10
|
+
module JSON
|
11
|
+
class << self
|
12
|
+
def safe_parse(json_string, default_value: nil)
|
13
|
+
SafeParse.call(json_string, default_value: default_value)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
##
|
4
|
+
#
|
5
|
+
#
|
6
|
+
module ConvenientService
|
7
|
+
module Examples
|
8
|
+
module Standard
|
9
|
+
module RequestParams
|
10
|
+
module Utils
|
11
|
+
module Object
|
12
|
+
##
|
13
|
+
# TODO: Specs.
|
14
|
+
#
|
15
|
+
class Blank < Support::Command
|
16
|
+
attr_reader :object
|
17
|
+
|
18
|
+
def initialize(object)
|
19
|
+
@object = object
|
20
|
+
end
|
21
|
+
|
22
|
+
##
|
23
|
+
# https://api.rubyonrails.org/classes/Object.html#method-i-blank-3F
|
24
|
+
#
|
25
|
+
def call
|
26
|
+
object.respond_to?(:empty?) ? !!object.empty? : !object
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
##
|
4
|
+
#
|
5
|
+
#
|
6
|
+
module ConvenientService
|
7
|
+
module Examples
|
8
|
+
module Standard
|
9
|
+
module RequestParams
|
10
|
+
module Utils
|
11
|
+
module Object
|
12
|
+
##
|
13
|
+
# TODO: Specs.
|
14
|
+
#
|
15
|
+
class Present < Support::Command
|
16
|
+
attr_reader :object
|
17
|
+
|
18
|
+
def initialize(object)
|
19
|
+
@object = object
|
20
|
+
end
|
21
|
+
|
22
|
+
def call
|
23
|
+
Utils::Object.blank?(object)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "object/blank"
|
4
|
+
require_relative "object/present"
|
5
|
+
|
6
|
+
module ConvenientService
|
7
|
+
module Examples
|
8
|
+
module Standard
|
9
|
+
module RequestParams
|
10
|
+
module Utils
|
11
|
+
module Object
|
12
|
+
class << self
|
13
|
+
def blank?(object)
|
14
|
+
Blank.call(object)
|
15
|
+
end
|
16
|
+
|
17
|
+
def present?(object)
|
18
|
+
Present.call(object)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "request_params/constants"
|
4
|
+
require_relative "request_params/entities"
|
5
|
+
require_relative "request_params/services"
|
6
|
+
require_relative "request_params/utils"
|
7
|
+
|
8
|
+
##
|
9
|
+
# @since 0.3.0
|
10
|
+
#
|
11
|
+
# @internal
|
12
|
+
# Usage example:
|
13
|
+
#
|
14
|
+
# http_string =
|
15
|
+
# <<~TEXT
|
16
|
+
# POST /rules/1000000.json HTTP/1.1
|
17
|
+
# User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36
|
18
|
+
# Host: code-review.com
|
19
|
+
# Content-Type: application/json; charset=utf-8
|
20
|
+
# Content-Length: 134
|
21
|
+
# Accept-Language: en-us
|
22
|
+
# Accept-Encoding: gzip, deflate
|
23
|
+
# Connection: Keep-Alive
|
24
|
+
#
|
25
|
+
# {"title":"Avoid error shadowing","description":"Check the official User Docs","tags":["error-shadowing"]}
|
26
|
+
# TEXT
|
27
|
+
#
|
28
|
+
# request = ConvenientService::Examples::Standard::RequestParams::Entities::Request.new(http_string:)
|
29
|
+
#
|
30
|
+
# ConvenientService::Examples::Standard::RequestParams.prepare(request)
|
31
|
+
#
|
32
|
+
# - https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types
|
33
|
+
# - https://www.whatismybrowser.com/guides/the-latest-user-agent/chrome
|
34
|
+
# - https://www.tutorialspoint.com/http/http_requests.htm
|
35
|
+
#
|
36
|
+
module ConvenientService
|
37
|
+
module Examples
|
38
|
+
module Standard
|
39
|
+
module RequestParams
|
40
|
+
class << self
|
41
|
+
def prepare(request)
|
42
|
+
Services::Prepare[request: request]
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ConvenientService
|
4
|
+
module RSpec
|
5
|
+
module Matchers
|
6
|
+
module Custom
|
7
|
+
class DelegateTo
|
8
|
+
module Entities
|
9
|
+
class Matcher
|
10
|
+
module Commands
|
11
|
+
class GeneratePrintableMethod < Support::Command
|
12
|
+
##
|
13
|
+
# @!attribute [r] object
|
14
|
+
# @return [Object] Can be any type.
|
15
|
+
#
|
16
|
+
attr_reader :object
|
17
|
+
|
18
|
+
##
|
19
|
+
# @!attribute [r] method
|
20
|
+
# @return [Symbol, String]
|
21
|
+
#
|
22
|
+
attr_reader :method
|
23
|
+
|
24
|
+
##
|
25
|
+
# @param object [Object] Can by any type.
|
26
|
+
# @param method [Symbol, String]
|
27
|
+
# @return [void]
|
28
|
+
#
|
29
|
+
def initialize(object:, method:)
|
30
|
+
@object = object
|
31
|
+
@method = method
|
32
|
+
end
|
33
|
+
|
34
|
+
def call
|
35
|
+
case Utils::Object.resolve_type(object)
|
36
|
+
when "class", "module"
|
37
|
+
"#{object}.#{method}"
|
38
|
+
when "instance"
|
39
|
+
"#{object.class}##{method}"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ConvenientService
|
4
|
+
module RSpec
|
5
|
+
module Matchers
|
6
|
+
module Custom
|
7
|
+
class DelegateTo
|
8
|
+
module Entities
|
9
|
+
class Matcher
|
10
|
+
module Entities
|
11
|
+
module Chainings
|
12
|
+
module SubMatchers
|
13
|
+
class Arguments < Chainings::SubMatchers::Base
|
14
|
+
module Commands
|
15
|
+
class ApplyStubToTrackDelegations < Support::Command
|
16
|
+
##
|
17
|
+
# @internal
|
18
|
+
# NOTE: `include ::RSpec::Mocks::ExampleMethods`.
|
19
|
+
# - https://github.com/rspec/rspec-mocks/blob/v3.11.1/lib/rspec/mocks/example_methods.rb
|
20
|
+
# - https://github.com/rspec/rspec-mocks/blob/main/lib/rspec/mocks/example_methods.rb
|
21
|
+
#
|
22
|
+
include ::RSpec::Mocks::ExampleMethods
|
23
|
+
|
24
|
+
##
|
25
|
+
# @return [ConvenientService::RSpec::Matchers::Custom::DelegateTo]
|
26
|
+
#
|
27
|
+
attr_reader :matcher
|
28
|
+
|
29
|
+
##
|
30
|
+
# @param matcher [ConvenientService::RSpec::Matchers::Custom::DelegateTo]
|
31
|
+
# @return [void]
|
32
|
+
#
|
33
|
+
def initialize(matcher:)
|
34
|
+
@matcher = matcher
|
35
|
+
end
|
36
|
+
|
37
|
+
def call
|
38
|
+
##
|
39
|
+
# NOTE: RSpec `allow(object).to receive(method).with(*args, **kwargs)` does NOT support block.
|
40
|
+
# https://github.com/rspec/rspec-mocks/issues/1182#issuecomment-679820352
|
41
|
+
#
|
42
|
+
# NOTE: RSpec `allow(object).to receive(method) do` does NOT support `and_call_original`.
|
43
|
+
# https://github.com/rspec/rspec-mocks/issues/774#issuecomment-54245277
|
44
|
+
#
|
45
|
+
# NOTE: That is why `and_wrap_original` is used.
|
46
|
+
# https://relishapp.com/rspec/rspec-mocks/docs/configuring-responses/wrapping-the-original-implementation
|
47
|
+
#
|
48
|
+
allow(matcher.object).to receive(matcher.method).and_wrap_original do |original, *actual_args, **actual_kwargs, &actual_block|
|
49
|
+
##
|
50
|
+
# TODO: Add backtrace for easier reason tracing.
|
51
|
+
#
|
52
|
+
matcher.delegations << Entities::Delegation.new(
|
53
|
+
object: matcher.object,
|
54
|
+
method: matcher.method,
|
55
|
+
args: actual_args,
|
56
|
+
kwargs: actual_kwargs,
|
57
|
+
block: actual_block
|
58
|
+
)
|
59
|
+
|
60
|
+
##
|
61
|
+
# NOTE: Imitates `and_call_original`.
|
62
|
+
#
|
63
|
+
original.call(*actual_args, **actual_kwargs, &actual_block) if matcher.should_call_original?
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ConvenientService
|
4
|
+
module RSpec
|
5
|
+
module Matchers
|
6
|
+
module Custom
|
7
|
+
class DelegateTo
|
8
|
+
module Entities
|
9
|
+
class Matcher
|
10
|
+
module Entities
|
11
|
+
module Chainings
|
12
|
+
module SubMatchers
|
13
|
+
class Arguments < Chainings::SubMatchers::Base
|
14
|
+
module Commands
|
15
|
+
class GeneratePrintableArguments < Support::Command
|
16
|
+
include Support::Delegate
|
17
|
+
|
18
|
+
##
|
19
|
+
# @!attribute [r] arguments
|
20
|
+
# @return [ConvenientService::RSpec::Matchers::Custom::DelegateTo::Entities::Arguments]
|
21
|
+
#
|
22
|
+
attr_reader :arguments
|
23
|
+
|
24
|
+
##
|
25
|
+
# @return [Array]
|
26
|
+
#
|
27
|
+
delegate :args, to: :arguments
|
28
|
+
|
29
|
+
##
|
30
|
+
# @return [Hash]
|
31
|
+
#
|
32
|
+
delegate :kwargs, to: :arguments
|
33
|
+
|
34
|
+
##
|
35
|
+
# @return [Proc]
|
36
|
+
#
|
37
|
+
delegate :block, to: :arguments
|
38
|
+
|
39
|
+
##
|
40
|
+
# @param arguments [ConvenientService::RSpec::Matchers::Custom::DelegateTo::Entities::Arguments]
|
41
|
+
#
|
42
|
+
def initialize(arguments:)
|
43
|
+
@arguments = arguments
|
44
|
+
end
|
45
|
+
|
46
|
+
##
|
47
|
+
# @return [String]
|
48
|
+
#
|
49
|
+
def call
|
50
|
+
return "" if arguments.null_arguments?
|
51
|
+
|
52
|
+
text =
|
53
|
+
if args.any? && kwargs.any?
|
54
|
+
"(#{printable_args}, #{printable_kwargs})"
|
55
|
+
elsif args.any?
|
56
|
+
"(#{printable_args})"
|
57
|
+
elsif kwargs.any?
|
58
|
+
"(#{printable_kwargs})"
|
59
|
+
else
|
60
|
+
"()"
|
61
|
+
end
|
62
|
+
|
63
|
+
text += " #{printable_block}" if block
|
64
|
+
|
65
|
+
text
|
66
|
+
end
|
67
|
+
|
68
|
+
##
|
69
|
+
# @return [String]
|
70
|
+
#
|
71
|
+
def printable_args
|
72
|
+
args.map(&:inspect).join(", ")
|
73
|
+
end
|
74
|
+
|
75
|
+
##
|
76
|
+
# @return [String]
|
77
|
+
#
|
78
|
+
def printable_kwargs
|
79
|
+
kwargs.map { |key, value| "#{key}: #{value.inspect}" }.join(", ")
|
80
|
+
end
|
81
|
+
|
82
|
+
##
|
83
|
+
# @return [String]
|
84
|
+
#
|
85
|
+
def printable_block
|
86
|
+
block ? Utils::Proc.display(block) : ""
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "arguments/commands"
|
4
|
+
|
5
|
+
module ConvenientService
|
6
|
+
module RSpec
|
7
|
+
module Matchers
|
8
|
+
module Custom
|
9
|
+
class DelegateTo
|
10
|
+
module Entities
|
11
|
+
class Matcher
|
12
|
+
module Entities
|
13
|
+
module Chainings
|
14
|
+
module SubMatchers
|
15
|
+
class Arguments < Chainings::SubMatchers::Base
|
16
|
+
##
|
17
|
+
# @return [void]
|
18
|
+
#
|
19
|
+
def apply_stubs!
|
20
|
+
Commands::ApplyStubToTrackDelegations.call(matcher: matcher)
|
21
|
+
end
|
22
|
+
|
23
|
+
##
|
24
|
+
# @param block_expectation_value [Object]
|
25
|
+
# @return [Boolean]
|
26
|
+
#
|
27
|
+
def matches?(block_expectation_value)
|
28
|
+
super
|
29
|
+
|
30
|
+
matcher.delegations.any? { |delegation| matches_arguments?(delegation.arguments) }
|
31
|
+
end
|
32
|
+
|
33
|
+
##
|
34
|
+
# @return [String]
|
35
|
+
#
|
36
|
+
# @internal
|
37
|
+
# TODO: Prettier message.
|
38
|
+
#
|
39
|
+
def failure_message
|
40
|
+
<<~MESSAGE.chomp
|
41
|
+
#{failure_message_expected_part}
|
42
|
+
|
43
|
+
#{failure_message_got_part}
|
44
|
+
MESSAGE
|
45
|
+
end
|
46
|
+
|
47
|
+
##
|
48
|
+
# @return [String]
|
49
|
+
#
|
50
|
+
def failure_message_when_negated
|
51
|
+
"expected `#{matcher.printable_block_expectation}` NOT to delegate to `#{matcher.printable_method}` #{printable_expected_arguments} at least once, but it did."
|
52
|
+
end
|
53
|
+
|
54
|
+
##
|
55
|
+
# @return [String]
|
56
|
+
#
|
57
|
+
def printable_actual_arguments
|
58
|
+
return "" if matcher.delegations.none?
|
59
|
+
|
60
|
+
matcher.delegations
|
61
|
+
.map { |delegation| Commands::GeneratePrintableArguments.call(arguments: delegation.arguments) }
|
62
|
+
.join(", ")
|
63
|
+
.prepend("with ")
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
def failure_message_expected_part
|
69
|
+
"expected `#{matcher.printable_block_expectation}` to delegate to `#{matcher.printable_method}` #{printable_expected_arguments} at least once, but it didn't."
|
70
|
+
end
|
71
|
+
|
72
|
+
##
|
73
|
+
# @return [String]
|
74
|
+
#
|
75
|
+
# @internal
|
76
|
+
# TODO: String diff.
|
77
|
+
#
|
78
|
+
def failure_message_got_part
|
79
|
+
if matcher.delegations.any?
|
80
|
+
"got delegated #{printable_actual_arguments}"
|
81
|
+
else
|
82
|
+
"got not delegated at all"
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|