convenient_service 0.4.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (127) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +16 -0
  3. data/LICENSE.txt +1 -1
  4. data/README.md +1 -1
  5. data/ROADMAP.md +14 -3
  6. data/Taskfile.yml +30 -0
  7. data/convenient_service.gemspec +4 -2
  8. data/lib/convenient_service/aliases.rb +6 -0
  9. data/lib/convenient_service/common/plugins/assigns_attributes_in_constructor/using_active_model_attribute_assignment/concern.rb +0 -2
  10. data/lib/convenient_service/common/plugins/assigns_attributes_in_constructor/using_dry_initializer/concern.rb +0 -2
  11. data/lib/convenient_service/core/entities/config/entities/method_middlewares/entities/container/commands/resolve_methods_middlewares_callers.rb +1 -24
  12. data/lib/convenient_service/dependencies.rb +7 -0
  13. data/lib/convenient_service/examples/dry/gemfile/dry_service/config.rb +3 -1
  14. data/lib/convenient_service/examples/rails/gemfile/rails_service/config.rb +3 -1
  15. data/lib/convenient_service/examples/standard/request_params/constants.rb +15 -0
  16. data/lib/convenient_service/examples/standard/request_params/entities/description.rb +40 -0
  17. data/lib/convenient_service/examples/standard/request_params/entities/format.rb +40 -0
  18. data/lib/convenient_service/examples/standard/request_params/entities/id.rb +47 -0
  19. data/lib/convenient_service/examples/standard/request_params/entities/logger.rb +21 -0
  20. data/lib/convenient_service/examples/standard/request_params/entities/request.rb +23 -0
  21. data/lib/convenient_service/examples/standard/request_params/entities/source.rb +40 -0
  22. data/lib/convenient_service/examples/standard/request_params/entities/tag.rb +40 -0
  23. data/lib/convenient_service/examples/standard/request_params/entities/title.rb +40 -0
  24. data/lib/convenient_service/examples/standard/request_params/entities.rb +11 -0
  25. data/lib/convenient_service/examples/standard/request_params/services/apply_default_param_values.rb +26 -0
  26. data/lib/convenient_service/examples/standard/request_params/services/cast_params.rb +38 -0
  27. data/lib/convenient_service/examples/standard/request_params/services/extract_params_from_body.rb +70 -0
  28. data/lib/convenient_service/examples/standard/request_params/services/extract_params_from_path.rb +62 -0
  29. data/lib/convenient_service/examples/standard/request_params/services/filter_out_unpermitted_params.rb +26 -0
  30. data/lib/convenient_service/examples/standard/request_params/services/log_request_params.rb +54 -0
  31. data/lib/convenient_service/examples/standard/request_params/services/merge_params.rb +26 -0
  32. data/lib/convenient_service/examples/standard/request_params/services/prepare.rb +65 -0
  33. data/lib/convenient_service/examples/standard/request_params/services/validate_casted_params.rb +94 -0
  34. data/lib/convenient_service/examples/standard/request_params/services/validate_uncasted_params.rb +72 -0
  35. data/lib/convenient_service/examples/standard/request_params/services.rb +13 -0
  36. data/lib/convenient_service/examples/standard/request_params/utils/array/wrap.rb +46 -0
  37. data/lib/convenient_service/examples/standard/request_params/utils/array.rb +21 -0
  38. data/lib/convenient_service/examples/standard/request_params/utils/http/request/parse_body.rb +42 -0
  39. data/lib/convenient_service/examples/standard/request_params/utils/http/request/parse_path.rb +40 -0
  40. data/lib/convenient_service/examples/standard/request_params/utils/http/request.rb +28 -0
  41. data/lib/convenient_service/examples/standard/request_params/utils/http.rb +3 -0
  42. data/lib/convenient_service/examples/standard/request_params/utils/integer/safe_parse.rb +31 -0
  43. data/lib/convenient_service/examples/standard/request_params/utils/integer.rb +25 -0
  44. data/lib/convenient_service/examples/standard/request_params/utils/json/safe_parse.rb +40 -0
  45. data/lib/convenient_service/examples/standard/request_params/utils/json.rb +21 -0
  46. data/lib/convenient_service/examples/standard/request_params/utils/object/blank.rb +34 -0
  47. data/lib/convenient_service/examples/standard/request_params/utils/object/present.rb +31 -0
  48. data/lib/convenient_service/examples/standard/request_params/utils/object.rb +26 -0
  49. data/lib/convenient_service/examples/standard/request_params/utils.rb +7 -0
  50. data/lib/convenient_service/examples/standard/request_params.rb +48 -0
  51. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/commands/generate_printable_method.rb +50 -0
  52. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/commands.rb +3 -0
  53. 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
  54. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/sub_matchers/arguments/commands/generate_printable_arguments.rb +100 -0
  55. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/sub_matchers/arguments/commands.rb +4 -0
  56. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/sub_matchers/arguments.rb +95 -0
  57. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/sub_matchers/base.rb +87 -0
  58. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/sub_matchers/return_its_value.rb +129 -0
  59. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/sub_matchers/with_any_arguments.rb +37 -0
  60. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/sub_matchers/with_concrete_arguments.rb +37 -0
  61. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/sub_matchers/without_arguments.rb +37 -0
  62. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/values/base.rb +41 -0
  63. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/values/with_calling_original.rb +30 -0
  64. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/values/without_calling_original.rb +30 -0
  65. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings.rb +12 -0
  66. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings_collection/errors.rb +57 -0
  67. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings_collection.rb +171 -0
  68. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/delegation.rb +79 -0
  69. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities.rb +5 -0
  70. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher.rb +276 -0
  71. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities.rb +3 -0
  72. data/lib/convenient_service/rspec/matchers/custom/delegate_to.rb +58 -234
  73. data/lib/convenient_service/rspec/matchers/custom/singleton_prepend_module.rb +79 -0
  74. data/lib/convenient_service/rspec/matchers/custom.rb +1 -0
  75. data/lib/convenient_service/rspec/matchers/singleton_prepend_module.rb +13 -0
  76. data/lib/convenient_service/rspec/matchers.rb +2 -0
  77. data/lib/convenient_service/service/plugins/has_result_steps/concern.rb +25 -0
  78. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/commands/cast_method.rb +3 -0
  79. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/commands/cast_method_caller.rb +8 -1
  80. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/commands/cast_method_direction.rb +3 -0
  81. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/commands/cast_method_key.rb +8 -1
  82. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/commands/cast_method_name.rb +9 -5
  83. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/concern/instance_methods.rb +4 -0
  84. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/entities/callers/base.rb +4 -0
  85. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/entities/callers/reassignment/commands/define_method_in_container.rb +74 -0
  86. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/entities/callers/reassignment/commands.rb +3 -0
  87. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/entities/callers/reassignment.rb +50 -0
  88. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/entities/callers.rb +1 -0
  89. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/entities/values/reassignment.rb +43 -0
  90. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/entities/values.rb +3 -0
  91. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/entities.rb +1 -0
  92. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/errors.rb +22 -0
  93. data/lib/convenient_service/service/plugins/has_result_steps/entities/step/concern/instance_methods.rb +13 -2
  94. data/lib/convenient_service/support/arguments/null_arguments.rb +28 -0
  95. data/lib/convenient_service/support/arguments.rb +87 -0
  96. data/lib/convenient_service/support/dependency_container/commands/assert_valid_scope.rb +32 -0
  97. data/lib/convenient_service/support/dependency_container/commands/create_methods_module.rb +27 -0
  98. data/lib/convenient_service/support/dependency_container/commands/import_method.rb +101 -0
  99. data/lib/convenient_service/support/dependency_container/commands.rb +5 -0
  100. data/lib/convenient_service/support/dependency_container/constants.rb +17 -0
  101. data/lib/convenient_service/support/dependency_container/entities/method.rb +114 -0
  102. data/lib/convenient_service/support/dependency_container/entities/method_collection.rb +98 -0
  103. data/lib/convenient_service/support/dependency_container/entities/namespace.rb +69 -0
  104. data/lib/convenient_service/support/dependency_container/entities/namespace_collection.rb +94 -0
  105. data/lib/convenient_service/support/dependency_container/entities.rb +6 -0
  106. data/lib/convenient_service/support/dependency_container/errors.rb +74 -0
  107. data/lib/convenient_service/support/dependency_container/export.rb +38 -0
  108. data/lib/convenient_service/support/dependency_container/import.rb +32 -0
  109. data/lib/convenient_service/support/dependency_container.rb +9 -0
  110. data/lib/convenient_service/support/not_passed.rb +7 -0
  111. data/lib/convenient_service/support/version/null_version.rb +7 -0
  112. data/lib/convenient_service/support/version.rb +11 -1
  113. data/lib/convenient_service/support.rb +3 -0
  114. data/lib/convenient_service/utils/module/fetch_own_const.rb +88 -0
  115. data/lib/convenient_service/utils/module/include_module.rb +38 -0
  116. data/lib/convenient_service/utils/module.rb +10 -0
  117. data/lib/convenient_service/utils/object/instance_variable_delete.rb +41 -0
  118. data/lib/convenient_service/utils/object/instance_variable_fetch.rb +4 -0
  119. data/lib/convenient_service/utils/object.rb +9 -0
  120. data/lib/convenient_service/utils/proc/conjunct.rb +1 -1
  121. data/lib/convenient_service/utils/proc/display.rb +43 -0
  122. data/lib/convenient_service/utils/proc.rb +5 -0
  123. data/lib/convenient_service/utils/string/split.rb +41 -0
  124. data/lib/convenient_service/utils/string.rb +5 -0
  125. data/lib/convenient_service/version.rb +1 -1
  126. data/lib/convenient_service.rb +6 -0
  127. metadata +123 -8
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConvenientService
4
+ module Support
5
+ module DependencyContainer
6
+ module Entities
7
+ class MethodCollection
8
+ ##
9
+ # @param methods [Array<ConvenientService::Support::DependencyContainer::Entities::Method>]
10
+ # @return [void]
11
+ #
12
+ def initialize(methods: [])
13
+ @methods = methods
14
+ end
15
+
16
+ ##
17
+ # @param name [String, Symbol]
18
+ # @param full_name [String, Symbol]
19
+ # @param scope [:instance, :class]
20
+ # @return [ConvenientService::Support::DependencyContainer::Entities::Method, nil]
21
+ #
22
+ def find_by(name: Support::NOT_PASSED, full_name: Support::NOT_PASSED, scope: Support::NOT_PASSED)
23
+ rules = []
24
+
25
+ rules << ->(method) { method.name.to_s == name.to_s } if name != Support::NOT_PASSED
26
+ rules << ->(method) { method.full_name.to_s == full_name.to_s } if full_name != Support::NOT_PASSED
27
+ rules << ->(method) { method.scope == scope } if scope != Support::NOT_PASSED
28
+
29
+ condition = Utils::Proc.conjunct(rules)
30
+
31
+ methods.find(&condition)
32
+ end
33
+
34
+ ##
35
+ # @param method [ConvenientService::Support::DependencyContainer::Entities::Method]
36
+ # @return [ConvenientService::Support::DependencyContainer::Entities::MethodCollection]
37
+ #
38
+ def <<(method)
39
+ methods << method
40
+
41
+ self
42
+ end
43
+
44
+ ##
45
+ # @return [Boolean]
46
+ #
47
+ def empty?
48
+ methods.empty?
49
+ end
50
+
51
+ ##
52
+ # @param method [ConvenientService::Support::DependencyContainer::Entities::Method]
53
+ # @return [Boolean]
54
+ #
55
+ def include?(method)
56
+ methods.include?(method)
57
+ end
58
+
59
+ ##
60
+ # @return [ConvenientService::Support::DependencyContainer::Entities::MethodCollection]
61
+ #
62
+ def clear
63
+ methods.clear
64
+
65
+ self
66
+ end
67
+
68
+ ##
69
+ # @return [Array]
70
+ #
71
+ def to_a
72
+ methods.to_a
73
+ end
74
+
75
+ ##
76
+ # @param other [Object] Can be any type.
77
+ # @return [Boolean]
78
+ #
79
+ def ==(other)
80
+ return unless other.instance_of?(self.class)
81
+
82
+ return false if methods != other.methods
83
+
84
+ true
85
+ end
86
+
87
+ protected
88
+
89
+ ##
90
+ # @!attribute [r] methods
91
+ # @return [Array<ConvenientService::Support::DependencyContainer::Entities::Method>]
92
+ #
93
+ attr_reader :methods
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConvenientService
4
+ module Support
5
+ module DependencyContainer
6
+ module Entities
7
+ class Namespace
8
+ ##
9
+ # @!attribute [r] name
10
+ # @return [String, Symbol]
11
+ #
12
+ attr_reader :name
13
+
14
+ ##
15
+ # @param name [String, Symbol]
16
+ # @return [void]
17
+ #
18
+ def initialize(name:)
19
+ @name = name
20
+ end
21
+
22
+ ##
23
+ # @return [Proc]
24
+ #
25
+ def body
26
+ @body ||= -> { namespace }
27
+ end
28
+
29
+ ##
30
+ # @return [ConvenientService::Support::DependencyContainer::Entities::NamespaceCollection]
31
+ #
32
+ def namespaces
33
+ @namespaces ||= Entities::NamespaceCollection.new
34
+ end
35
+
36
+ ##
37
+ # @param name [String, Symbol]
38
+ # @param body [Proc]
39
+ # @return [Symbol]
40
+ #
41
+ def define_method(name, &body)
42
+ define_singleton_method(name, &body)
43
+ end
44
+
45
+ ##
46
+ # @param other [Object] Can be any type.
47
+ # @return [Boolean]
48
+ #
49
+ def ==(other)
50
+ return unless other.instance_of?(self.class)
51
+
52
+ return false if name != other.name
53
+
54
+ true
55
+ end
56
+
57
+ private
58
+
59
+ ##
60
+ # @return [ConvenientService::Support::DependencyContainer::Entities::Namespace]
61
+ #
62
+ def namespace
63
+ self
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,94 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConvenientService
4
+ module Support
5
+ module DependencyContainer
6
+ module Entities
7
+ class NamespaceCollection
8
+ ##
9
+ # @param namespaces [Array<ConvenientService::Support::DependencyContainer::Entities::Namespace>]
10
+ # @return [void]
11
+ #
12
+ def initialize(namespaces: [])
13
+ @namespaces = namespaces
14
+ end
15
+
16
+ ##
17
+ # @param name [String, Symbol]
18
+ # @return [ConvenientService::Support::DependencyContainer::Entities::Namespace, nil]
19
+ #
20
+ def find_by(name: Support::NOT_PASSED)
21
+ rules = []
22
+
23
+ rules << ->(namespace) { namespace.name.to_s == name.to_s } if name != Support::NOT_PASSED
24
+
25
+ condition = Utils::Proc.conjunct(rules)
26
+
27
+ namespaces.find(&condition)
28
+ end
29
+
30
+ ##
31
+ # @param namespace [ConvenientService::Support::DependencyContainer::Entities::Namespace]
32
+ # @return [ConvenientService::Support::DependencyContainer::Entities::NamespaceCollection]
33
+ #
34
+ def <<(namespace)
35
+ namespaces << namespace
36
+
37
+ self
38
+ end
39
+
40
+ ##
41
+ # @return [Boolean]
42
+ #
43
+ def empty?
44
+ namespaces.empty?
45
+ end
46
+
47
+ ##
48
+ # @param namespace [ConvenientService::Support::DependencyContainer::Entities::Namespace]
49
+ # @return [Boolean]
50
+ #
51
+ def include?(namespace)
52
+ namespaces.include?(namespace)
53
+ end
54
+
55
+ ##
56
+ # @return [ConvenientService::Support::DependencyContainer::Entities::NamespaceCollection]
57
+ #
58
+ def clear
59
+ namespaces.clear
60
+
61
+ self
62
+ end
63
+
64
+ ##
65
+ # @return [Array]
66
+ #
67
+ def to_a
68
+ namespaces.to_a
69
+ end
70
+
71
+ ##
72
+ # @param other [Object] Can be any type.
73
+ # @return [Boolean]
74
+ #
75
+ def ==(other)
76
+ return unless other.instance_of?(self.class)
77
+
78
+ return false if namespaces != other.namespaces
79
+
80
+ true
81
+ end
82
+
83
+ protected
84
+
85
+ ##
86
+ # @!attribute [r] namespaces
87
+ # @return [Array<ConvenientService::Support::DependencyContainer::Entities::Namespace>]
88
+ #
89
+ attr_reader :namespaces
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "entities/method_collection"
4
+ require_relative "entities/method"
5
+ require_relative "entities/namespace_collection"
6
+ require_relative "entities/namespace"
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConvenientService
4
+ module Support
5
+ module DependencyContainer
6
+ module Errors
7
+ class InvalidScope < ConvenientService::Error
8
+ ##
9
+ # @param scope [Object]
10
+ # @return [void]
11
+ #
12
+ def initialize(scope:)
13
+ message = <<~TEXT
14
+ Scope `#{scope.inspect}` is NOT valid.
15
+
16
+ Valid options are #{printable_valid_scopes}.
17
+ TEXT
18
+
19
+ super(message)
20
+ end
21
+
22
+ private
23
+
24
+ ##
25
+ # @return [String]
26
+ #
27
+ def printable_valid_scopes
28
+ Constants::SCOPES.map { |scope| "`:#{scope}`" }.join(", ")
29
+ end
30
+ end
31
+
32
+ class NotExportableModule < ConvenientService::Error
33
+ ##
34
+ # @param mod [Module]
35
+ # @return [void]
36
+ #
37
+ def initialize(mod:)
38
+ message = <<~TEXT
39
+ Module `#{mod}` can NOT export methods.
40
+
41
+ Did you forget to include `ConvenientService::Container.export` into it?
42
+ TEXT
43
+
44
+ super(message)
45
+ end
46
+ end
47
+
48
+ class NotExportedMethod < ConvenientService::Error
49
+ ##
50
+ # @param method_name [String]
51
+ # @param method_scope [Symbol]
52
+ # @param mod [Module]
53
+ # @return [void]
54
+ #
55
+ def initialize(method_name:, method_scope:, mod:)
56
+ message = <<~TEXT
57
+ Module `#{mod}` does NOT export method `#{method_name}` with `#{method_scope}` scope.
58
+
59
+ Did you forget to export if from `#{mod}`? For example:
60
+
61
+ module #{mod}
62
+ export #{method_name}, scope: :#{method_scope} do |*args, **kwargs, &block|
63
+ # ...
64
+ end
65
+ end
66
+ TEXT
67
+
68
+ super(message)
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConvenientService
4
+ module Support
5
+ module DependencyContainer
6
+ module Export
7
+ include Support::Concern
8
+
9
+ class_methods do
10
+ ##
11
+ # @param full_name [String, Symbol]
12
+ # @param scope [:instance, :class]
13
+ # @param body [Proc]
14
+ # @return [ConvenientService::Support::DependencyContainer::Entities::Method]
15
+ #
16
+ # @internal
17
+ # NOTE: `export` does NOT accept `prepend` kwarg intentionally.
18
+ # It is done to follow "the Ruby way".
19
+ # You won't ever see a module in Ruby that contains methods for `include` and `prepend` at the same time.
20
+ # So why `export` should allow to do it?
21
+ #
22
+ def export(full_name, scope: Constants::DEFAULT_SCOPE, &body)
23
+ Commands::AssertValidScope.call(scope: scope)
24
+
25
+ Entities::Method.new(full_name: full_name, scope: scope, body: body).tap { |method| exported_methods << method }
26
+ end
27
+
28
+ ##
29
+ # @return [ConvenientService::Support::DependencyContainer::Entities::MethodCollection]
30
+ #
31
+ def exported_methods
32
+ @exported_methods ||= Entities::MethodCollection.new
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConvenientService
4
+ module Support
5
+ module DependencyContainer
6
+ module Import
7
+ include Support::Concern
8
+
9
+ class_methods do
10
+ ##
11
+ # @param full_name [String, Symbol]
12
+ # @param from [Module]
13
+ # @param scope [:instance, :class]
14
+ # @param prepend [Boolean]
15
+ # @return [ConvenientService::Support::DependencyContainer::Entities::Method]
16
+ #
17
+ def import(full_name, from:, scope: Constants::DEFAULT_SCOPE, prepend: Constants::DEFAULT_PREPEND)
18
+ Commands::AssertValidScope.call(scope: scope)
19
+
20
+ raise Errors::NotExportableModule.new(mod: from) unless Utils::Module.include_module?(from, DependencyContainer::Export)
21
+
22
+ method = from.exported_methods.find_by(full_name: full_name, scope: scope)
23
+
24
+ raise Errors::NotExportedMethod.new(method_name: full_name, method_scope: scope, mod: from) unless method
25
+
26
+ Commands::ImportMethod.call(importing_module: self, exported_method: method, prepend: prepend)
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "dependency_container/commands"
4
+ require_relative "dependency_container/constants"
5
+ require_relative "dependency_container/entities"
6
+ require_relative "dependency_container/errors"
7
+
8
+ require_relative "dependency_container/export"
9
+ require_relative "dependency_container/import"
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConvenientService
4
+ module Support
5
+ NOT_PASSED = ::Object.new
6
+ end
7
+ end
@@ -11,6 +11,13 @@ module ConvenientService
11
11
  # - https://avdi.codes/null-objects-and-falsiness/
12
12
  #
13
13
  class NullVersion
14
+ ##
15
+ # @return [Boolean]
16
+ #
17
+ def null_version?
18
+ true
19
+ end
20
+
14
21
  ##
15
22
  # @return [nil]
16
23
  #
@@ -24,10 +24,17 @@ module ConvenientService
24
24
  # @return [ConvenientService::Support::Version::NullVersion]
25
25
  #
26
26
  def null_version
27
- @null_version ||= NullVersion.new
27
+ @null_version ||= Support::Version::NullVersion.new
28
28
  end
29
29
  end
30
30
 
31
+ ##
32
+ # @return [Boolean]
33
+ #
34
+ def null_version?
35
+ false
36
+ end
37
+
31
38
  ##
32
39
  # @return [Gem::Version, nil]
33
40
  #
@@ -52,6 +59,9 @@ module ConvenientService
52
59
 
53
60
  private
54
61
 
62
+ ##
63
+ # @return [String]
64
+ #
55
65
  attr_reader :value
56
66
 
57
67
  ##
@@ -1,13 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "support/not_passed"
3
4
  require_relative "support/concern"
4
5
 
5
6
  require_relative "support/abstract_method"
7
+ require_relative "support/arguments"
6
8
  require_relative "support/cache"
7
9
  require_relative "support/castable"
8
10
  require_relative "support/command"
9
11
  require_relative "support/copyable"
10
12
  require_relative "support/delegate"
13
+ require_relative "support/dependency_container"
11
14
  require_relative "support/finite_loop"
12
15
  require_relative "support/gems"
13
16
  require_relative "support/middleware"
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ ##
4
+ # @example Two args form (works as GetOwnConst).
5
+ # module Test
6
+ # end
7
+ #
8
+ # ConvenientService::Utils::Module::FetchOwnConst.call(Test, :File)
9
+ # # => nil, not File from Ruby Core.
10
+ #
11
+ # module Test
12
+ # class File
13
+ # end
14
+ # end
15
+ #
16
+ # ConvenientService::Utils::Module::FetchOwnConst.call(Test, :File)
17
+ # # => Test::File
18
+ #
19
+ # @example Two args + block form.
20
+ # module Test
21
+ # end
22
+ #
23
+ # ConvenientService::Utils::Module::FetchOwnConst.call(Test, :File) { Class.new }
24
+ # # => Test::File, just created.
25
+ #
26
+ # module Test
27
+ # class File
28
+ # end
29
+ # end
30
+ #
31
+ # ConvenientService::Utils::Module::FetchOwnConst.call(Test, :File)
32
+ # # => Test::File, already existing.
33
+ #
34
+ module ConvenientService
35
+ module Utils
36
+ module Module
37
+ class FetchOwnConst < Support::Command
38
+ ##
39
+ # @!attribute [r] mod
40
+ # @return [Class, Module]
41
+ #
42
+ attr_reader :mod
43
+
44
+ ##
45
+ # @!attribute [r] const_name
46
+ # @return [Symbol]
47
+ #
48
+ attr_reader :const_name
49
+
50
+ ##
51
+ # @!attribute [r] fallback_block
52
+ # @return [Proc, nil]
53
+ #
54
+ attr_reader :fallback_block
55
+
56
+ ##
57
+ # @param mod [Class, Module]
58
+ # @param const_name [Symbol]
59
+ # @param fallback_block [Proc]
60
+ # @return [void]
61
+ #
62
+ def initialize(mod, const_name, &fallback_block)
63
+ @mod = mod
64
+ @const_name = const_name
65
+ @fallback_block = fallback_block
66
+ end
67
+
68
+ ##
69
+ # @return [Object] Value of own const. Can be any type.
70
+ #
71
+ # @internal
72
+ # TODO: Wrap by mutex?
73
+ #
74
+ def call
75
+ ##
76
+ # NOTE: > If `inherit` is `false`, the lookup only checks the constants in the receiver:
77
+ # https://ruby-doc.org/core-3.0.0/Module.html#method-i-const_defined-3F
78
+ #
79
+ return mod.const_get(const_name, false) if mod.const_defined?(const_name, false)
80
+
81
+ return mod.const_set(const_name, fallback_block.call) if fallback_block
82
+
83
+ nil
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConvenientService
4
+ module Utils
5
+ module Module
6
+ class IncludeModule < Support::Command
7
+ ##
8
+ # @!attribute [r] mod
9
+ # @return [Module]
10
+ #
11
+ attr_reader :mod
12
+
13
+ ##
14
+ # @!attribute [r] other_mod
15
+ # @return [Module]
16
+ #
17
+ attr_reader :other_mod
18
+
19
+ ##
20
+ # @param mod [Module]
21
+ # @param other_mod [Module]
22
+ # @return [void]
23
+ #
24
+ def initialize(mod, other_mod)
25
+ @mod = mod
26
+ @other_mod = other_mod
27
+ end
28
+
29
+ ##
30
+ # @return [Boolean]
31
+ #
32
+ def call
33
+ mod.included_modules.include?(other_mod)
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -1,9 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "module/class_method_defined"
4
+ require_relative "module/fetch_own_const"
4
5
  require_relative "module/get_own_instance_method"
5
6
  require_relative "module/get_own_const"
6
7
  require_relative "module/has_own_instance_method"
8
+ require_relative "module/include_module"
7
9
  require_relative "module/instance_method_defined"
8
10
 
9
11
  module ConvenientService
@@ -19,6 +21,10 @@ module ConvenientService
19
21
  ClassMethodDefined.call(...)
20
22
  end
21
23
 
24
+ def fetch_own_const(...)
25
+ FetchOwnConst.call(...)
26
+ end
27
+
22
28
  def get_own_instance_method(...)
23
29
  GetOwnInstanceMethod.call(...)
24
30
  end
@@ -31,6 +37,10 @@ module ConvenientService
31
37
  HasOwnInstanceMethod.call(...)
32
38
  end
33
39
 
40
+ def include_module?(...)
41
+ IncludeModule.call(...)
42
+ end
43
+
34
44
  def instance_method_defined?(...)
35
45
  InstanceMethodDefined.call(...)
36
46
  end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ ##
4
+ #
5
+ #
6
+ module ConvenientService
7
+ module Utils
8
+ module Object
9
+ class InstanceVariableDelete < Support::Command
10
+ ##
11
+ # @!attribute [r] object
12
+ # @return [Object]
13
+ #
14
+ attr_reader :object
15
+
16
+ ##
17
+ # @!attribute [r] ivar_name
18
+ # @return [Symbol, String]
19
+ #
20
+ attr_reader :ivar_name
21
+
22
+ ##
23
+ # @param object [Object]
24
+ # @param ivar_name [Symbol]
25
+ # @return [void]
26
+ #
27
+ def initialize(object, ivar_name)
28
+ @object = object
29
+ @ivar_name = ivar_name
30
+ end
31
+
32
+ ##
33
+ # @return [Object] Value of ivar. Can be any type.
34
+ #
35
+ def call
36
+ return object.remove_instance_variable(ivar_name) if object.instance_variable_defined?(ivar_name)
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end