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 +4 -4
- data/app/services/simple_wallet/transfer_service.rb +64 -0
- data/lib/simple_wallet/version.rb +1 -1
- data/test/dummy/log/test.log +25650 -0
- data/test/services/transfer_service_test.rb +193 -0
- metadata +4 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d40dd6572b85931bc243f759cc5ff11c0a59eedc6260bbea55fb7687c28f4d39
|
|
4
|
+
data.tar.gz: ff5f84546d807d2e1a2548ba15f92100b051d66f9a9787246d07a924c07cff9c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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
|