convenient_service 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (99) hide show
  1. checksums.yaml +4 -4
  2. data/.gem_release.yml +0 -12
  3. data/.github/issue_template.md +35 -0
  4. data/.github/pull_request_template.md +31 -0
  5. data/.github/workflows/cd.yml +15 -2
  6. data/CHANGELOG.md +18 -0
  7. data/README.md +12 -3
  8. data/ROADMAP.md +19 -8
  9. data/Taskfile.yml +2 -10
  10. data/lib/convenient_service/common/plugins/caches_return_value/middleware.rb +17 -2
  11. data/lib/convenient_service/common/plugins/caches_return_value.rb +0 -1
  12. data/lib/convenient_service/common/plugins/has_constructor/concern.rb +13 -0
  13. data/lib/convenient_service/common/plugins/has_constructor_without_initialize/concern.rb +27 -0
  14. data/lib/convenient_service/common/plugins/has_constructor_without_initialize.rb +3 -0
  15. data/lib/convenient_service/common/plugins/has_internals/entities/internals/plugins/has_cache/concern.rb +8 -4
  16. data/lib/convenient_service/common/plugins/has_internals/entities/internals/plugins/has_cache.rb +0 -1
  17. data/lib/convenient_service/common/plugins.rb +1 -0
  18. data/lib/convenient_service/configs/standard.rb +16 -0
  19. data/lib/convenient_service/core/entities/config/entities/method_middlewares/entities/container/commands/cast_container.rb +0 -2
  20. data/lib/convenient_service/dependencies.rb +1 -0
  21. data/lib/convenient_service/examples/dry/gemfile.rb +5 -4
  22. data/lib/convenient_service/examples/rails/gemfile.rb +5 -4
  23. data/lib/convenient_service/examples/standard/cowsay/services/build_cloud.rb +42 -0
  24. data/lib/convenient_service/examples/standard/cowsay/services/build_cow.rb +38 -0
  25. data/lib/convenient_service/examples/standard/cowsay/services/print.rb +32 -0
  26. data/lib/convenient_service/examples/standard/cowsay/services.rb +5 -0
  27. data/lib/convenient_service/examples/standard/cowsay.rb +24 -0
  28. data/lib/convenient_service/examples/standard/gemfile/services/format.rb +2 -0
  29. data/lib/convenient_service/examples/standard/gemfile.rb +5 -4
  30. data/lib/convenient_service/rspec/helpers/custom/stub_service/entities/result_spec.rb +96 -7
  31. data/lib/convenient_service/rspec/helpers/custom/wrap_method/entities/wrapped_method.rb +44 -8
  32. data/lib/convenient_service/rspec/helpers/custom/wrap_method/errors.rb +11 -9
  33. data/lib/convenient_service/rspec/matchers/custom/call_chain_next.rb +2 -0
  34. data/lib/convenient_service/rspec/matchers/custom/results/be_success.rb +6 -0
  35. data/lib/convenient_service/service/plugins/aliases.rb +1 -0
  36. data/lib/convenient_service/service/plugins/has_inspect/concern.rb +23 -0
  37. data/lib/convenient_service/service/plugins/has_inspect.rb +3 -0
  38. data/lib/convenient_service/service/plugins/has_result/commands/create_result_class.rb +10 -0
  39. data/lib/convenient_service/service/plugins/has_result/concern/class_methods.rb +23 -14
  40. data/lib/convenient_service/service/plugins/has_result/concern/instance_methods.rb +17 -1
  41. data/lib/convenient_service/service/plugins/has_result/entities/result/concern.rb +4 -3
  42. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/has_inspect/concern.rb +31 -0
  43. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/has_inspect.rb +3 -0
  44. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/has_jsend_status_and_attributes/commands/cast_jsend_attributes.rb +37 -0
  45. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/has_jsend_status_and_attributes/commands.rb +3 -0
  46. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/has_jsend_status_and_attributes/concern/instance_methods.rb +115 -0
  47. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/has_jsend_status_and_attributes/concern.rb +27 -0
  48. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/has_jsend_status_and_attributes/entities/code/class_methods.rb +34 -0
  49. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/has_jsend_status_and_attributes/entities/code.rb +49 -0
  50. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/has_jsend_status_and_attributes/entities/data/class_methods.rb +32 -0
  51. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/has_jsend_status_and_attributes/entities/data.rb +49 -0
  52. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/has_jsend_status_and_attributes/entities/message/class_methods.rb +34 -0
  53. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/has_jsend_status_and_attributes/entities/message.rb +45 -0
  54. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/has_jsend_status_and_attributes/entities/status/class_methods.rb +34 -0
  55. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/has_jsend_status_and_attributes/entities/status.rb +73 -0
  56. data/lib/convenient_service/service/plugins/has_result/entities/result/{entities.rb → plugins/has_jsend_status_and_attributes/entities.rb} +0 -0
  57. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/has_jsend_status_and_attributes/errors.rb +29 -0
  58. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/has_jsend_status_and_attributes/middleware.rb +31 -0
  59. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/has_jsend_status_and_attributes/structs/jsend_attributes.rb +21 -0
  60. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/has_jsend_status_and_attributes/structs.rb +3 -0
  61. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/has_jsend_status_and_attributes.rb +8 -0
  62. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins.rb +2 -0
  63. data/lib/convenient_service/service/plugins/has_result/entities/result.rb +0 -4
  64. data/lib/convenient_service/service/plugins/has_result_method_steps/middleware.rb +1 -1
  65. data/lib/convenient_service/service/plugins/has_result_method_steps/services/method_step_config.rb +30 -4
  66. data/lib/convenient_service/service/plugins/has_result_status_check_short_syntax/concern.rb +63 -0
  67. data/lib/convenient_service/service/plugins/has_result_status_check_short_syntax.rb +3 -0
  68. data/lib/convenient_service/service/plugins/has_result_steps/concern.rb +2 -1
  69. data/lib/convenient_service/service/plugins/has_result_steps/entities/step/plugins/has_inspect/concern.rb +31 -0
  70. data/lib/convenient_service/service/plugins/has_result_steps/entities/step/plugins/has_inspect.rb +3 -0
  71. data/lib/convenient_service/service/plugins/has_result_steps/entities/step/plugins.rb +3 -0
  72. data/lib/convenient_service/service/plugins/has_result_steps/entities/step.rb +1 -0
  73. data/lib/convenient_service/service/plugins.rb +2 -0
  74. data/lib/convenient_service/support/cache/key.rb +100 -0
  75. data/lib/convenient_service/support/cache.rb +131 -0
  76. data/lib/convenient_service/support/copyable.rb +3 -2
  77. data/lib/convenient_service/support/delegate.rb +3 -2
  78. data/lib/convenient_service/support.rb +1 -0
  79. data/lib/convenient_service/version.rb +1 -1
  80. data/logo.png +0 -0
  81. metadata +40 -21
  82. data/lib/convenient_service/common/plugins/caches_return_value/entities/key.rb +0 -79
  83. data/lib/convenient_service/common/plugins/caches_return_value/entities.rb +0 -3
  84. data/lib/convenient_service/common/plugins/has_internals/entities/internals/plugins/has_cache/entities/cache.rb +0 -81
  85. data/lib/convenient_service/common/plugins/has_internals/entities/internals/plugins/has_cache/entities.rb +0 -3
  86. data/lib/convenient_service/service/plugins/has_result/entities/result/commands/cast_result_params.rb +0 -33
  87. data/lib/convenient_service/service/plugins/has_result/entities/result/commands.rb +0 -3
  88. data/lib/convenient_service/service/plugins/has_result/entities/result/concern/instance_methods.rb +0 -64
  89. data/lib/convenient_service/service/plugins/has_result/entities/result/entities/code/class_methods.rb +0 -30
  90. data/lib/convenient_service/service/plugins/has_result/entities/result/entities/code.rb +0 -45
  91. data/lib/convenient_service/service/plugins/has_result/entities/result/entities/data/class_methods.rb +0 -28
  92. data/lib/convenient_service/service/plugins/has_result/entities/result/entities/data.rb +0 -45
  93. data/lib/convenient_service/service/plugins/has_result/entities/result/entities/message/class_methods.rb +0 -30
  94. data/lib/convenient_service/service/plugins/has_result/entities/result/entities/message.rb +0 -41
  95. data/lib/convenient_service/service/plugins/has_result/entities/result/entities/status/class_methods.rb +0 -30
  96. data/lib/convenient_service/service/plugins/has_result/entities/result/entities/status.rb +0 -69
  97. data/lib/convenient_service/service/plugins/has_result/entities/result/errors.rb +0 -25
  98. data/lib/convenient_service/service/plugins/has_result/entities/result/structs/result_params.rb +0 -17
  99. data/lib/convenient_service/service/plugins/has_result/entities/result/structs.rb +0 -3
@@ -14,6 +14,8 @@ module ConvenientService
14
14
  class Format
15
15
  include ConvenientService::Standard::Config
16
16
 
17
+ attr_reader :path
18
+
17
19
  step Services::ReadFileContent, in: :path, out: :content
18
20
  step Services::StripComments, in: :content, out: :content_without_comments
19
21
  step Services::ParseContent, in: {content: :content_without_comments}, out: :parsed_content
@@ -3,17 +3,18 @@
3
3
  require_relative "gemfile/services"
4
4
 
5
5
  ##
6
- # Usage example:
6
+ # @internal
7
+ # Usage example:
7
8
  #
8
- # result = ConvenientService::Examples::Standard::Gemfile.format(path: "Gemfile")
9
- # result = ConvenientService::Examples::Standard::Gemfile.format(path: "spec/cli/gemfile/format/fixtures/Gemfile")
9
+ # result = ConvenientService::Examples::Standard::Gemfile.format("Gemfile")
10
+ # result = ConvenientService::Examples::Standard::Gemfile.format("spec/cli/gemfile/format/fixtures/Gemfile")
10
11
  #
11
12
  module ConvenientService
12
13
  module Examples
13
14
  module Standard
14
15
  module Gemfile
15
16
  class << self
16
- def format
17
+ def format(path)
17
18
  Services::Format[path: path]
18
19
  end
19
20
  end
@@ -5,72 +5,140 @@ module ConvenientService
5
5
  module Helpers
6
6
  module Custom
7
7
  ##
8
- # TODO: Specs.
8
+ # @internal
9
+ # TODO: Specs.
9
10
  #
10
11
  class StubService < Support::Command
11
12
  module Entities
12
13
  class ResultSpec
14
+ ##
15
+ # @param status [Symbol]
16
+ # @param service_class [Class]
17
+ # @param chain [Hash]
18
+ # @return [void]
19
+ # @since 0.1.0
20
+ #
13
21
  def initialize(status:, service_class: nil, chain: {})
14
22
  @status = status
15
23
  @service_class = service_class
16
24
  @chain = chain
17
25
  end
18
26
 
27
+ ##
28
+ # @param service_class [Class]
29
+ # @return [ConvenientService::RSpec::Helpers::Custom::StubService::Entities::ResultSpec]
30
+ # @since 0.1.0
31
+ #
19
32
  def for(service_class)
20
33
  self.class.new(status: status, service_class: service_class, chain: chain)
21
34
  end
22
35
 
36
+ ##
37
+ # @param data [Hash]
38
+ # @return [ConvenientService::RSpec::Helpers::Custom::StubService::Entities::ResultSpec]
39
+ # @since 0.1.0
40
+ #
23
41
  def with_data(data)
24
42
  chain[:data] = data
25
43
 
26
44
  self
27
45
  end
28
46
 
47
+ ##
48
+ # @param message [String]
49
+ # @return [ConvenientService::RSpec::Helpers::Custom::StubService::Entities::ResultSpec]
50
+ # @since 0.1.0
51
+ #
29
52
  def with_message(message)
30
53
  chain[:message] = message
31
54
 
32
55
  self
33
56
  end
34
57
 
58
+ ##
59
+ # @param code [String]
60
+ # @return [ConvenientService::RSpec::Helpers::Custom::StubService::Entities::ResultSpec]
61
+ # @since 0.1.0
62
+ #
35
63
  def with_code(code)
36
64
  chain[:code] = code
37
65
 
38
66
  self
39
67
  end
40
68
 
69
+ ##
70
+ # @param data [Hash]
71
+ # @return [ConvenientService::RSpec::Helpers::Custom::StubService::Entities::ResultSpec]
72
+ # @since 0.1.0
73
+ #
41
74
  def and_data(data)
42
75
  chain[:data] = data
43
76
 
44
77
  self
45
78
  end
46
79
 
80
+ ##
81
+ # @param message [String]
82
+ # @return [ConvenientService::RSpec::Helpers::Custom::StubService::Entities::ResultSpec]
83
+ # @since 0.1.0
84
+ #
47
85
  def and_message(message)
48
86
  chain[:message] = message
49
87
 
50
88
  self
51
89
  end
52
90
 
91
+ ##
92
+ # @param code [String]
93
+ # @return [ConvenientService::RSpec::Helpers::Custom::StubService::Entities::ResultSpec]
94
+ # @since 0.1.0
95
+ #
53
96
  def and_code(code)
54
97
  chain[:code] = code
55
98
 
56
99
  self
57
100
  end
58
101
 
102
+ ##
103
+ # @return [Object]
104
+ #
105
+ # @internal
106
+ # TODO: Assert.
107
+ #
59
108
  def calculate_value
60
- ##
61
- # TODO: Assert.
62
- #
63
109
  service_class.__send__(status, **kwargs)
64
110
  end
65
111
 
66
112
  private
67
113
 
68
- attr_reader :status, :service_class, :chain
69
-
114
+ ##
115
+ # @!attribute [r] status
116
+ # @return [Symbol]
117
+ #
118
+ attr_reader :status
119
+
120
+ ##
121
+ # @!attribute [r] service_class
122
+ # @return [Class]
123
+ #
124
+ attr_reader :service_class
125
+
126
+ ##
127
+ # @!attribute [r] chain
128
+ # @return [Hash]
129
+ #
130
+ attr_reader :chain
131
+
132
+ ##
133
+ # @return [Hash]
134
+ #
70
135
  def kwargs
71
136
  @kwargs ||= calculate_kwargs
72
137
  end
73
138
 
139
+ ##
140
+ # @return [Hash]
141
+ #
74
142
  def calculate_kwargs
75
143
  kwargs = {}
76
144
 
@@ -83,32 +151,53 @@ module ConvenientService
83
151
  kwargs
84
152
  end
85
153
 
154
+ ##
155
+ # @return [Boolean]
156
+ #
86
157
  def used_data?
87
158
  chain.key?(:data)
88
159
  end
89
160
 
161
+ ##
162
+ # @return [Boolean]
163
+ #
90
164
  def used_message?
91
165
  chain.key?(:message)
92
166
  end
93
167
 
168
+ ##
169
+ # @return [Boolean]
170
+ #
94
171
  def used_code?
95
172
  chain.key?(:code)
96
173
  end
97
174
 
175
+ ##
176
+ # @return [Hash]
177
+ #
98
178
  def data
99
179
  @data ||= chain[:data] || {}
100
180
  end
101
181
 
182
+ ##
183
+ # @return [String]
184
+ #
102
185
  def message
103
186
  @message ||= chain[:message] || ""
104
187
  end
105
188
 
189
+ ##
190
+ # @return [String]
191
+ #
106
192
  def code
107
193
  @code ||= chain[:code] || ""
108
194
  end
109
195
 
196
+ ##
197
+ # @return [Object]
198
+ #
110
199
  def service_instance
111
- @service_instance ||= service_class.new_without_initialize
200
+ @service_instance ||= service_class.create_without_initialize
112
201
  end
113
202
  end
114
203
  end
@@ -4,17 +4,23 @@ module ConvenientService
4
4
  module RSpec
5
5
  module Helpers
6
6
  module Custom
7
- ##
8
- # TODO: Specs.
9
- #
10
7
  class WrapMethod < Support::Command
11
8
  module Entities
12
9
  ##
13
- # NOTE: Do NOT pollute the interface of this class until really needed. Avoid even pollution of private methods.
10
+ # @internal
11
+ # TODO: A better name.
12
+ #
13
+ # NOTE: Do NOT pollute the interface of this class until really needed. Avoid even pollution of private methods.
14
14
  #
15
15
  class WrappedMethod
16
16
  ##
17
- # NOTE: `middlewares` are `MethodChainMiddleware` classes.
17
+ # @param entity [Object, Class]
18
+ # @param method [String]
19
+ # @param middlewares [Array<ConvenientService::Core::MethodChainMiddleware>]
20
+ # @return [void]
21
+ #
22
+ # @internal
23
+ # NOTE: `middlewares` are `MethodChainMiddleware` classes.
18
24
  #
19
25
  def initialize(entity:, method:, middlewares:)
20
26
  @entity = entity
@@ -29,9 +35,6 @@ module ConvenientService
29
35
  #
30
36
  stack.use(
31
37
  proc do |env|
32
- ##
33
- # TODO: Enforce to always pass args, kwargs, block.
34
- #
35
38
  entity.__send__(method, *env[:args], **env[:kwargs], &env[:block])
36
39
  .tap { |value| @chain_value = value }
37
40
  .tap { @chain_arguments = {args: env[:args], kwargs: env[:kwargs], block: env[:block]} }
@@ -40,32 +43,65 @@ module ConvenientService
40
43
  end
41
44
  end
42
45
 
46
+ ##
47
+ # @param args [Array]
48
+ # @param kwargs [Hash]
49
+ # @param block [Proc]
50
+ # @return [Object] Can be any type.
51
+ #
43
52
  def call(*args, **kwargs, &block)
44
53
  @stack.call(entity: @entity, method: @method, args: args, kwargs: kwargs, block: block)
45
54
  end
46
55
 
56
+ ##
57
+ # @return [void]
58
+ #
59
+ def reset!
60
+ remove_instance_variable(:@chain_value) if defined? @chain_value
61
+ remove_instance_variable(:@chain_arguments) if defined? @chain_arguments
62
+ end
63
+
64
+ ##
65
+ # @return [Boolean]
66
+ #
47
67
  def chain_called?
48
68
  Utils::Bool.to_bool(defined? @chain_value)
49
69
  end
50
70
 
71
+ ##
72
+ # @return [Object] Can be any type.
73
+ # @raise [ConvenientService::RSpec::Helpers::Custom::WrapMethod::Errors::ChainAttributePreliminaryAccess]
74
+ #
51
75
  def chain_value
52
76
  raise Errors::ChainAttributePreliminaryAccess.new(attribute: :value) unless chain_called?
53
77
 
54
78
  @chain_value
55
79
  end
56
80
 
81
+ ##
82
+ # @return [Array]
83
+ # @raise [ConvenientService::RSpec::Helpers::Custom::WrapMethod::Errors::ChainAttributePreliminaryAccess]
84
+ #
57
85
  def chain_args
58
86
  raise Errors::ChainAttributePreliminaryAccess.new(attribute: :args) unless chain_called?
59
87
 
60
88
  @chain_arguments[:args]
61
89
  end
62
90
 
91
+ ##
92
+ # @return [Hash]
93
+ # @raise [ConvenientService::RSpec::Helpers::Custom::WrapMethod::Errors::ChainAttributePreliminaryAccess]
94
+ #
63
95
  def chain_kwargs
64
96
  raise Errors::ChainAttributePreliminaryAccess.new(attribute: :kwargs) unless chain_called?
65
97
 
66
98
  @chain_arguments[:kwargs]
67
99
  end
68
100
 
101
+ ##
102
+ # @return [Proc, nil]
103
+ # @raise [ConvenientService::RSpec::Helpers::Custom::WrapMethod::Errors::ChainAttributePreliminaryAccess]
104
+ #
69
105
  def chain_block
70
106
  raise Errors::ChainAttributePreliminaryAccess.new(attribute: :block) unless chain_called?
71
107
 
@@ -5,16 +5,18 @@ module ConvenientService
5
5
  module Helpers
6
6
  module Custom
7
7
  class WrapMethod < Support::Command
8
- class WrappedMethod
9
- module Errors
10
- class ChainAttributePreliminaryAccess < ::StandardError
11
- def initialize(attribute:)
12
- message <<~TEXT
13
- Chain attribute `#{attribute}` is accessed before the chain is called.
14
- TEXT
8
+ module Errors
9
+ class ChainAttributePreliminaryAccess < ConvenientService::Error
10
+ ##
11
+ # @param attribute [Symbol]
12
+ # @return [void]
13
+ #
14
+ def initialize(attribute:)
15
+ message = <<~TEXT
16
+ Chain attribute `#{attribute}` is accessed before the chain is called.
17
+ TEXT
15
18
 
16
- super(message)
17
- end
19
+ super(message)
18
20
  end
19
21
  end
20
22
  end
@@ -84,6 +84,8 @@ module ConvenientService
84
84
  end
85
85
 
86
86
  def on(method)
87
+ method.reset!
88
+
87
89
  chain[:method] = method
88
90
 
89
91
  self
@@ -27,6 +27,12 @@ module ConvenientService
27
27
  self
28
28
  end
29
29
 
30
+ def without_data
31
+ chain[:data] = {}
32
+
33
+ self
34
+ end
35
+
30
36
  def of(service_class)
31
37
  chain[:service_class] = service_class
32
38
 
@@ -4,5 +4,6 @@ module ConvenientService
4
4
  module Plugins
5
5
  Result = ::ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins
6
6
  Service = ::ConvenientService::Service::Plugins
7
+ Step = ::ConvenientService::Service::Plugins::HasResultSteps::Entities::Step::Plugins
7
8
  end
8
9
  end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConvenientService
4
+ module Service
5
+ module Plugins
6
+ module HasInspect
7
+ module Concern
8
+ include Support::Concern
9
+
10
+ instance_methods do
11
+ ##
12
+ # @return [String]
13
+ # @since 0.2.0
14
+ #
15
+ def inspect
16
+ "<#{self.class.name}>"
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "has_inspect/concern"
@@ -8,10 +8,17 @@ module ConvenientService
8
8
  class CreateResultClass < Support::Command
9
9
  attr_reader :service_class
10
10
 
11
+ ##
12
+ # @param service_class [Class]
13
+ # @return [void]
14
+ #
11
15
  def initialize(service_class:)
12
16
  @service_class = service_class
13
17
  end
14
18
 
19
+ ##
20
+ # @return [void]
21
+ #
15
22
  def call
16
23
  result_class.include Entities::Result::Concern
17
24
 
@@ -45,6 +52,9 @@ module ConvenientService
45
52
 
46
53
  private
47
54
 
55
+ ##
56
+ # @return [Class]
57
+ #
48
58
  def result_class
49
59
  @result_class ||= Utils::Module.get_own_const(service_class, :Result) || ::Class.new(Entities::Result)
50
60
  end
@@ -7,28 +7,25 @@ module ConvenientService
7
7
  module Concern
8
8
  module ClassMethods
9
9
  ##
10
- #
10
+ # @api public
11
+ # @return [ConvenientService::Service::Plugins::HasResult::Entities::Result]
11
12
  #
12
13
  def result(...)
13
14
  new(...).result
14
15
  end
15
16
 
16
17
  ##
17
- # https://ruby-doc.org/core-2.5.0/Class.html#method-i-allocate
18
- # https://frontdeveloper.pl/2018/11/ruby-allocate-method/
18
+ # @api private
19
+ # @return [ConvenientService::Service::Plugins::HasResult::Entities::Result]
19
20
  #
20
- def new_without_initialize
21
- allocate
22
- end
23
-
24
- ##
25
- # NOTE: This method is internally used by custom RSpec helper `stub_service`. It should NOT be used in the client code.
21
+ # @internal
22
+ # NOTE: This method is internally used by custom RSpec helper `stub_service`. It should NOT be used in the client code.
26
23
  #
27
24
  def success(
28
25
  service: Constants::DEFAULT_SERVICE_INSTANCE,
29
26
  data: Constants::DEFAULT_SUCCESS_DATA
30
27
  )
31
- result_class.new(
28
+ result_class.create(
32
29
  service: service,
33
30
  status: Constants::SUCCESS_STATUS,
34
31
  data: data,
@@ -38,14 +35,18 @@ module ConvenientService
38
35
  end
39
36
 
40
37
  ##
41
- # NOTE: This method is internally used by custom RSpec helper `stub_service`. It should NOT be used in the client code.
38
+ # @api private
39
+ # @return [ConvenientService::Service::Plugins::HasResult::Entities::Result]
40
+ #
41
+ # @internal
42
+ # NOTE: This method is internally used by custom RSpec helper `stub_service`. It should NOT be used in the client code.
42
43
  #
43
44
  def failure(
44
45
  service: Constants::DEFAULT_SERVICE_INSTANCE,
45
46
  data: Constants::DEFAULT_FAILURE_DATA,
46
47
  message: data.any? ? data.first.join(" ") : Constants::DEFAULT_FAILURE_MESSAGE
47
48
  )
48
- result_class.new(
49
+ result_class.create(
49
50
  service: service,
50
51
  status: Constants::FAILURE_STATUS,
51
52
  data: data,
@@ -55,14 +56,18 @@ module ConvenientService
55
56
  end
56
57
 
57
58
  ##
58
- # NOTE: This method is internally used by custom RSpec helper `stub_service`. It should NOT be used in the client code.
59
+ # @api private
60
+ # @return [ConvenientService::Service::Plugins::HasResult::Entities::Result]
61
+ #
62
+ # @internal
63
+ # NOTE: This method is internally used by custom RSpec helper `stub_service`. It should NOT be used in the client code.
59
64
  #
60
65
  def error(
61
66
  service: Constants::DEFAULT_SERVICE_INSTANCE,
62
67
  message: Constants::DEFAULT_ERROR_MESSAGE,
63
68
  code: Constants::DEFAULT_ERROR_CODE
64
69
  )
65
- result_class.new(
70
+ result_class.create(
66
71
  service: service,
67
72
  status: Constants::ERROR_STATUS,
68
73
  data: Constants::ERROR_DATA,
@@ -71,6 +76,10 @@ module ConvenientService
71
76
  )
72
77
  end
73
78
 
79
+ ##
80
+ # @api private
81
+ # @return [Class]
82
+ #
74
83
  def result_class
75
84
  @result_class ||= Commands::CreateResultClass.call(service_class: self)
76
85
  end
@@ -6,21 +6,37 @@ module ConvenientService
6
6
  module HasResult
7
7
  module Concern
8
8
  module InstanceMethods
9
+ ##
10
+ # @api public
11
+ # @raise [ConvenientService::Service::Plugins::HasResult::Errors::ResultIsNotOverridden]
12
+ #
9
13
  def result
10
14
  raise Errors::ResultIsNotOverridden.new(service: self)
11
15
  end
12
16
 
13
17
  ##
14
- # TODO: Specs.
18
+ # @api public
19
+ # @param kwargs [Hash]
20
+ # @return [ConvenientService::Service::Plugins::HasResult::Entities::Result]
15
21
  #
16
22
  def success(**kwargs)
17
23
  self.class.success(**kwargs.merge(service: self))
18
24
  end
19
25
 
26
+ ##
27
+ # @api public
28
+ # @param kwargs [Hash]
29
+ # @return [ConvenientService::Service::Plugins::HasResult::Entities::Result]
30
+ #
20
31
  def failure(**kwargs)
21
32
  self.class.failure(**kwargs.merge(service: self))
22
33
  end
23
34
 
35
+ ##
36
+ # @api public
37
+ # @param kwargs [Hash]
38
+ # @return [ConvenientService::Service::Plugins::HasResult::Entities::Result]
39
+ #
24
40
  def error(**kwargs)
25
41
  self.class.error(**kwargs.merge(service: self))
26
42
  end
@@ -1,15 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "concern/instance_methods"
4
-
5
3
  module ConvenientService
6
4
  module Service
7
5
  module Plugins
8
6
  module HasResult
9
7
  module Entities
10
8
  class Result
9
+ ##
10
+ # @internal
11
+ # This concern is needed for `CastResultClass` and `be_success`, `be_error`, `be_failure` matchers.
12
+ #
11
13
  module Concern
12
- include Support::Concern
13
14
  end
14
15
  end
15
16
  end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConvenientService
4
+ module Service
5
+ module Plugins
6
+ module HasResult
7
+ module Entities
8
+ class Result
9
+ module Plugins
10
+ module HasInspect
11
+ module Concern
12
+ include Support::Concern
13
+
14
+ instance_methods do
15
+ ##
16
+ # @return [String]
17
+ # @since 0.2.0
18
+ #
19
+ def inspect
20
+ "<#{service.class.name}::Result status: :#{status}>"
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "has_inspect/concern"
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConvenientService
4
+ module Service
5
+ module Plugins
6
+ module HasResult
7
+ module Entities
8
+ class Result
9
+ module Plugins
10
+ module HasJsendStatusAndAttributes
11
+ module Commands
12
+ class CastJSendAttributes < Support::Command
13
+ attr_reader :attributes
14
+
15
+ def initialize(attributes:)
16
+ @attributes = attributes
17
+ end
18
+
19
+ def call
20
+ Structs::JSendAttributes.new(
21
+ service: attributes[:service],
22
+ status: Entities::Status.cast!(attributes[:status]),
23
+ data: Entities::Data.cast!(attributes[:data]),
24
+ message: Entities::Message.cast!(attributes[:message]),
25
+ code: Entities::Code.cast!(attributes[:code])
26
+ )
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "commands/cast_jsend_attributes"