convenient_service 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (83) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +40 -0
  3. data/ROADMAP.md +12 -1
  4. data/convenient_service.gemspec +2 -1
  5. data/lib/convenient_service/aliases.rb +1 -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.rb +1 -1
  12. data/lib/convenient_service/configs/standard.rb +25 -12
  13. data/lib/convenient_service/dependencies.rb +12 -0
  14. data/lib/convenient_service/examples/dry/gemfile/dry_service/config.rb +1 -1
  15. data/lib/convenient_service/examples/rails/gemfile/rails_service/config.rb +1 -1
  16. data/lib/convenient_service/examples/rails/gemfile/services/format.rb +35 -6
  17. data/lib/convenient_service/examples/rails/gemfile/services/format_header.rb +1 -2
  18. data/lib/convenient_service/examples/rails/gemfile/services/merge_sections.rb +25 -0
  19. data/lib/convenient_service/examples/rails/gemfile/services/replace_file_content.rb +37 -0
  20. data/lib/convenient_service/examples/rails/gemfile/services.rb +8 -4
  21. data/lib/convenient_service/examples/standard/gemfile/services/format.rb +45 -6
  22. data/lib/convenient_service/examples/standard/gemfile/services/merge_sections.rb +52 -0
  23. data/lib/convenient_service/examples/standard/gemfile/services/replace_file_content.rb +48 -0
  24. data/lib/convenient_service/examples/standard/gemfile/services.rb +8 -4
  25. data/lib/convenient_service/factories/arguments.rb +43 -0
  26. data/lib/convenient_service/factories/results.rb +214 -0
  27. data/lib/convenient_service/factories/services.rb +189 -0
  28. data/lib/convenient_service/factories/step/instance.rb +32 -0
  29. data/lib/convenient_service/factories/step.rb +3 -0
  30. data/lib/convenient_service/factories/steps.rb +126 -0
  31. data/lib/convenient_service/factories.rb +22 -0
  32. data/lib/convenient_service/factory.rb +21 -0
  33. data/lib/convenient_service/rspec/matchers/custom/be_descendant_of.rb +2 -2
  34. data/lib/convenient_service/rspec/matchers/custom/be_direct_descendant_of.rb +2 -2
  35. data/lib/convenient_service/rspec/matchers/custom/delegate_to.rb +9 -0
  36. data/lib/convenient_service/rspec/matchers/custom/results/base/commands/find_result_method_step.rb +74 -0
  37. data/lib/convenient_service/rspec/matchers/custom/results/base/commands/find_result_result_method_step.rb +66 -0
  38. data/lib/convenient_service/rspec/matchers/custom/results/base/commands/find_result_service_step.rb +48 -0
  39. data/lib/convenient_service/rspec/matchers/custom/results/base/commands/generate_expected_step_part.rb +42 -0
  40. data/lib/convenient_service/rspec/matchers/custom/results/base/commands/generate_got_step_part.rb +42 -0
  41. data/lib/convenient_service/rspec/matchers/custom/results/base/commands/match_result_step.rb +89 -0
  42. data/lib/convenient_service/rspec/matchers/custom/results/base/commands.rb +10 -0
  43. data/lib/convenient_service/rspec/matchers/custom/results/base/errors.rb +35 -0
  44. data/lib/convenient_service/rspec/matchers/custom/results/base.rb +73 -12
  45. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/can_have_parent_result/concern.rb +85 -0
  46. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/can_have_parent_result/initialize/middleware.rb +27 -0
  47. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/can_have_parent_result/initialize.rb +3 -0
  48. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/can_have_parent_result/to_kwargs/middleware.rb +25 -0
  49. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/can_have_parent_result/to_kwargs.rb +3 -0
  50. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/can_have_parent_result.rb +5 -0
  51. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/has_jsend_status_and_attributes/concern/instance_methods.rb +7 -1
  52. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/has_jsend_status_and_attributes/entities/data.rb +4 -0
  53. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/has_step/concern.rb +27 -0
  54. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/has_step/initialize/middleware.rb +27 -0
  55. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/has_step/initialize.rb +3 -0
  56. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/has_step/to_kwargs/middleware.rb +25 -0
  57. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/has_step/to_kwargs.rb +3 -0
  58. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/has_step.rb +5 -0
  59. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins.rb +2 -0
  60. data/lib/convenient_service/service/plugins/has_result_method_steps.rb +0 -2
  61. data/lib/convenient_service/service/plugins/has_result_steps/concern.rb +18 -7
  62. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/commands/define_method_in_container.rb +2 -2
  63. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/entities/callers/reassignment/commands/define_method_in_container.rb +1 -1
  64. data/lib/convenient_service/service/plugins/has_result_steps/entities/step/concern/instance_methods.rb +26 -1
  65. data/lib/convenient_service/service/plugins/has_result_steps/entities/step/plugins/can_have_parent_result/middleware.rb +23 -0
  66. data/lib/convenient_service/service/plugins/has_result_steps/entities/step/plugins/can_have_parent_result.rb +3 -0
  67. data/lib/convenient_service/service/plugins/has_result_steps/entities/step/plugins.rb +1 -0
  68. data/lib/convenient_service/service/plugins/has_result_steps/entities/step_collection.rb +13 -0
  69. data/lib/convenient_service/service/plugins/has_result_steps/middleware.rb +18 -4
  70. data/lib/convenient_service/services/run_method_in_organizer.rb +28 -0
  71. data/lib/convenient_service/services/run_own_method_in_organizer.rb +64 -0
  72. data/lib/convenient_service/{service/plugins/has_result_method_steps/services.rb → services.rb} +0 -1
  73. data/lib/convenient_service/support/copyable.rb +6 -2
  74. data/lib/convenient_service/support/dependency_container/errors.rb +1 -1
  75. data/lib/convenient_service/support/not_passed.rb +3 -1
  76. data/lib/convenient_service/version.rb +1 -1
  77. data/lib/convenient_service.rb +6 -0
  78. data/logo.png +0 -0
  79. metadata +58 -11
  80. data/lib/convenient_service/service/plugins/has_result_method_steps/errors.rb +0 -23
  81. data/lib/convenient_service/service/plugins/has_result_method_steps/services/method_step_config.rb +0 -55
  82. data/lib/convenient_service/service/plugins/has_result_method_steps/services/run_method_in_organizer.rb +0 -30
  83. data/lib/convenient_service/service/plugins/has_result_method_steps/services/run_own_method_in_organizer.rb +0 -52
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7ec5188e0830cb8a97b589ff6985d4aa1e361bce486dba86a1a1f697924d0be7
4
- data.tar.gz: ab763a1c604849509f5557938768bf019b5d962dd5d6fa62771b17e3c524025d
3
+ metadata.gz: 2d49bd8a8f45539edd750ba5388d4cd0dc7ab2e442380a24f38879f17c405944
4
+ data.tar.gz: 63adf6692c4989970e4fe1fa2b0c28bfa2c29020f33e9567d3fabbd9284f1944
5
5
  SHA512:
6
- metadata.gz: 32cf7296bf8a5ff16d7bfed6c20a855831e27b0300edede75ba7c737f357ed6ddf462641d7cacc9c5f7a65c64c3a4e59b04a60cee0ad9793f173fca7e9c0ad24
7
- data.tar.gz: 71aadf20b54f0f507eb6721999b2192630eb2265d660d40ecc461de311fb930376883d1546c9c030c03f15e90e0c02e172066adc2213cb61140cc3f8b45b5137
6
+ metadata.gz: 199c1e80a8dfeedc2a8d1873f8f75008d510fb48374ab116f7c51f49ef44cf034454280279a5fda2bdc16ccd69e6010cb70291bdf0f10295d3eabddfcc391a6e
7
+ data.tar.gz: 2f03ad73249e0f61ee1d2f2b3fde41c82d9bd2089ebcd95f01390df206f6dded8ee5c4880d409065f6df01ba63eec1cbf26caabaf94c4e4a962752bb1f9d967f
data/CHANGELOG.md CHANGED
@@ -1,5 +1,45 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.7.0](https://github.com/marian13/convenient_service/compare/v0.6.0...v0.7.0) (2023-02-13)
4
+
5
+
6
+ ### ⚠ BREAKING CHANGES
7
+
8
+ * **be_result:** introduce #of_step, #of_service, remove #of
9
+ * **has_result:** use callbacks before result
10
+
11
+ ### Features
12
+
13
+ * **be_result:** introduce #of_step, #of_service, remove #of ([0d9ba16](https://github.com/marian13/convenient_service/commit/0d9ba16f035ef50268fdf484b0fd83c58dbb328a))
14
+ * **be_result:** of_step supports method steps ([9127301](https://github.com/marian13/convenient_service/commit/9127301f41549fbb8c48b718e410ebd7c100e2e9))
15
+ * **can_have_parent_Result:** introduce CanHaveParentResult plugin ([55f0b0f](https://github.com/marian13/convenient_service/commit/55f0b0f558e2edb8b0fdc56a2d8b52bc44995a2f))
16
+ * **command:** expose command ([6abcb1a](https://github.com/marian13/convenient_service/commit/6abcb1ad481cad66eba2d4d409e3ce16b0f6591d))
17
+ * **configs:** add around callbacks for steps ([cb9d342](https://github.com/marian13/convenient_service/commit/cb9d34204d63a96e89ebebe2991e8ab77b2624f0))
18
+ * **has_around_callbacks:** pass arguments to around callbacks ([f682f9a](https://github.com/marian13/convenient_service/commit/f682f9a6fb3ef6e8ab50ccf0f7f3ba375a63092e))
19
+ * **has_callbacks:** pass arguments to callbacks ([2d7f720](https://github.com/marian13/convenient_service/commit/2d7f720e37a15874ec89167e46fb67a0631ec923))
20
+ * **has_jsend_status_and_attributes:** introduce Data#to_s ([c1e20c0](https://github.com/marian13/convenient_service/commit/c1e20c093ec4575a939645a2d951fe4e035154b8))
21
+ * **has_result_steps:** add callback trigger for step ([475d46a](https://github.com/marian13/convenient_service/commit/475d46a5abaf6fa85260cef90d78db60146b86d9))
22
+ * **has_result_steps:** introduce Step#original_result ([8891247](https://github.com/marian13/convenient_service/commit/8891247e5f0339d9e96e733d05b29e45a2e87e71))
23
+ * **has_step:** introduce HasStep plugin ([e6eee96](https://github.com/marian13/convenient_service/commit/e6eee965c8707b7378173f228b9b212834be434b))
24
+ * **not_passed:** add better #inspect ([d7ce5d9](https://github.com/marian13/convenient_service/commit/d7ce5d93a3aa5f139f6b7ee578975c1da52488c0))
25
+
26
+
27
+ ### Bug Fixes
28
+
29
+ * **be_descendant_of:** fix typo ([58e0f73](https://github.com/marian13/convenient_service/commit/58e0f73f74685614d24e0ee3c8208a5626f7ef42))
30
+ * **be_direct_descendant_of:** fix typo ([bda7d06](https://github.com/marian13/convenient_service/commit/bda7d063d1801309a58b5e4d43d738ebab5fef59))
31
+ * **ci:** change the version of yard ([185c23c](https://github.com/marian13/convenient_service/commit/185c23c40dacf997e29d0b7defd10020c22dc196))
32
+ * **copyable:** do not mutate input params ([0809592](https://github.com/marian13/convenient_service/commit/08095927ea03c60a00eb3b0d0323458b482827b0))
33
+ * **dependency_container:** fix typo ([b1cf420](https://github.com/marian13/convenient_service/commit/b1cf420a2e69cc51e928a8a1ad885a65ad7fb4fe))
34
+ * **has_result_steps:** use unsafe attributes in to_kwargs ([6c2b1d3](https://github.com/marian13/convenient_service/commit/6c2b1d3912106f6848b875d06a4d7ce1d79c4edb))
35
+ * **has_result_steps:** use unsafe_data ([e00c346](https://github.com/marian13/convenient_service/commit/e00c3468f5761e20678c7019f0d13f2569778b19))
36
+ * **has_result:** use callbacks before result ([eb46444](https://github.com/marian13/convenient_service/commit/eb46444536a7cf4aab869aa009c1a7c896c00c11))
37
+
38
+
39
+ ### Miscellaneous Chores
40
+
41
+ * release 0.7.0 ([6433f5a](https://github.com/marian13/convenient_service/commit/6433f5a67c3f21c620d500186e0308fb7b098efc))
42
+
3
43
  ## [0.6.0](https://github.com/marian13/convenient_service/compare/v0.5.0...v0.6.0) (2023-01-22)
4
44
 
5
45
 
data/ROADMAP.md CHANGED
@@ -32,7 +32,7 @@
32
32
  | High | 🚧 | Optimize `stack.dup` in `MethodMiddlewares#call` | Core v3 |
33
33
  | Medium | 🚧 | Make a decision of what to do with `printable_block` in custom RSpec matchers | |
34
34
  | Medium | 🚧 | User-friendly exception messages | |
35
- | High | 🚧 | Factories for POROs in specs ❗❗❗ | Start with `result_class`, `class self::Result`, `service_class`, `step_class`, `organizer_class` |
35
+ | High | 🚧 | Factory for POROs in specs ❗❗❗ | Start with `result_class`, `class self::Result`, `service_class`, `step_class`, `organizer_class` |
36
36
  | High | 🚧 | Resolve warning during specs | |
37
37
  | Medium | 🚧 | Consider to change/rewrite `delegate` backend to minify its interface | |
38
38
  | Medium | 🚧 | Same order of attr macros, delegators, initialize, class methods, attr methods, queries, actions, `to_*`, comparison, inspect | |
@@ -64,5 +64,16 @@
64
64
  | Medium | 🚧 | Add `shoulda-context` to test conditionals when `RSpec` is not loaded | [shoulda-context](https://github.com/thoughtbot/shoulda-context) |
65
65
  | High | 🚧 | Abstract factory for `CastMethod` | |
66
66
  | Medium | 🚧 | `delegate_to` - option to specify of how to compare blocks | |
67
+ | High | 🚧 | Add specs for [Reassignment::Commands::DefineMethodInContainer](https://github.com/marian13/convenient_service/blob/v0.6.0/lib/convenient_service/service/plugins/has_result_steps/entities/method/entities/callers/reassignment/commands/define_method_in_container.rb#L13) | |
68
+ | High | 🚧 | Example for dependency containers | |
69
+ | Low | 🚧 | `delegate_to.and_return_value` | |
70
+ | Medium | 🚧 | Support two Cache implementations, array-based and hash-based | |
71
+ | Low | 🚧 | `delegate_to_service` | |
72
+ | High | 🚧 | Intentionally disable plugins to document dependencies | |
73
+ | Low | 🚧 | `.and_return_value_should be_instance_of(expected)` | |
74
+ | Low | 🚧 | `ConvenientService::Config.delegate_to_diff_argorithm = :diffy` | |
75
+ | Low | 🚧 | Rubocop cop that complains when a service name does NOT start with a verb | |
76
+ | Low | 🚧 | Generator to create a spec default structure | |
77
+ | Low | 🚧 | Rubocop cop that complains when a service does NOT have its own suite of specs | |
67
78
 
68
79
  Search for `TODO`s in the codebase for more tasks.
@@ -33,6 +33,7 @@ Gem::Specification.new do |spec|
33
33
  spec.add_development_dependency "awesome_print"
34
34
  spec.add_development_dependency "byebug", "~> 10.0"
35
35
  spec.add_development_dependency "commonmarker"
36
+ spec.add_development_dependency "faker"
36
37
  spec.add_development_dependency "gem-release"
37
38
  spec.add_development_dependency "inch"
38
39
  spec.add_development_dependency "json"
@@ -49,6 +50,6 @@ Gem::Specification.new do |spec|
49
50
  spec.add_development_dependency "simplecov"
50
51
  spec.add_development_dependency "simplecov-lcov"
51
52
  spec.add_development_dependency "webrick"
52
- spec.add_development_dependency "yard"
53
+ spec.add_development_dependency "yard", "~> 0.9.28"
53
54
  spec.add_development_dependency "yard-junk"
54
55
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ConvenientService
4
+ Command = ::ConvenientService::Support::Command
4
5
  Concern = ::ConvenientService::Support::Concern
5
6
  DependencyContainer = ::ConvenientService::Support::DependencyContainer
6
7
  end
@@ -5,7 +5,11 @@ module ConvenientService
5
5
  module Plugins
6
6
  module HasAroundCallbacks
7
7
  class Middleware < Core::MethodChainMiddleware
8
- def next(...)
8
+ include Support::DependencyContainer::Import
9
+
10
+ import :"entities.Callback", from: Common::Plugins::HasCallbacks::Container
11
+
12
+ def next(*args, **kwargs, &block)
9
13
  ##
10
14
  # A variable that stores return value of middleware `chain.next` aka `original_value`.
11
15
  # It is reassigned later by the `initial_around_callback`.
@@ -15,14 +19,30 @@ module ConvenientService
15
19
  ##
16
20
  # A list of around callbacks.
17
21
  #
22
+ # class Service
23
+ # around do |chain|
24
+ # # part before `chain.yield`
25
+ # original_value = chain.yield
26
+ # # part after `chain.yield`
27
+ # end
28
+ # end
29
+ #
30
+ # class Service
31
+ # around do |chain, arguments|
32
+ # # part before `chain.yield`
33
+ # original_value = chain.yield
34
+ # # part after `chain.yield`
35
+ # end
36
+ # end
37
+ #
18
38
  around_callbacks = entity.callbacks.for([:around, method])
19
39
 
20
40
  ##
21
41
  #
22
42
  #
23
- initial_around_callback = Plugins::HasCallbacks::Entities::Callback.new(
43
+ initial_around_callback = entities.Callback.new(
24
44
  types: [:around, method],
25
- block: proc { original_value = chain.next(...) }
45
+ block: proc { original_value = chain.next(*args, **kwargs, &block) }
26
46
  )
27
47
 
28
48
  ##
@@ -76,7 +96,7 @@ module ConvenientService
76
96
  # rubocop:disable Style/Semicolon
77
97
  composed =
78
98
  around_callbacks.reverse.reduce(initial_around_callback) do |composed, callback|
79
- proc { callback.call_in_context(entity, composed); original_value }
99
+ proc { callback.call_in_context_with_value_and_arguments(entity, composed, *args, **kwargs, &block); original_value }
80
100
  end
81
101
  # rubocop:enable Style/Semicolon
82
102
 
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConvenientService
4
+ module Common
5
+ module Plugins
6
+ module HasCallbacks
7
+ module Container
8
+ include Support::DependencyContainer::Export
9
+
10
+ export :"entities.Callback" do
11
+ Entities::Callback
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -6,33 +6,99 @@ module ConvenientService
6
6
  module HasCallbacks
7
7
  module Entities
8
8
  class Callback
9
- attr_reader :types, :block
9
+ ##
10
+ # @!attribute [r] types
11
+ # @return [ConvenientService::Common::Plugins::HasCallbacks::Entities::TypeCollection]
12
+ #
13
+ attr_reader :types
10
14
 
15
+ ##
16
+ # @!attribute [r] block
17
+ # @return [Proc]
18
+ #
19
+ attr_reader :block
20
+
21
+ ##
22
+ # @param types [ConvenientService::Common::Plugins::HasCallbacks::Entities::TypeCollection]
23
+ # @param block [Proc]
24
+ # @return [void]
25
+ #
11
26
  def initialize(types:, block:)
12
27
  @types = Entities::TypeCollection.new(types: types)
13
28
  @block = block
14
29
  end
15
30
 
31
+ ##
32
+ # @return [Boolean]
33
+ #
16
34
  def called?
17
35
  Utils::Bool.to_bool(@called)
18
36
  end
19
37
 
38
+ ##
39
+ # @return [Boolean]
40
+ #
20
41
  def not_called?
21
42
  !called?
22
43
  end
23
44
 
24
- def call(*args, **kwargs)
25
- block.call(*args, **kwargs).tap { mark_as_called }
45
+ ##
46
+ # @return [Object] Can be any type.
47
+ #
48
+ def call(...)
49
+ call_callback(...)
26
50
  end
27
51
 
52
+ ##
53
+ # @return [Object] Can be any type.
54
+ #
28
55
  alias_method :yield, :call
56
+
57
+ ##
58
+ # @return [Object] Can be any type.
59
+ #
29
60
  alias_method :[], :call
61
+
62
+ ##
63
+ # @return [Object] Can be any type.
64
+ #
30
65
  alias_method :===, :call
31
66
 
32
- def call_in_context(context, *args, **kwargs)
33
- context.instance_exec(*args, **kwargs, &block).tap { mark_as_called }
67
+ ##
68
+ # @param context [Object] Can be any type.
69
+ # @return [ConvenientService::Common::Plugins::HasCallbacks::Entities::Callback]
70
+ #
71
+ def call_in_context(context)
72
+ call_callback_in_context(context)
73
+ end
74
+
75
+ ##
76
+ # @param context [Object] Can be any type.
77
+ # @param args [Array]
78
+ # @param kwargs [Hash]
79
+ # @param block [Proc]
80
+ # @return [ConvenientService::Common::Plugins::HasCallbacks::Entities::Callback]
81
+ #
82
+ def call_in_context_with_arguments(context, *args, **kwargs, &block)
83
+ call_callback_in_context(context, arguments(*args, **kwargs, &block))
84
+ end
85
+
86
+ ##
87
+ # @param context [Object] Can be any type.
88
+ # @param value [Object] Can be any type.
89
+ # @param args [Array]
90
+ # @param kwargs [Hash]
91
+ # @param block [Proc]
92
+ # @return [ConvenientService::Common::Plugins::HasCallbacks::Entities::Callback]
93
+ #
94
+ def call_in_context_with_value_and_arguments(context, value, *args, **kwargs, &block)
95
+ call_callback_in_context(context, value, arguments(*args, **kwargs, &block))
34
96
  end
35
97
 
98
+ ##
99
+ # @param other [Object] Can be any type.
100
+ # @return [Boolead]
101
+ #
36
102
  def ==(other)
37
103
  return unless other.instance_of?(self.class)
38
104
 
@@ -42,15 +108,42 @@ module ConvenientService
42
108
  true
43
109
  end
44
110
 
111
+ ##
112
+ # @return [Proc]
113
+ #
45
114
  def to_proc
46
115
  block
47
116
  end
48
117
 
49
118
  private
50
119
 
120
+ ##
121
+ # @return [Boolean]
122
+ #
51
123
  def mark_as_called
52
124
  @called = true
53
125
  end
126
+
127
+ ##
128
+ # @return [ConvenientService::Support::Arguments]
129
+ #
130
+ def arguments(...)
131
+ Support::Arguments.new(...)
132
+ end
133
+
134
+ ##
135
+ # @return [Object] Can be any type.
136
+ #
137
+ def call_callback(...)
138
+ block.call(...).tap { mark_as_called }
139
+ end
140
+
141
+ ##
142
+ # @return [Object] Can be any type.
143
+ #
144
+ def call_callback_in_context(context, *args)
145
+ context.instance_exec(*args, &block).tap { mark_as_called }
146
+ end
54
147
  end
55
148
  end
56
149
  end
@@ -5,12 +5,33 @@ module ConvenientService
5
5
  module Plugins
6
6
  module HasCallbacks
7
7
  class Middleware < Core::MethodChainMiddleware
8
- def next(...)
9
- entity.callbacks.for([:before, method]).each { |callback| callback.call_in_context(entity) }
8
+ def next(*args, **kwargs, &block)
9
+ ##
10
+ # class Service
11
+ # before :result do
12
+ # end
13
+ #
14
+ # before :result do |arguments|
15
+ # end
16
+ # end
17
+ #
18
+ entity.callbacks.for([:before, method]).each { |callback| callback.call_in_context_with_arguments(entity, *args, **kwargs, &block) }
10
19
 
11
- original_value = chain.next(...)
20
+ original_value = chain.next(*args, **kwargs, &block)
12
21
 
13
- entity.callbacks.for([:after, method]).reverse_each { |callback| callback.call_in_context(entity, original_value) }
22
+ ##
23
+ # class Service
24
+ # after :result do
25
+ # end
26
+ #
27
+ # after :result do |result|
28
+ # end
29
+ #
30
+ # after :result do |result, arguments|
31
+ # end
32
+ # end
33
+ #
34
+ entity.callbacks.for([:after, method]).reverse_each { |callback| callback.call_in_context_with_value_and_arguments(entity, original_value, *args, **kwargs, &block) }
14
35
 
15
36
  original_value
16
37
  end
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "has_callbacks/concern"
4
+ require_relative "has_callbacks/container"
4
5
  require_relative "has_callbacks/entities"
5
6
  require_relative "has_callbacks/middleware"
@@ -8,8 +8,8 @@ require_relative "plugins/normalizes_env"
8
8
  require_relative "plugins/caches_constructor_params"
9
9
  require_relative "plugins/caches_return_value"
10
10
  require_relative "plugins/can_be_copied"
11
- require_relative "plugins/has_around_callbacks"
12
11
  require_relative "plugins/has_callbacks"
12
+ require_relative "plugins/has_around_callbacks"
13
13
  require_relative "plugins/has_constructor"
14
14
  require_relative "plugins/has_constructor_without_initialize"
15
15
  require_relative "plugins/has_internals"
@@ -34,12 +34,6 @@ module ConvenientService
34
34
  use Plugins::Common::HasAroundCallbacks::Concern
35
35
 
36
36
  use Plugins::Service::HasInspect::Concern
37
- ##
38
- # NOTE: Optional plugins.
39
- # TODO: Specs.
40
- #
41
- # use Plugins::Common::HasConfig::Concern
42
- #
43
37
  end
44
38
 
45
39
  middlewares :initialize do
@@ -51,18 +45,24 @@ module ConvenientService
51
45
  middlewares :result do
52
46
  use Plugins::Common::NormalizesEnv::Middleware
53
47
 
54
- use Plugins::Service::HasResult::Middleware
55
-
56
- use Plugins::Service::HasResultSteps::Middleware
57
-
58
48
  use Plugins::Common::HasCallbacks::Middleware
59
49
  use Plugins::Common::HasAroundCallbacks::Middleware
60
50
 
51
+ use Plugins::Service::HasResult::Middleware
52
+ use Plugins::Service::HasResultSteps::Middleware
53
+
61
54
  use Plugins::Service::RaisesOnDoubleResult::Middleware
62
55
 
63
56
  use Plugins::Common::CachesReturnValue::Middleware
64
57
  end
65
58
 
59
+ middlewares :step do
60
+ use Plugins::Common::NormalizesEnv::Middleware
61
+
62
+ use Plugins::Common::HasCallbacks::Middleware
63
+ use Plugins::Common::HasAroundCallbacks::Middleware
64
+ end
65
+
66
66
  middlewares :success do
67
67
  use Plugins::Common::NormalizesEnv::Middleware
68
68
 
@@ -111,6 +111,9 @@ module ConvenientService
111
111
  use Plugins::Result::HasResultShortSyntax::Concern
112
112
  use Plugins::Result::CanRecalculateResult::Concern
113
113
 
114
+ use Plugins::Result::HasStep::Concern
115
+ use Plugins::Result::CanHaveParentResult::Concern
116
+
114
117
  use Plugins::Result::HasInspect::Concern
115
118
  end
116
119
 
@@ -118,6 +121,9 @@ module ConvenientService
118
121
  use Plugins::Common::NormalizesEnv::Middleware
119
122
 
120
123
  use Plugins::Result::HasJsendStatusAndAttributes::Middleware
124
+
125
+ use Plugins::Result::HasStep::Initialize::Middleware
126
+ use Plugins::Result::CanHaveParentResult::Initialize::Middleware
121
127
  end
122
128
 
123
129
  middlewares :success? do
@@ -174,6 +180,13 @@ module ConvenientService
174
180
  use Plugins::Result::RaisesOnNotCheckedResultStatus::Middleware
175
181
  end
176
182
 
183
+ middlewares :to_kwargs do
184
+ use Plugins::Common::NormalizesEnv::Middleware
185
+
186
+ use Plugins::Result::HasStep::ToKwargs::Middleware
187
+ use Plugins::Result::CanHaveParentResult::ToKwargs::Middleware
188
+ end
189
+
177
190
  class self::Internals
178
191
  include Core
179
192
 
@@ -192,10 +205,10 @@ module ConvenientService
192
205
  use Plugins::Step::HasInspect::Concern
193
206
  end
194
207
 
195
- middlewares :result do
208
+ middlewares :calculate_result do
196
209
  use Plugins::Common::NormalizesEnv::Middleware
197
210
 
198
- use Plugins::Common::CachesReturnValue::Middleware
211
+ use Plugins::Step::CanHaveParentResult::Middleware
199
212
  end
200
213
 
201
214
  class self::Internals
@@ -106,6 +106,18 @@ module ConvenientService
106
106
  require_relative "rspec"
107
107
  end
108
108
 
109
+ ##
110
+ # Loads test factory.
111
+ #
112
+ # @return [Boolean]
113
+ # @note Expected to be called from `spec_helper.rb`.
114
+ #
115
+ def require_factory
116
+ require "faker"
117
+
118
+ require_relative "factory"
119
+ end
120
+
109
121
  ##
110
122
  # Loads standard config examples.
111
123
  #
@@ -34,7 +34,7 @@ module ConvenientService
34
34
 
35
35
  middlewares :result do
36
36
  insert_before \
37
- Plugins::Service::HasResultSteps::Middleware,
37
+ Plugins::Service::HasResult::Middleware,
38
38
  Plugins::Service::HasResultParamsValidations::UsingDryValidation::Middleware
39
39
  end
40
40
  end
@@ -45,7 +45,7 @@ module ConvenientService
45
45
 
46
46
  middlewares :result do
47
47
  insert_before \
48
- Plugins::Service::HasResultSteps::Middleware,
48
+ Plugins::Service::HasResult::Middleware,
49
49
  Plugins::Service::HasResultParamsValidations::UsingActiveModelValidations::Middleware
50
50
  end
51
51
  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