Class: DoubleEntry::Transfer
- Inherits:
-
Object
- Object
- DoubleEntry::Transfer
- Defined in:
- lib/double_entry/transfer.rb
Defined Under Namespace
Classes: Set
Class Attribute Summary collapse
- .transfers ⇒ Object private
Instance Attribute Summary collapse
-
#code ⇒ Object
Returns the value of attribute code.
-
#description ⇒ Object
Returns the value of attribute description.
-
#from ⇒ Object
Returns the value of attribute from.
-
#to ⇒ Object
Returns the value of attribute to.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(attributes) ⇒ Transfer
constructor
A new instance of Transfer.
- #process(amount, from, to, code, detail) ⇒ Object
Constructor Details
#initialize(attributes) ⇒ Transfer
Returns a new instance of Transfer.
59 60 61 |
# File 'lib/double_entry/transfer.rb', line 59 def initialize(attributes) attributes.each { |name, value| send("#{name}=", value) } end |
Class Attribute Details
.transfers ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
9 10 11 |
# File 'lib/double_entry/transfer.rb', line 9 def transfers @transfers ||= Set.new end |
Instance Attribute Details
#code ⇒ Object
Returns the value of attribute code.
57 58 59 |
# File 'lib/double_entry/transfer.rb', line 57 def code @code end |
#description ⇒ Object
Returns the value of attribute description.
57 58 59 |
# File 'lib/double_entry/transfer.rb', line 57 def description @description end |
#from ⇒ Object
Returns the value of attribute from.
57 58 59 |
# File 'lib/double_entry/transfer.rb', line 57 def from @from end |
#to ⇒ Object
Returns the value of attribute to.
57 58 59 |
# File 'lib/double_entry/transfer.rb', line 57 def to @to end |
Class Method Details
.transfer(amount, options = {}) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
14 15 16 17 18 19 20 21 |
# File 'lib/double_entry/transfer.rb', line 14 def transfer(amount, = {}) raise TransferIsNegative if amount < Money.zero from = [:from] to = [:to] code = [:code] detail = [:detail] transfers.find!(from, to, code).process(amount, from, to, code, detail) end |
Instance Method Details
#process(amount, from, to, code, detail) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/double_entry/transfer.rb', line 63 def process(amount, from, to, code, detail) if from.scope_identity == to.scope_identity and from.identifier == to.identifier raise TransferNotAllowed.new("from and to are identical") end if to.currency != from.currency raise MismatchedCurrencies.new("Missmatched currency (#{to.currency} <> #{from.currency})") end Locking.lock_accounts(from, to) do credit, debit = Line.new, Line.new credit_balance = Locking.balance_for_locked_account(from) debit_balance = Locking.balance_for_locked_account(to) credit_balance.update_attribute :balance, credit_balance.balance - amount debit_balance.update_attribute :balance, debit_balance.balance + amount credit.amount, debit.amount = -amount, amount credit.account, debit.account = from, to credit.code, debit.code = code, code credit.detail, debit.detail = detail, detail credit.balance, debit.balance = credit_balance.balance, debit_balance.balance credit.partner_account, debit.partner_account = to, from credit.save! debit.partner_id = credit.id debit.save! credit.update_attribute :partner_id, debit.id end end |