startback 0.9.1 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c2a20858c9d0287af3cfaa8e60c6f59630f1d258de469f7c50ffe1e6529742ca
4
- data.tar.gz: c5d3fb83ab9a7aa0b1bb6fbec2f0bfeef40b4c51c15c602d9a2bca85b17e8d26
3
+ metadata.gz: 10937b9e7877dd7393837c70185fe245493a35234d66a899efee5fbebb539b75
4
+ data.tar.gz: 5fd7234b34b09f875dcf9b21ad819eb5072ffed902ddd08ca6c2e6a68e3ec1df
5
5
  SHA512:
6
- metadata.gz: ee763fb9d8be8ca1f6cc8a541b70df6b72ae982e2026b04286ec5b6a208aba31641ea8062330f7efc8d01e548bfbca408edb115678dbc6fefa707e3d98fc8f0e
7
- data.tar.gz: ec3607dec57ff640d4b49afb147a5c6893e45844e5d53cf07e8ca26ea709341260c02f01a25bad7fc68f231e859986ce2bb0604de7e6031f59f97d1f0a06a464
6
+ metadata.gz: 37df032165dd22b0680f8eaf874bada4810707253e06677c6a80b5d21afc87062643143e5f2b50e0f3adbe48d9df00368bb5f7070068e8458565e0d0fb8f1112
7
+ data.tar.gz: 754c4703f78bca862a4a7b2f8d973609ec44fab70368fe6d6d371050c140592a5706720ef0208ac4002ce434d85ad8e4d095d07840cd7993e2cb698cd116da5d
@@ -74,13 +74,13 @@ module Startback
74
74
 
75
75
  protected
76
76
 
77
- def op_to_trail(op, time, ex = nil)
77
+ def op_to_trail(op, time = nil, ex = nil)
78
78
  log_msg = {
79
79
  op_took: time ? time.round(8) : nil,
80
80
  op: op_name(op),
81
81
  context: op_context(op),
82
82
  op_data: op_data(op)
83
- }
83
+ }.compact
84
84
  log_msg[:error] = ex if ex
85
85
  log_msg
86
86
  end
@@ -104,6 +104,8 @@ module Startback
104
104
  op.input
105
105
  elsif op.respond_to?(:request, false)
106
106
  op.request
107
+ elsif op.is_a?(Operation::MultiOperation)
108
+ op.ops.map{ |sub_op| op_to_trail(sub_op) }
107
109
  end
108
110
  sanitize(data)
109
111
  end
@@ -1,6 +1,6 @@
1
1
  module Startback
2
2
  class Operation
3
- class MultiOperation
3
+ class MultiOperation < Operation
4
4
 
5
5
  def initialize(ops = [])
6
6
  @ops = ops
@@ -29,6 +29,7 @@ module Startback
29
29
  # end
30
30
  #
31
31
  class Operation
32
+ extend Support::TransactionPolicy
32
33
  include Errors
33
34
  include Support::OperationRunner
34
35
  include Support::Hooks.new(:call)
@@ -0,0 +1,25 @@
1
+ module Startback
2
+ module Support
3
+ class TransactionManager
4
+
5
+ def initialize(db, method = :transaction)
6
+ @db = db
7
+ @method = method
8
+ end
9
+
10
+ def call(runner, op, &then_block)
11
+ raise ArgumentError, "A block is required" unless then_block
12
+
13
+ before = (op.class.transaction_policy == :before_call)
14
+ if before
15
+ @db.send(@method) do
16
+ then_block.call
17
+ end
18
+ else
19
+ then_block.call
20
+ end
21
+ end
22
+
23
+ end # class TransactionManager
24
+ end # module Support
25
+ end # module Startback
@@ -0,0 +1,33 @@
1
+ module Startback
2
+ module Support
3
+ module TransactionPolicy
4
+
5
+ # Returns the operation's transaction policy
6
+ def transaction_policy
7
+ @transaction_policy || :before_call
8
+ end
9
+
10
+ # Sets the transaction policy to use. Valid values are:
11
+ # - before_call : the transaction is started by the operation
12
+ # runner, right before calling the #call method on operation
13
+ # instance
14
+ # - within_call: the transaction is started by the operation
15
+ # itself, as part of its internal logic.
16
+ def transaction_policy=(policy)
17
+ unless [:before_call, :within_call].include?(policy)
18
+ raise ArgumentError, "Unknown policy `#{policy}`"
19
+ end
20
+ @transaction_policy = policy
21
+ end
22
+
23
+ def after_commit(&bl)
24
+ after_call do
25
+ db.after_commit do
26
+ instance_exec(&bl)
27
+ end
28
+ end
29
+ end
30
+
31
+ end # module TransactionPolicy
32
+ end # module Support
33
+ end # module Startback
@@ -19,3 +19,5 @@ require_relative 'support/logger'
19
19
  require_relative 'support/robustness'
20
20
  require_relative 'support/hooks'
21
21
  require_relative 'support/operation_runner'
22
+ require_relative 'support/transaction_policy'
23
+ require_relative 'support/transaction_manager'
@@ -1,8 +1,8 @@
1
1
  module Startback
2
2
  module Version
3
3
  MAJOR = 0
4
- MINOR = 9
5
- TINY = 1
4
+ MINOR = 10
5
+ TINY = 0
6
6
  end
7
7
  VERSION = "#{Version::MAJOR}.#{Version::MINOR}.#{Version::TINY}"
8
8
  end
@@ -68,7 +68,7 @@ module Startback
68
68
  end
69
69
 
70
70
  context 'the around feature with a class' do
71
- class TransactionManager
71
+ class SomeTransactionManager
72
72
  include Singleton
73
73
 
74
74
  def initialize
@@ -87,7 +87,7 @@ module Startback
87
87
 
88
88
  class RunnerTest3
89
89
  include OperationRunner
90
- around_run TransactionManager.instance
90
+ around_run SomeTransactionManager.instance
91
91
 
92
92
  def operation_world(op)
93
93
  { hello: "world" }
@@ -97,7 +97,7 @@ module Startback
97
97
  it 'calls the proc with expected parameters' do
98
98
  test = RunnerTest3.new
99
99
  got = test.run(op)
100
- expect(TransactionManager.instance.called).to eql(true)
100
+ expect(SomeTransactionManager.instance.called).to eql(true)
101
101
  expect(got).to eql({
102
102
  seen_hello: "world"
103
103
  })
@@ -154,4 +154,3 @@ module Startback
154
154
  end # module OperationRunner
155
155
  end # module Support
156
156
  end # module Startback
157
-
@@ -0,0 +1,64 @@
1
+ require 'spec_helper'
2
+ module Startback
3
+ module Support
4
+ describe TransactionManager do
5
+ subject do
6
+ TransactionManager.new(db).call(nil, op) do
7
+ op.call
8
+ end
9
+ end
10
+
11
+ class FakeDatabase
12
+ def initialize
13
+ @called = false
14
+ end
15
+ attr_reader :called
16
+
17
+ def transaction
18
+ @called = true
19
+ yield
20
+ end
21
+ end
22
+
23
+ let(:db) do
24
+ FakeDatabase.new
25
+ end
26
+
27
+ context 'when called with a default operation' do
28
+ class OperationNotManagingTransactions < Startback::Operation
29
+ def call
30
+ 12
31
+ end
32
+ end
33
+
34
+ let(:op) do
35
+ OperationNotManagingTransactions.new
36
+ end
37
+
38
+ it 'calls db.transaction' do
39
+ expect(subject).to eql(12)
40
+ expect(db.called).to eql(true)
41
+ end
42
+ end
43
+
44
+ context 'when called with an operation that manages the transactions itself' do
45
+ class OperationManagingTransactions < Startback::Operation
46
+ self.transaction_policy = :within_call
47
+
48
+ def call
49
+ 12
50
+ end
51
+ end
52
+
53
+ let(:op) do
54
+ OperationManagingTransactions.new
55
+ end
56
+
57
+ it 'calls db.transaction' do
58
+ expect(subject).to eql(12)
59
+ expect(db.called).to eql(false)
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: startback
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.1
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bernard Lambeau
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-12-13 00:00:00.000000000 Z
11
+ date: 2021-12-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -380,7 +380,6 @@ extra_rdoc_files: []
380
380
  files:
381
381
  - README.md
382
382
  - Rakefile
383
- - VERSION
384
383
  - lib/startback.rb
385
384
  - lib/startback/audit.rb
386
385
  - lib/startback/audit/prometheus.rb
@@ -411,6 +410,8 @@ files:
411
410
  - lib/startback/support/logger.rb
412
411
  - lib/startback/support/operation_runner.rb
413
412
  - lib/startback/support/robustness.rb
413
+ - lib/startback/support/transaction_manager.rb
414
+ - lib/startback/support/transaction_policy.rb
414
415
  - lib/startback/version.rb
415
416
  - lib/startback/web/api.rb
416
417
  - lib/startback/web/auto_caching.rb
@@ -438,6 +439,7 @@ files:
438
439
  - spec/unit/support/operation_runner/test_around_run.rb
439
440
  - spec/unit/support/operation_runner/test_before_after_call.rb
440
441
  - spec/unit/support/test_robusteness.rb
442
+ - spec/unit/support/test_transaction_manager.rb
441
443
  - spec/unit/test_event.rb
442
444
  - spec/unit/test_operation.rb
443
445
  - spec/unit/test_support.rb
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.9.1