simple_wallet 0.1.2 → 0.1.3

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: f6eac1eb277d4d4262cd05c2ced9a10c7f790613a97fb3385e496953cfd7764a
4
- data.tar.gz: c4147c19783452b10af6c63bd97e50c4b2c75e70032129e4e3aa5f734630d151
3
+ metadata.gz: d40dd6572b85931bc243f759cc5ff11c0a59eedc6260bbea55fb7687c28f4d39
4
+ data.tar.gz: ff5f84546d807d2e1a2548ba15f92100b051d66f9a9787246d07a924c07cff9c
5
5
  SHA512:
6
- metadata.gz: 1e6c8f15d40e3d4c9612ace9b5e14ba6aadc55dd21b2fc807b8c3326d21ef83532cbbc21edad53ed2ec1e742aaae23af3ae51fbb7ffe7fa47fa0936ca1c9fed2
7
- data.tar.gz: dacc153a8da1661f1cecc050b995740889d35359fcc1de4827d0ff28b573a8b8dd7a7bce895079ac2a8dc783558aa671eb1dcb4eedd605b92d295976d18db91e
6
+ metadata.gz: 13ae43936eba505ba853c47c996e4077b0f2c707080cfa2b89f0ce923b477408be49ec8e385e5fe7f96b81188acf400689f5e5d720ea9cd26909f2ce1014b7cc
7
+ data.tar.gz: 553dcb7358aad4469d10db3623e382cba8e21f942e4780b1b2647a42a8e5843b3f2a294da76e217f0fbd2ca28e30002af4dd5f320be9c83527f4394b401e01ef
@@ -0,0 +1,64 @@
1
+ module SimpleWallet
2
+ class TransferService < Service
3
+ validates :from, presence: true
4
+ validates :to, presence: true
5
+ validates :amount, presence: true, numericality: { only_integer: true, greater_than: 0 }
6
+
7
+ def initialize(from:, to:, amount:, note:)
8
+ @from = from
9
+ @to = to
10
+ @amount = amount
11
+ @note = note
12
+ end
13
+
14
+ # Use set_transaction_isolation_level: false to avoid getting this exception:
15
+ # ActiveRecord::TransactionIsolationError (cannot set isolation when joining a transaction)
16
+ def transfer(set_transaction_isolation_level: true)
17
+ # Checking account's balance should be wrapped in a transaction:
18
+ # Setting transaction isolation level in tests is problematic due to nested transactions :|
19
+ isolation_level = (Rails.env.test? || !set_transaction_isolation_level) ? nil : :serializable
20
+
21
+ success = ActiveRecord::Base.transaction(isolation: isolation_level) do
22
+ return false unless valid?
23
+
24
+ unless debiting_service.debit(set_transaction_isolation_level: set_transaction_isolation_level)
25
+ copy_errors_from(debiting_service)
26
+ return false
27
+ end
28
+
29
+ unless crediting_service.credit(set_transaction_isolation_level: set_transaction_isolation_level)
30
+ copy_errors_from(crediting_service)
31
+ raise ActiveRecord::Rollback
32
+ end
33
+
34
+ after_operation
35
+ true
36
+ end
37
+
38
+ !!success
39
+ end
40
+
41
+ private
42
+
43
+ attr_reader :from, :to, :amount, :note
44
+
45
+ def debiting_service
46
+ @debiting_service ||= ::SimpleWallet::AccountDebitingService.new(
47
+ account: from,
48
+ amount: amount,
49
+ note: note
50
+ )
51
+ end
52
+
53
+ def crediting_service
54
+ @crediting_service ||= ::SimpleWallet::AccountCreditingService.new(
55
+ account: to,
56
+ amount: amount,
57
+ note: note
58
+ )
59
+ end
60
+
61
+ def after_operation
62
+ end
63
+ end
64
+ end
@@ -1,3 +1,3 @@
1
1
  module SimpleWallet
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end