convenient_service 0.6.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (101) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +58 -0
  3. data/ROADMAP.md +14 -1
  4. data/convenient_service.gemspec +2 -1
  5. data/lib/convenient_service/aliases.rb +10 -0
  6. data/lib/convenient_service/common/plugins/has_around_callbacks/middleware.rb +24 -4
  7. data/lib/convenient_service/common/plugins/has_callbacks/container.rb +17 -0
  8. data/lib/convenient_service/common/plugins/has_callbacks/entities/callback.rb +98 -5
  9. data/lib/convenient_service/common/plugins/has_callbacks/middleware.rb +25 -4
  10. data/lib/convenient_service/common/plugins/has_callbacks.rb +1 -0
  11. data/lib/convenient_service/common/plugins/has_constructor_without_initialize/concern.rb +19 -0
  12. data/lib/convenient_service/common/plugins.rb +1 -1
  13. data/lib/convenient_service/configs/minimal.rb +176 -0
  14. data/lib/convenient_service/configs/standard.rb +30 -105
  15. data/lib/convenient_service/configs.rb +1 -0
  16. data/lib/convenient_service/dependencies.rb +20 -0
  17. data/lib/convenient_service/examples/dry/gemfile/dry_service/config.rb +5 -5
  18. data/lib/convenient_service/examples/rails/gemfile/rails_service/config.rb +7 -7
  19. data/lib/convenient_service/examples/rails/gemfile/services/format.rb +35 -6
  20. data/lib/convenient_service/examples/rails/gemfile/services/format_header.rb +1 -2
  21. data/lib/convenient_service/examples/rails/gemfile/services/merge_sections.rb +25 -0
  22. data/lib/convenient_service/examples/rails/gemfile/services/replace_file_content.rb +37 -0
  23. data/lib/convenient_service/examples/rails/gemfile/services.rb +8 -4
  24. data/lib/convenient_service/examples/standard/gemfile/services/format.rb +45 -6
  25. data/lib/convenient_service/examples/standard/gemfile/services/merge_sections.rb +52 -0
  26. data/lib/convenient_service/examples/standard/gemfile/services/replace_file_content.rb +48 -0
  27. data/lib/convenient_service/examples/standard/gemfile/services.rb +8 -4
  28. data/lib/convenient_service/factories/arguments.rb +43 -0
  29. data/lib/convenient_service/factories/results.rb +214 -0
  30. data/lib/convenient_service/factories/services.rb +189 -0
  31. data/lib/convenient_service/factories/step/instance.rb +32 -0
  32. data/lib/convenient_service/factories/step.rb +3 -0
  33. data/lib/convenient_service/factories/steps.rb +126 -0
  34. data/lib/convenient_service/factories.rb +22 -0
  35. data/lib/convenient_service/factory.rb +21 -0
  36. data/lib/convenient_service/rspec/helpers/custom/ignoring_error.rb +3 -0
  37. data/lib/convenient_service/rspec/helpers/custom/wrap_method/entities/wrapped_method.rb +29 -3
  38. data/lib/convenient_service/rspec/matchers/custom/be_descendant_of.rb +2 -2
  39. data/lib/convenient_service/rspec/matchers/custom/be_direct_descendant_of.rb +2 -2
  40. data/lib/convenient_service/rspec/matchers/custom/delegate_to.rb +9 -0
  41. data/lib/convenient_service/rspec/matchers/custom/results/base/commands/find_result_method_step.rb +74 -0
  42. data/lib/convenient_service/rspec/matchers/custom/results/base/commands/find_result_result_method_step.rb +66 -0
  43. data/lib/convenient_service/rspec/matchers/custom/results/base/commands/find_result_service_step.rb +48 -0
  44. data/lib/convenient_service/rspec/matchers/custom/results/base/commands/generate_expected_step_part.rb +42 -0
  45. data/lib/convenient_service/rspec/matchers/custom/results/base/commands/generate_got_step_part.rb +42 -0
  46. data/lib/convenient_service/rspec/matchers/custom/results/base/commands/match_result_step.rb +89 -0
  47. data/lib/convenient_service/rspec/matchers/custom/results/base/commands.rb +10 -0
  48. data/lib/convenient_service/rspec/matchers/custom/results/base/errors.rb +35 -0
  49. data/lib/convenient_service/rspec/matchers/custom/results/base.rb +78 -12
  50. data/lib/convenient_service/service/plugins/has_result/concern/class_methods.rb +3 -3
  51. data/lib/convenient_service/service/plugins/has_result/constants.rb +0 -3
  52. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/can_have_parent_result/concern.rb +85 -0
  53. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/can_have_parent_result/initialize/middleware.rb +27 -0
  54. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/can_have_parent_result/initialize.rb +3 -0
  55. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/can_have_parent_result/to_kwargs/middleware.rb +25 -0
  56. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/can_have_parent_result/to_kwargs.rb +3 -0
  57. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/can_have_parent_result.rb +5 -0
  58. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/has_jsend_status_and_attributes/concern/instance_methods.rb +7 -1
  59. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/has_jsend_status_and_attributes/entities/data.rb +17 -0
  60. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/has_step/concern.rb +27 -0
  61. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/has_step/initialize/middleware.rb +27 -0
  62. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/has_step/initialize.rb +3 -0
  63. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/has_step/to_kwargs/middleware.rb +25 -0
  64. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/has_step/to_kwargs.rb +3 -0
  65. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/has_step.rb +5 -0
  66. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins.rb +2 -0
  67. data/lib/convenient_service/service/plugins/has_result_method_steps.rb +0 -2
  68. data/lib/convenient_service/service/plugins/has_result_steps/concern.rb +18 -7
  69. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/commands/define_method_in_container.rb +2 -2
  70. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/entities/callers/reassignment/commands/define_method_in_container.rb +1 -1
  71. data/lib/convenient_service/service/plugins/has_result_steps/entities/step/concern/instance_methods.rb +26 -1
  72. data/lib/convenient_service/service/plugins/has_result_steps/entities/step/plugins/can_have_parent_result/middleware.rb +23 -0
  73. data/lib/convenient_service/service/plugins/has_result_steps/entities/step/plugins/can_have_parent_result.rb +3 -0
  74. data/lib/convenient_service/service/plugins/has_result_steps/entities/step/plugins.rb +1 -0
  75. data/lib/convenient_service/service/plugins/has_result_steps/entities/step_collection.rb +13 -0
  76. data/lib/convenient_service/service/plugins/has_result_steps/middleware.rb +18 -4
  77. data/lib/convenient_service/service/plugins/raises_on_double_result/middleware.rb +37 -2
  78. data/lib/convenient_service/service/plugins/rescues_result_unhandled_exceptions/commands/format_backtrace.rb +80 -0
  79. data/lib/convenient_service/service/plugins/rescues_result_unhandled_exceptions/commands/format_cause.rb +78 -0
  80. data/lib/convenient_service/service/plugins/rescues_result_unhandled_exceptions/commands/format_exception.rb +169 -0
  81. data/lib/convenient_service/service/plugins/rescues_result_unhandled_exceptions/commands/format_line.rb +40 -0
  82. data/lib/convenient_service/service/plugins/rescues_result_unhandled_exceptions/commands.rb +7 -0
  83. data/lib/convenient_service/service/plugins/rescues_result_unhandled_exceptions/constants.rb +13 -0
  84. data/lib/convenient_service/service/plugins/rescues_result_unhandled_exceptions/middleware.rb +58 -0
  85. data/lib/convenient_service/service/plugins/rescues_result_unhandled_exceptions.rb +5 -0
  86. data/lib/convenient_service/services/run_method_in_organizer.rb +28 -0
  87. data/lib/convenient_service/services/run_own_method_in_organizer.rb +64 -0
  88. data/lib/convenient_service/{service/plugins/has_result_method_steps/services.rb → services.rb} +0 -1
  89. data/lib/convenient_service/support/copyable.rb +6 -2
  90. data/lib/convenient_service/support/dependency_container/errors.rb +1 -1
  91. data/lib/convenient_service/support/not_passed.rb +3 -1
  92. data/lib/convenient_service/support/undefined.rb +9 -0
  93. data/lib/convenient_service/support.rb +2 -0
  94. data/lib/convenient_service/version.rb +1 -1
  95. data/lib/convenient_service.rb +6 -0
  96. data/logo.png +0 -0
  97. metadata +68 -11
  98. data/lib/convenient_service/service/plugins/has_result_method_steps/errors.rb +0 -23
  99. data/lib/convenient_service/service/plugins/has_result_method_steps/services/method_step_config.rb +0 -55
  100. data/lib/convenient_service/service/plugins/has_result_method_steps/services/run_method_in_organizer.rb +0 -30
  101. data/lib/convenient_service/service/plugins/has_result_method_steps/services/run_own_method_in_organizer.rb +0 -52
@@ -2,208 +2,133 @@
2
2
 
3
3
  module ConvenientService
4
4
  module Configs
5
+ ##
6
+ # Default configuration for the user-defined services.
7
+ #
5
8
  module Standard
6
9
  include Support::Concern
7
10
 
8
11
  ##
9
- # IMPORTANT: Order of plugins matters.
12
+ # @internal
13
+ # IMPORTANT: Order of plugins matters.
10
14
  #
11
- # NOTE: `class_exec` (that is used under the hood by `included`) defines `class Result` in the global namespace.
12
- # That is why `class self::Result` is used.
13
- #
14
- # https://stackoverflow.com/a/51965126/12201472
15
+ # NOTE: `class_exec` (that is used under the hood by `included`) defines `class Result` in the global namespace.
16
+ # That is why `class self::Result` is used.
17
+ # - https://stackoverflow.com/a/51965126/12201472
15
18
  #
16
19
  # rubocop:disable Lint/ConstantDefinitionInBlock
17
20
  included do
18
- include Core
21
+ include Configs::Minimal
19
22
 
20
23
  concerns do
21
- use Plugins::Common::HasInternals::Concern
22
- use Plugins::Common::HasConstructor::Concern
23
- use Plugins::Common::HasConstructorWithoutInitialize::Concern
24
-
25
24
  use Plugins::Common::CachesConstructorParams::Concern
26
25
  use Plugins::Common::CanBeCopied::Concern
27
- use Plugins::Service::HasResult::Concern
28
- use Plugins::Service::HasResultShortSyntax::Concern
29
- use Plugins::Service::HasResultSteps::Concern
30
26
  use Plugins::Service::CanRecalculateResult::Concern
27
+ use Plugins::Service::HasResultShortSyntax::Concern
31
28
  use Plugins::Service::HasResultStatusCheckShortSyntax::Concern
32
29
 
33
30
  use Plugins::Common::HasCallbacks::Concern
34
31
  use Plugins::Common::HasAroundCallbacks::Concern
35
-
36
- use Plugins::Service::HasInspect::Concern
37
- ##
38
- # NOTE: Optional plugins.
39
- # TODO: Specs.
40
- #
41
- # use Plugins::Common::HasConfig::Concern
42
- #
43
32
  end
44
33
 
45
34
  middlewares :initialize do
46
- use Plugins::Common::NormalizesEnv::Middleware
47
-
48
35
  use Plugins::Common::CachesConstructorParams::Middleware
49
36
  end
50
37
 
51
38
  middlewares :result do
52
- use Plugins::Common::NormalizesEnv::Middleware
39
+ insert_before \
40
+ Plugins::Service::HasResult::Middleware,
41
+ Plugins::Common::HasCallbacks::Middleware
53
42
 
54
- use Plugins::Service::HasResult::Middleware
55
-
56
- use Plugins::Service::HasResultSteps::Middleware
57
-
58
- use Plugins::Common::HasCallbacks::Middleware
59
- use Plugins::Common::HasAroundCallbacks::Middleware
43
+ insert_after \
44
+ Plugins::Common::HasCallbacks::Middleware,
45
+ Plugins::Common::HasAroundCallbacks::Middleware
60
46
 
61
47
  use Plugins::Service::RaisesOnDoubleResult::Middleware
62
48
 
63
49
  use Plugins::Common::CachesReturnValue::Middleware
64
50
  end
65
51
 
66
- middlewares :success do
67
- use Plugins::Common::NormalizesEnv::Middleware
52
+ middlewares :step do
53
+ use Plugins::Common::HasCallbacks::Middleware
54
+ use Plugins::Common::HasAroundCallbacks::Middleware
55
+ end
68
56
 
57
+ middlewares :success do
69
58
  use Plugins::Service::HasResultShortSyntax::Success::Middleware
70
59
  end
71
60
 
72
61
  middlewares :failure do
73
- use Plugins::Common::NormalizesEnv::Middleware
74
-
75
62
  use Plugins::Service::HasResultShortSyntax::Failure::Middleware
76
63
  end
77
64
 
78
65
  middlewares :error do
79
- use Plugins::Common::NormalizesEnv::Middleware
80
-
81
66
  use Plugins::Service::HasResultShortSyntax::Error::Middleware
82
67
  end
83
68
 
84
- middlewares :result, scope: :class do
85
- use Plugins::Common::NormalizesEnv::Middleware
86
- end
87
-
88
69
  middlewares :step, scope: :class do
89
- use Plugins::Common::NormalizesEnv::Middleware
90
-
91
70
  use Plugins::Service::HasResultMethodSteps::Middleware
92
71
  end
93
72
 
94
- class self::Internals
95
- include Core
96
-
97
- concerns do
98
- use Plugins::Internals::HasCache::Concern
99
- end
100
- end
101
-
102
73
  class self::Result
103
- include Core
104
-
105
74
  concerns do
106
- use Plugins::Common::HasInternals::Concern
107
- use Plugins::Common::HasConstructor::Concern
108
-
109
- use Plugins::Result::HasJsendStatusAndAttributes::Concern
110
-
111
75
  use Plugins::Result::HasResultShortSyntax::Concern
112
76
  use Plugins::Result::CanRecalculateResult::Concern
113
77
 
114
- use Plugins::Result::HasInspect::Concern
78
+ use Plugins::Result::HasStep::Concern
79
+ use Plugins::Result::CanHaveParentResult::Concern
115
80
  end
116
81
 
117
82
  middlewares :initialize do
118
- use Plugins::Common::NormalizesEnv::Middleware
119
-
120
- use Plugins::Result::HasJsendStatusAndAttributes::Middleware
83
+ use Plugins::Result::HasStep::Initialize::Middleware
84
+ use Plugins::Result::CanHaveParentResult::Initialize::Middleware
121
85
  end
122
86
 
123
87
  middlewares :success? do
124
- use Plugins::Common::NormalizesEnv::Middleware
125
-
126
88
  use Plugins::Result::MarksResultStatusAsChecked::Middleware
127
89
  end
128
90
 
129
91
  middlewares :failure? do
130
- use Plugins::Common::NormalizesEnv::Middleware
131
-
132
92
  use Plugins::Result::MarksResultStatusAsChecked::Middleware
133
93
  end
134
94
 
135
95
  middlewares :error? do
136
- use Plugins::Common::NormalizesEnv::Middleware
137
-
138
96
  use Plugins::Result::MarksResultStatusAsChecked::Middleware
139
97
  end
140
98
 
141
99
  middlewares :not_success? do
142
- use Plugins::Common::NormalizesEnv::Middleware
143
-
144
100
  use Plugins::Result::MarksResultStatusAsChecked::Middleware
145
101
  end
146
102
 
147
103
  middlewares :not_failure? do
148
- use Plugins::Common::NormalizesEnv::Middleware
149
-
150
104
  use Plugins::Result::MarksResultStatusAsChecked::Middleware
151
105
  end
152
106
 
153
107
  middlewares :not_error? do
154
- use Plugins::Common::NormalizesEnv::Middleware
155
-
156
108
  use Plugins::Result::MarksResultStatusAsChecked::Middleware
157
109
  end
158
110
 
159
111
  middlewares :data do
160
- use Plugins::Common::NormalizesEnv::Middleware
161
-
162
112
  use Plugins::Result::RaisesOnNotCheckedResultStatus::Middleware
163
113
  end
164
114
 
165
115
  middlewares :message do
166
- use Plugins::Common::NormalizesEnv::Middleware
167
-
168
116
  use Plugins::Result::RaisesOnNotCheckedResultStatus::Middleware
169
117
  end
170
118
 
171
119
  middlewares :code do
172
- use Plugins::Common::NormalizesEnv::Middleware
173
-
174
120
  use Plugins::Result::RaisesOnNotCheckedResultStatus::Middleware
175
121
  end
176
122
 
177
- class self::Internals
178
- include Core
179
-
180
- concerns do
181
- use Plugins::Internals::HasCache::Concern
182
- end
123
+ middlewares :to_kwargs do
124
+ use Plugins::Result::HasStep::ToKwargs::Middleware
125
+ use Plugins::Result::CanHaveParentResult::ToKwargs::Middleware
183
126
  end
184
127
  end
185
128
 
186
129
  class self::Step
187
- include Core
188
-
189
- concerns do
190
- use Plugins::Common::HasInternals::Concern
191
-
192
- use Plugins::Step::HasInspect::Concern
193
- end
194
-
195
- middlewares :result do
196
- use Plugins::Common::NormalizesEnv::Middleware
197
-
198
- use Plugins::Common::CachesReturnValue::Middleware
199
- end
200
-
201
- class self::Internals
202
- include Core
203
-
204
- concerns do
205
- use Plugins::Internals::HasCache::Concern
206
- end
130
+ middlewares :calculate_result do
131
+ use Plugins::Step::CanHaveParentResult::Middleware
207
132
  end
208
133
  end
209
134
 
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "configs/minimal"
3
4
  require_relative "configs/standard"
4
5
 
5
6
  require_relative "configs/aliases"
@@ -75,6 +75,14 @@ module ConvenientService
75
75
  require_relative "service/plugins/has_result_params_validations/using_dry_validation"
76
76
  end
77
77
 
78
+ ##
79
+ # @return [Boolean]
80
+ # @note Expected to be called from app entry points like `initializers` in Rails.
81
+ #
82
+ def require_rescues_result_unhandled_exceptions
83
+ require_relative "service/plugins/rescues_result_unhandled_exceptions"
84
+ end
85
+
78
86
  ##
79
87
  # @return [Boolean]
80
88
  # @note Expected to be called from `irb`, `pry`, `spec_helper.rb`, etc.
@@ -106,6 +114,18 @@ module ConvenientService
106
114
  require_relative "rspec"
107
115
  end
108
116
 
117
+ ##
118
+ # Loads test factory.
119
+ #
120
+ # @return [Boolean]
121
+ # @note Expected to be called from `spec_helper.rb`.
122
+ #
123
+ def require_factory
124
+ require "faker"
125
+
126
+ require_relative "factory"
127
+ end
128
+
109
129
  ##
110
130
  # Loads standard config examples.
111
131
  #
@@ -16,26 +16,26 @@ module ConvenientService
16
16
 
17
17
  included do |service_class|
18
18
  service_class.class_exec do
19
- include Configs::Standard
19
+ include ConvenientService::Standard::Config
20
20
 
21
21
  ##
22
22
  # NOTE: `AssignsAttributesInConstructor::UsingDryInitializer` plugin.
23
23
  #
24
24
  concerns do
25
- use Plugins::Common::AssignsAttributesInConstructor::UsingDryInitializer::Concern
25
+ use ConvenientService::Plugins::Common::AssignsAttributesInConstructor::UsingDryInitializer::Concern
26
26
  end
27
27
 
28
28
  ##
29
29
  # NOTE: `HasResultParamsValidations::UsingDryValidation` plugin.
30
30
  #
31
31
  concerns do
32
- use Plugins::Service::HasResultParamsValidations::UsingDryValidation::Concern
32
+ use ConvenientService::Plugins::Service::HasResultParamsValidations::UsingDryValidation::Concern
33
33
  end
34
34
 
35
35
  middlewares :result do
36
36
  insert_before \
37
- Plugins::Service::HasResultSteps::Middleware,
38
- Plugins::Service::HasResultParamsValidations::UsingDryValidation::Middleware
37
+ ConvenientService::Plugins::Service::HasResult::Middleware,
38
+ ConvenientService::Plugins::Service::HasResultParamsValidations::UsingDryValidation::Middleware
39
39
  end
40
40
  end
41
41
  end
@@ -16,37 +16,37 @@ module ConvenientService
16
16
 
17
17
  included do |service_class|
18
18
  service_class.class_exec do
19
- include Configs::Standard
19
+ include ConvenientService::Standard::Config
20
20
 
21
21
  ##
22
22
  # NOTE: `AssignsAttributesInConstructor::UsingActiveModelAttributeAssignment` plugin.
23
23
  #
24
24
  concerns do
25
- use Plugins::Common::AssignsAttributesInConstructor::UsingActiveModelAttributeAssignment::Concern
25
+ use ConvenientService::Plugins::Common::AssignsAttributesInConstructor::UsingActiveModelAttributeAssignment::Concern
26
26
  end
27
27
 
28
28
  middlewares :initialize do
29
- use Plugins::Common::AssignsAttributesInConstructor::UsingActiveModelAttributeAssignment::Middleware
29
+ use ConvenientService::Plugins::Common::AssignsAttributesInConstructor::UsingActiveModelAttributeAssignment::Middleware
30
30
  end
31
31
 
32
32
  ##
33
33
  # NOTE: `HasAttributes::UsingActiveModelAttributes` plugin.
34
34
  #
35
35
  concerns do
36
- use Plugins::Common::HasAttributes::UsingActiveModelAttributes::Concern
36
+ use ConvenientService::Plugins::Common::HasAttributes::UsingActiveModelAttributes::Concern
37
37
  end
38
38
 
39
39
  ##
40
40
  # NOTE: `HasResultParamsValidations::UsingActiveModelValidations` plugin.
41
41
  #
42
42
  concerns do
43
- use Plugins::Service::HasResultParamsValidations::UsingActiveModelValidations::Concern
43
+ use ConvenientService::Plugins::Service::HasResultParamsValidations::UsingActiveModelValidations::Concern
44
44
  end
45
45
 
46
46
  middlewares :result do
47
47
  insert_before \
48
- Plugins::Service::HasResultSteps::Middleware,
49
- Plugins::Service::HasResultParamsValidations::UsingActiveModelValidations::Middleware
48
+ ConvenientService::Plugins::Service::HasResult::Middleware,
49
+ ConvenientService::Plugins::Service::HasResultParamsValidations::UsingActiveModelValidations::Middleware
50
50
  end
51
51
  end
52
52
  end
@@ -16,17 +16,46 @@ module ConvenientService
16
16
 
17
17
  attribute :path, :string
18
18
 
19
- step Services::ReadFileContent, in: :path, out: :content
20
- step Services::StripComments, in: :content, out: :content_without_comments
21
- step Services::ParseContent, in: {content: :content_without_comments}, out: :parsed_content
22
- step Services::FormatHeader, in: :parsed_content, out: {formatted_content: :formatted_header_content}
23
- step Services::FormatBody, in: :parsed_content, out: {formatted_content: :formatted_body_content}
19
+ validates :path, presence: true if ConvenientService::Dependencies.support_has_result_params_validations_using_active_model_validations?
20
+
21
+ step Services::ReadFileContent,
22
+ in: :path,
23
+ out: :content
24
+
25
+ step Services::StripComments,
26
+ in: :content,
27
+ out: :content_without_comments
28
+
29
+ step Services::ParseContent,
30
+ in: {content: :content_without_comments},
31
+ out: :parsed_content
32
+
33
+ step Services::FormatHeader,
34
+ in: :parsed_content,
35
+ out: {formatted_content: :formatted_header_content}
36
+
37
+ step Services::FormatBody,
38
+ in: :parsed_content,
39
+ out: {formatted_content: :formatted_body_content}
40
+
41
+ step Services::MergeSections,
42
+ in: [
43
+ {header: :formatted_header_content},
44
+ {body: :formatted_body_content}
45
+ ],
46
+ out: :merged_sections
47
+
48
+ step Services::ReplaceFileContent,
49
+ in: [
50
+ :path,
51
+ {content: :merged_sections}
52
+ ]
24
53
 
25
54
  before :result do
26
55
  @progressbar = ::ProgressBar.create(title: "Formatting", total: steps.count)
27
56
  end
28
57
 
29
- after :step do |step_result|
58
+ after :step do |step|
30
59
  @progressbar.increment
31
60
  end
32
61
  end
@@ -38,8 +38,7 @@ module ConvenientService
38
38
  #
39
39
  # TODO: Implement custom types.
40
40
  #
41
- attr_accessor :parsed_content
42
-
41
+ attribute :parsed_content
43
42
  attribute :skip_frozen_string_literal, :boolean, default: false
44
43
 
45
44
  def result
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConvenientService
4
+ module Examples
5
+ module Rails
6
+ module Gemfile
7
+ module Services
8
+ class MergeSections
9
+ include RailsService::Config
10
+
11
+ attribute :header, :string
12
+ attribute :body, :string
13
+
14
+ validates :header, presence: true if ConvenientService::Dependencies.support_has_result_params_validations_using_active_model_validations?
15
+ validates :body, presence: true if ConvenientService::Dependencies.support_has_result_params_validations_using_active_model_validations?
16
+
17
+ def result
18
+ success(merged_sections: "#{header}\n#{body}")
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConvenientService
4
+ module Examples
5
+ module Rails
6
+ module Gemfile
7
+ module Services
8
+ class ReplaceFileContent
9
+ include RailsService::Config
10
+
11
+ attribute :path, :string
12
+ attribute :content, :string
13
+
14
+ validates :path, presence: true if ConvenientService::Dependencies.support_has_result_params_validations_using_active_model_validations?
15
+
16
+ validate :content_not_nil if ConvenientService::Dependencies.support_has_result_params_validations_using_active_model_validations?
17
+
18
+ step Services::AssertFileExists, in: :path
19
+ step :result, in: :path
20
+
21
+ def result
22
+ ::File.write(path, content)
23
+
24
+ success
25
+ end
26
+
27
+ private
28
+
29
+ def content_not_nil
30
+ errors.add(:content, "can't be nil") if content.nil?
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -1,17 +1,21 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "services/run_shell"
4
+
4
5
  require_relative "services/assert_file_exists"
5
6
  require_relative "services/assert_file_not_empty"
6
7
  require_relative "services/assert_node_available"
7
8
  require_relative "services/assert_npm_package_available"
8
9
  require_relative "services/assert_valid_ruby_syntax"
9
- require_relative "services/read_file_content"
10
- require_relative "services/strip_comments"
11
- require_relative "services/parse_content"
12
- require_relative "services/print_shell_command"
13
10
  require_relative "services/format_header"
14
11
  require_relative "services/format_gems_without_envs"
15
12
  require_relative "services/format_gems_with_envs"
16
13
  require_relative "services/format_body"
14
+ require_relative "services/merge_sections"
15
+ require_relative "services/parse_content"
16
+ require_relative "services/print_shell_command"
17
+ require_relative "services/read_file_content"
18
+ require_relative "services/replace_file_content"
19
+ require_relative "services/strip_comments"
20
+
17
21
  require_relative "services/format"
@@ -16,23 +16,62 @@ module ConvenientService
16
16
 
17
17
  attr_reader :path
18
18
 
19
- step Services::ReadFileContent, in: :path, out: :content
20
- step Services::StripComments, in: :content, out: :content_without_comments
21
- step Services::ParseContent, in: {content: :content_without_comments}, out: :parsed_content
22
- step Services::FormatHeader, in: :parsed_content, out: {formatted_content: :formatted_header_content}
23
- step Services::FormatBody, in: :parsed_content, out: {formatted_content: :formatted_body_content}
19
+ step :validate_path,
20
+ in: :path
21
+
22
+ step Services::ReadFileContent,
23
+ in: :path,
24
+ out: :content
25
+
26
+ step Services::StripComments,
27
+ in: :content,
28
+ out: :content_without_comments
29
+
30
+ step Services::ParseContent,
31
+ in: {content: :content_without_comments},
32
+ out: :parsed_content
33
+
34
+ step Services::FormatHeader,
35
+ in: :parsed_content,
36
+ out: {formatted_content: :formatted_header_content}
37
+
38
+ step Services::FormatBody,
39
+ in: :parsed_content,
40
+ out: {formatted_content: :formatted_body_content}
41
+
42
+ step Services::MergeSections,
43
+ in: [
44
+ {header: :formatted_header_content},
45
+ {body: :formatted_body_content}
46
+ ],
47
+ out: :merged_sections
48
+
49
+ step Services::ReplaceFileContent,
50
+ in: [
51
+ :path,
52
+ {content: :merged_sections}
53
+ ]
24
54
 
25
55
  before :result do
26
56
  @progressbar = ::ProgressBar.create(title: "Formatting", total: steps.count)
27
57
  end
28
58
 
29
- after :step do |step_result|
59
+ after :step do |step|
30
60
  @progressbar.increment
31
61
  end
32
62
 
33
63
  def initialize(path:)
34
64
  @path = path
35
65
  end
66
+
67
+ private
68
+
69
+ def validate_path
70
+ return failure(path: "Path is `nil`") if path.nil?
71
+ return failure(path: "Path is empty") if path.empty?
72
+
73
+ success
74
+ end
36
75
  end
37
76
  end
38
77
  end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConvenientService
4
+ module Examples
5
+ module Standard
6
+ module Gemfile
7
+ module Services
8
+ class MergeSections
9
+ include ConvenientService::Standard::Config
10
+
11
+ attr_reader :header, :body
12
+
13
+ step :validate_header,
14
+ in: :header
15
+
16
+ step :validate_body,
17
+ in: :body
18
+
19
+ step :result,
20
+ in: [
21
+ :header,
22
+ :body
23
+ ]
24
+
25
+ def initialize(header:, body:)
26
+ @header = header
27
+ @body = body
28
+ end
29
+
30
+ def result
31
+ success(merged_sections: "#{header}\n#{body}")
32
+ end
33
+
34
+ def validate_header
35
+ return failure(header: "Header is `nil`") if header.nil?
36
+ return failure(header: "Header is empty") if header.empty?
37
+
38
+ success
39
+ end
40
+
41
+ def validate_body
42
+ return failure(body: "Body is `nil`") if body.nil?
43
+ return failure(body: "Body is empty") if body.empty?
44
+
45
+ success
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConvenientService
4
+ module Examples
5
+ module Standard
6
+ module Gemfile
7
+ module Services
8
+ class ReplaceFileContent
9
+ include ConvenientService::Standard::Config
10
+
11
+ attr_reader :path, :content
12
+
13
+ step :validate_path, in: :path
14
+ step :validate_content, in: :content
15
+ step Services::AssertFileExists, in: :path
16
+ step :result, in: :path
17
+
18
+ def initialize(path:, content:)
19
+ @path = path
20
+ @content = content
21
+ end
22
+
23
+ def result
24
+ ::File.write(path, content)
25
+
26
+ success
27
+ end
28
+
29
+ private
30
+
31
+ def validate_path
32
+ return failure(path: "Path is `nil`") if path.nil?
33
+ return failure(path: "Path is empty") if path.empty?
34
+
35
+ success
36
+ end
37
+
38
+ def validate_content
39
+ return failure(content: "Content is `nil`") if content.nil?
40
+
41
+ success
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end