light-service 0.13.0 → 0.14.0

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: 1e9dfb5cf61024afa28719b84e160c9580fa39c99eda19b73cdb0cced9017da2
4
- data.tar.gz: 7f389c208b3b06000c84859d595d9b49acf8dfe8d370736ee564a1ae8ef8b5d1
3
+ metadata.gz: 10ae707b6938f2898615668928728eb8e562fb9ed2f0ecf17e50deff76226a44
4
+ data.tar.gz: c870f719d663dd1aec96d895196f57b2caaae1cb9e61d073bd5fcef7ce33cff5
5
5
  SHA512:
6
- metadata.gz: b1b7792d344efe1aab4297f8774649590b8e0336a44082da31a167a62b75a039b7f89759923beb9a463f6c017b0387297efc815f357deae31270b54e2c1c1bcc
7
- data.tar.gz: 0d1d339a23d2d7b39bd5403d81fe1e1519a89eec22fd64f00931d9dc49a81c5476f67e6dd28591e0e88c55b1378d1155ed0d1700100569aa9b43a1922c860f3e
6
+ metadata.gz: 3062b1c4d10519f2f60c527ecedee3647d13c85419e5ad38db7ec7046b0cba68b122d044a1045724bc034c64d6e3b4afc3482cfa9bb8a87ee77ef5f847e0e728
7
+ data.tar.gz: c240aec3f4244f7ebb17f06c98e21c03495c1718d34dafcd862209fc95323bde27db6e9144e38ad285d0b83b2182713d5d1690dc43f28c95b591dd9d56aec7b7
data/README.md CHANGED
@@ -624,7 +624,28 @@ Using the `rolled_back` macro is optional for the actions in the chain. You shou
624
624
 
625
625
  The actions are rolled back in reversed order from the point of failure starting with the action that triggered it.
626
626
 
627
- See [this](spec/acceptance/rollback_spec.rb) acceptance test to learn more about this functionality.
627
+ See [this acceptance test](spec/acceptance/rollback_spec.rb) to learn more about this functionality.
628
+
629
+ You may find yourself directly using an action that can roll back by calling `.execute` instead of using it from within an Organizer.
630
+ If this action fails and attempts a rollback, a `FailWithRollbackError` exception will be raised. This is so that the organizer can
631
+ rollback the actions one by one. If you don't want to wrap your call to the action with a `begin, rescue FailWithRollbackError`
632
+ block, you can introspect the context like so, and keep your usage of the action clean:
633
+
634
+ ```ruby
635
+ class FooAction
636
+ extend LightService::Action
637
+
638
+ executed do |context|
639
+ # context.organized_by will be nil if run from an action,
640
+ # or will be the class name if run from an organizer
641
+ if context.organized_by.nil?
642
+ context.fail!
643
+ else
644
+ context.fail_with_rollback!
645
+ end
646
+ end
647
+ end
648
+ ```
628
649
 
629
650
  ## Localizing Messages
630
651
  By default LightService provides a mechanism for easily translating your error or success messages via I18n. You can also provide your own custom localization adapter if your application's logic is more complex than what is shown here.
@@ -1,8 +1,10 @@
1
1
  A brief list of new features and changes introduced with the specified version.
2
2
 
3
+ ### 0.14.0
4
+ * [Add 'organized_by' to context](https://github.com/adomokos/light-service/pull/192) - Context now have an #organized_by attribute
5
+
3
6
  ### 0.13.0
4
- * [Add 'add_to_context' and 'add_aliases'](https://github.com/adomokos/light-service/pull/172)
5
- * Updating Ruby compatibility, minor fixes
7
+ * [Add 'add_to_context' and 'add_aliases'](https://github.com/adomokos/light-service/pull/172) - Updating Ruby compatibility, minor fixes
6
8
 
7
9
  ### 0.12.0
8
10
  * [Per organizer logger](https://github.com/adomokos/light-service/pull/162)
@@ -8,7 +8,7 @@ module LightService
8
8
 
9
9
  # rubocop:disable ClassLength
10
10
  class Context < Hash
11
- attr_accessor :message, :error_code, :current_action
11
+ attr_accessor :message, :error_code, :current_action, :organized_by
12
12
 
13
13
  def initialize(context = {},
14
14
  outcome = Outcomes::SUCCESS,
@@ -18,6 +18,7 @@ module LightService
18
18
  @message = message
19
19
  @error_code = error_code
20
20
  @skip_remaining = false
21
+
21
22
  context.to_hash.each { |k, v| self[k] = v }
22
23
  end
23
24
 
@@ -1,10 +1,16 @@
1
1
  module LightService
2
2
  module Organizer
3
3
  class WithReducer
4
- attr_reader :context
4
+ attr_reader :context
5
+ attr_accessor :organizer
6
+
7
+ def initialize(monitored_organizer = nil)
8
+ @organizer = monitored_organizer
9
+ end
5
10
 
6
11
  def with(data = {})
7
12
  @context = LightService::Context.make(data)
13
+ @context.organized_by = organizer
8
14
  self
9
15
  end
10
16
 
@@ -4,7 +4,7 @@ module LightService
4
4
  def self.make(monitored_organizer)
5
5
  logger = monitored_organizer.logger ||
6
6
  LightService::Configuration.logger
7
- decorated = WithReducer.new
7
+ decorated = WithReducer.new(monitored_organizer)
8
8
 
9
9
  return decorated if logger.nil?
10
10
 
@@ -8,6 +8,9 @@ module LightService
8
8
  def initialize(organizer, decorated: WithReducer.new, logger:)
9
9
  @decorated = decorated
10
10
  @organizer = organizer
11
+
12
+ decorated.organizer = organizer
13
+
11
14
  @logger = logger
12
15
  @logged = false
13
16
  end
@@ -1,3 +1,3 @@
1
1
  module LightService
2
- VERSION = "0.13.0".freeze
2
+ VERSION = "0.14.0".freeze
3
3
  end
@@ -20,6 +20,13 @@ RSpec.describe LightService::Organizer do
20
20
  expect(result.number).to eq(5)
21
21
  end
22
22
 
23
+ it "knows that it's being iterated from within an organizer" do
24
+ result = TestDoubles::TestIterate.call(:number => 1,
25
+ :counters => [1, 2, 3, 4])
26
+
27
+ expect(result.organized_by).to eq TestDoubles::TestIterate
28
+ end
29
+
23
30
  it 'will not iterate over a failed context' do
24
31
  empty_context.fail!('Something bad happened')
25
32
 
@@ -49,6 +49,12 @@ RSpec.describe LightService::Organizer do
49
49
  expect(result).to be_success
50
50
  end
51
51
 
52
+ it "knows that it's being conditionally reduced from within an organizer" do
53
+ result = TestReduceIf.call(:number => 2)
54
+
55
+ expect(result.organized_by).to eq TestReduceIf
56
+ end
57
+
52
58
  it 'skips actions within in its own scope' do
53
59
  org = Class.new do
54
60
  extend LightService::Organizer
@@ -40,4 +40,10 @@ RSpec.describe LightService::Organizer do
40
40
  result = TestReduceUntil.call(empty_context)
41
41
  expect(result).to be_success
42
42
  end
43
+
44
+ it "is expected to know its organizer when reducing until a condition" do
45
+ result = TestReduceUntil.call(:number => 1)
46
+
47
+ expect(result.organized_by).to eq TestReduceUntil
48
+ end
43
49
  end
@@ -68,6 +68,14 @@ describe LightService::Action do
68
68
  expect(result.to_hash).to eq(:number => 2)
69
69
  end
70
70
 
71
+ context "when called directly" do
72
+ it "is expected to not be organized" do
73
+ result = TestDoubles::AddsTwoActionWithFetch.execute(context)
74
+
75
+ expect(result.organized_by).to be_nil
76
+ end
77
+ end
78
+
71
79
  context "when invoked with hash" do
72
80
  it "creates LightService::Context implicitly" do
73
81
  ctx = { :some_key => "some value" }
@@ -19,6 +19,11 @@ describe LightService::Organizer do
19
19
  result = TestDoubles::AnOrganizer.call(:user => user)
20
20
  expect(result).to eq(ctx)
21
21
  end
22
+
23
+ it "sets itself as the organizer" do
24
+ result = TestDoubles::AnOrganizer.call(:user => user)
25
+ expect(result.organized_by).to eq TestDoubles::AnOrganizer
26
+ end
22
27
  end
23
28
 
24
29
  context "when #with is called with Context" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: light-service
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.13.0
4
+ version: 0.14.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Attila Domokos
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-29 00:00:00.000000000 Z
11
+ date: 2020-06-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport