light-service-ext 0.1.9 → 0.1.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 587a60d79dffd34dfbdbdbfc28e024c3a2983310209a8169e555e9ae88c8592b
4
- data.tar.gz: 817889b86ce56ae818070d9bf938a34c8efca969729a51a8c4d37d738d20e821
3
+ metadata.gz: aad64b8f2360c7b9e4576bcfe6e167f10a87424392b9677ca620b973ba3dc8ef
4
+ data.tar.gz: 18a7ff6bde7fab9b58eb1e883fbb79cc346be9d98e9516135de3b5e6eabd570c
5
5
  SHA512:
6
- metadata.gz: 538351e6b9c9050dcc81f91487c4fcd81e48e822589b5b3a77fa39909b733964d383241bd236c9ac199a32b018b8f9c6edf11efa5ef37d4051f1d0d0212810bd
7
- data.tar.gz: 8ebd87e41ceb3bb2db3982289ded3281fd0c0234a33f255c95f9ff926b0a1a62231ade022b9c251246b1f1af6ee4ff4e0e5afe9b7e452ad8c8f41a3489022dbd
6
+ metadata.gz: c0f1d41f1530c3b8e9624f0c3ef360464b9a67d8c7e21893b4ecea3db8b09d19b967e7a0dbbc6d66cc1e90677186954c62ab1d734e026ca434b183d7ad3bbdc8
7
+ data.tar.gz: d81ae82181b451ad2330dc42ccbf33f11cab6b99a0afaa47ba77d4a8bf88bcb6903b486439be926ea408fc2dc06b8e27626843f45a69e6053c9afb4d3dd4f401
data/README.md CHANGED
@@ -173,12 +173,14 @@ end
173
173
  - `LightServiceExt::Status::INCOMPLETE`
174
174
  - `:last_failed_context` ~ copy of context that failed e.g. with `errors` field present
175
175
  - `internal_only` ~ includes the likes of raised error summary and should never be passed to endpoint responses
176
+ - `meta` ~ used to store any additional information that could be helpful especially for debugging purposes.
176
177
  Example
177
178
 
178
179
  ````ruby
179
180
  input = { order: order }
180
181
  overrides = {} # optionally override `params`, `errors` and `allow_raise_on_failure`
181
- LightServiceExt::ApplicationContext.make_with_defaults(input, overrides)
182
+ meta = { current_user_id: 12345, request_id: some-unique-request-id, impersonator_id: 54321 }
183
+ LightServiceExt::ApplicationContext.make_with_defaults(input, overrides, meta: meta)
182
184
 
183
185
  # => { input: { order: order },
184
186
  # errors: { email: ['not found'] },
@@ -190,7 +192,8 @@ LightServiceExt::ApplicationContext.make_with_defaults(input, overrides)
190
192
  # api_responses: [ { user_id: 1, status: 'ACTIVE' } ],
191
193
  # last_failed_context: {input: { order: order }, params: {}, ...},
192
194
  # allow_raise_on_failure: true,
193
- # internal_only: { error_info: ErrorInfoInstance }
195
+ # internal_only: { error_info: ErrorInfoInstance },
196
+ # meta: { current_user_id: 12345, request_id: some-unique-request-id, impersonator_id: 54321 }
194
197
  # }
195
198
  ````
196
199
 
@@ -25,7 +25,8 @@ module LightServiceExt
25
25
  api_responses: [],
26
26
  last_failed_context: nil,
27
27
  allow_raise_on_failure: LightServiceExt.config.allow_raise_on_failure?,
28
- internal_only: {}
28
+ internal_only: {},
29
+ meta: {}
29
30
  }.freeze
30
31
  end
31
32
  end
@@ -63,6 +64,11 @@ module LightServiceExt
63
64
  add_attrs_to_ctx(:internal_only, **attrs)
64
65
  end
65
66
 
67
+ def add_meta(**attrs)
68
+ add_attrs_to_ctx(:meta, **attrs)
69
+ end
70
+
71
+
66
72
  def record_raised_error(error)
67
73
  @error_info = ErrorInfo.new(error)
68
74
  error_type = @error_info.type
@@ -6,14 +6,37 @@ module LightServiceExt
6
6
 
7
7
  def self.call(context)
8
8
  with_error_handler(ctx: context) do
9
- result = yield
10
- return context if outcomes_complete?(ctx: context, result: result)
9
+ self.before_execute_block.call(context)
10
+
11
+ result = yield || context
12
+
13
+ self.after_execute_block.call(context)
14
+ self.after_success_block.call(context) if result.success?
15
+ self.after_failure_block.call(context) if result.failure?
11
16
 
17
+ return context if outcomes_complete?(ctx: context, result: result)
12
18
  merge_api_responses!(ctx: context, result: result)
13
19
  end
14
20
  end
15
21
 
16
22
  class << self
23
+ attr_writer :before_execute_block, :after_execute_block, :after_success_block, :after_failure_block
24
+
25
+ def before_execute_block
26
+ @before_execute_block ||= ->(_context) {}
27
+ end
28
+
29
+ def after_execute_block
30
+ @after_execute_block ||= ->(_context) {}
31
+ end
32
+
33
+ def after_success_block
34
+ @after_success_block ||= ->(_context) {}
35
+ end
36
+
37
+ def after_failure_block
38
+ @after_failure_block ||= ->(_context) {}
39
+ end
17
40
  def merge_api_responses!(ctx:, result:)
18
41
  invoked_action = result.invoked_action
19
42
  return if invoked_action.nil?
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LightServiceExt
4
- VERSION = "0.1.9"
4
+ VERSION = "0.1.10"
5
5
  end
@@ -4,7 +4,7 @@
4
4
  module LightServiceExt
5
5
  module WithErrorHandler
6
6
  def with_error_handler(ctx:)
7
- @result = yield || ApplicationContext.make_with_defaults
7
+ @result = yield || ctx
8
8
  rescue StandardError => e
9
9
  ctx.record_raised_error(e)
10
10
  ctx.add_status(Status::COMPLETE)
@@ -16,7 +16,7 @@ Gem::Specification.new do |gem|
16
16
  gem.name = "light-service-ext"
17
17
  gem.require_paths = ["lib"]
18
18
  gem.version = LightServiceExt::VERSION
19
- gem.required_ruby_version = ">= 3"
19
+ gem.required_ruby_version = ">= 2.7"
20
20
 
21
21
  gem.metadata["homepage_uri"] = gem.homepage
22
22
  gem.metadata["source_code_uri"] = gem.homepage
@@ -325,6 +325,21 @@ module LightServiceExt
325
325
 
326
326
  subject(:ctx_with_defaults) { described_class.make_with_defaults(input, overrides) }
327
327
 
328
+ describe '#add_meta' do
329
+ it 'adds meta to context' do
330
+ ctx_with_defaults.add_meta(key => value)
331
+ expect(ctx_with_defaults.meta).to eql(key => value)
332
+ end
333
+ end
334
+
335
+ context 'with meta as an override' do
336
+ let(:overrides) { { meta: { key: 'some-value' } } }
337
+
338
+ it 'adds meta to context' do
339
+ expect(ctx_with_defaults.meta).to eql(key => value)
340
+ end
341
+ end
342
+
328
343
  context 'with non symbolized input keys' do
329
344
  let(:input) { { "key" => 'some-value' } }
330
345
 
@@ -19,6 +19,40 @@ module LightServiceExt
19
19
 
20
20
  subject(:called_ctx) { described_class.call(ctx, &proc) }
21
21
 
22
+ describe 'lifecycle callbacks' do
23
+ before do
24
+ allow(described_class.before_execute_block).to receive(:call)
25
+ allow(described_class.after_execute_block).to receive(:call)
26
+ allow(described_class.after_success_block).to receive(:call)
27
+ allow(described_class.after_failure_block).to receive(:call)
28
+ end
29
+
30
+ it 'calls appropriate lifecycle callbacks' do
31
+ called_ctx
32
+
33
+ expect(described_class.before_execute_block).to have_received(:call).with(kind_of(ApplicationContext))
34
+ expect(described_class.after_execute_block).to have_received(:call).with(kind_of(ApplicationContext))
35
+ expect(described_class.after_success_block).to have_received(:call).with(kind_of(ApplicationContext))
36
+ expect(described_class.after_failure_block).not_to have_received(:call)
37
+ end
38
+
39
+ context 'with failure' do
40
+ before do
41
+ allow_any_instance_of(ApplicationContext).to receive(:success?) { false }
42
+ allow_any_instance_of(ApplicationContext).to receive(:failure?) { true }
43
+ end
44
+
45
+ it 'calls appropriate lifecycle callbacks' do
46
+ called_ctx
47
+
48
+ expect(described_class.before_execute_block).to have_received(:call).with(kind_of(ApplicationContext))
49
+ expect(described_class.after_execute_block).to have_received(:call).with(kind_of(ApplicationContext))
50
+ expect(described_class.after_failure_block).to have_received(:call).with(kind_of(ApplicationContext))
51
+ expect(described_class.after_success_block).not_to have_received(:call)
52
+ end
53
+ end
54
+ end
55
+
22
56
  it 'returns expected context' do
23
57
  expect(called_ctx.success?).to be_truthy
24
58
  expect(called_ctx.successful_actions).to be_empty
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: light-service-ext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.9
4
+ version: 0.1.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Desmond O'Leary
@@ -229,7 +229,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
229
229
  requirements:
230
230
  - - ">="
231
231
  - !ruby/object:Gem::Version
232
- version: '3'
232
+ version: '2.7'
233
233
  required_rubygems_version: !ruby/object:Gem::Requirement
234
234
  requirements:
235
235
  - - ">="