Class: DoubleEntry::Transfer
- Inherits:
-
Object
- Object
- DoubleEntry::Transfer
- Defined in:
- lib/double_entry/transfer.rb
Defined Under Namespace
Classes: Set
Class Attribute Summary collapse
- .code_max_length ⇒ Object private
- .transfers ⇒ Object private
Instance Attribute Summary collapse
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#from ⇒ Object
readonly
Returns the value of attribute from.
-
#to ⇒ Object
readonly
Returns the value of attribute to.
Class Method Summary collapse
Instance Method Summary collapse
- #create_line_metadata(credit, debit, metadata) ⇒ Object
- #create_lines(amount, code, detail, from_account, to_account) ⇒ Object
-
#initialize(attributes) ⇒ Transfer
constructor
A new instance of Transfer.
- #process(amount, options) ⇒ Object
Constructor Details
#initialize(attributes) ⇒ Transfer
Returns a new instance of Transfer.
64 65 66 67 68 69 70 71 72 |
# File 'lib/double_entry/transfer.rb', line 64 def initialize(attributes) @code = attributes[:code] @from = attributes[:from] @to = attributes[:to] if code.length > Transfer.code_max_length fail TransferCodeTooLongError, "transfer code '#{code}' is too long. Please limit it to #{Transfer.code_max_length} characters." end end |
Class Attribute Details
.code_max_length ⇒ 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.
13 14 15 |
# File 'lib/double_entry/transfer.rb', line 13 def code_max_length @code_max_length ||= 47 end |
Instance Attribute Details
#code ⇒ Object (readonly)
Returns the value of attribute code.
62 63 64 |
# File 'lib/double_entry/transfer.rb', line 62 def code @code end |
#from ⇒ Object (readonly)
Returns the value of attribute from.
62 63 64 |
# File 'lib/double_entry/transfer.rb', line 62 def from @from end |
#to ⇒ Object (readonly)
Returns the value of attribute to.
62 63 64 |
# File 'lib/double_entry/transfer.rb', line 62 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.
18 19 20 21 22 23 24 |
# File 'lib/double_entry/transfer.rb', line 18 def transfer(amount, = {}) fail TransferIsNegative if amount < Money.zero from_account = [:from] to_account = [:to] code = [:code] transfers.find!(from_account, to_account, code).process(amount, ) end |
Instance Method Details
#create_line_metadata(credit, debit, metadata) ⇒ Object
116 117 118 119 120 121 |
# File 'lib/double_entry/transfer.rb', line 116 def (credit, debit, ) .each_pair do |key, value| LineMetadata.create!(:line => credit, :key => key, :value => value) LineMetadata.create!(:line => debit, :key => key, :value => value) end end |
#create_lines(amount, code, detail, from_account, to_account) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/double_entry/transfer.rb', line 92 def create_lines(amount, code, detail, from_account, to_account) credit, debit = Line.new, Line.new credit_balance = Locking.balance_for_locked_account(from_account) debit_balance = Locking.balance_for_locked_account(to_account) 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_account, to_account 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_account, from_account credit.save! debit.partner_id = credit.id debit.save! credit.update_attribute :partner_id, debit.id [credit, debit] end |
#process(amount, options) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/double_entry/transfer.rb', line 74 def process(amount, ) from_account = [:from] to_account = [:to] code = [:code] detail = [:detail] = [:metadata] if from_account.scope_identity == to_account.scope_identity && from_account.identifier == to_account.identifier fail TransferNotAllowed, 'from account and to account are identical' end if to_account.currency != from_account.currency fail MismatchedCurrencies, "Mismatched currency (#{to_account.currency} <> #{from_account.currency})" end Locking.lock_accounts(from_account, to_account) do credit, debit = create_lines(amount, code, detail, from_account, to_account) (credit, debit, ) if end end |