Class: DoubleEntry::Transfer

Inherits:
Object
  • Object
show all
Defined in:
lib/double_entry/transfer.rb

Defined Under Namespace

Classes: Set

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Transfer

Returns a new instance of Transfer.



65
66
67
68
69
70
71
72
73
# File 'lib/double_entry/transfer.rb', line 65

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_lengthObject

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

.transfersObject

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.



8
9
10
# File 'lib/double_entry/transfer.rb', line 8

def transfers
  @transfers ||= Set.new
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



63
64
65
# File 'lib/double_entry/transfer.rb', line 63

def code
  @code
end

#fromObject (readonly)

Returns the value of attribute from.



63
64
65
# File 'lib/double_entry/transfer.rb', line 63

def from
  @from
end

#toObject (readonly)

Returns the value of attribute to.



63
64
65
# File 'lib/double_entry/transfer.rb', line 63

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
25
# File 'lib/double_entry/transfer.rb', line 18

def transfer(amount, options = {})
  fail TransferIsNegative if amount < Money.zero
  from = options[:from]
  to = options[:to]
  code = options[:code]
  detail = options[:detail]
  transfers.find!(from, to, code).process(amount, from, to, code, detail)
end

Instance Method Details

#process(amount, from, to, code, detail) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/double_entry/transfer.rb', line 75

def process(amount, from, to, code, detail)
  if from.scope_identity == to.scope_identity && from.identifier == to.identifier
    fail TransferNotAllowed, 'from and to are identical'
  end
  if to.currency != from.currency
    fail MismatchedCurrencies, "Missmatched currency (#{to.currency} <> #{from.currency})"
  end
  Locking.lock_accounts(from, to) do
    credit, debit = Line.new, Line.new

    credit_balance = Locking.(from)
    debit_balance  = Locking.(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., debit. = 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., debit. = to, from

    credit.save!
    debit.partner_id = credit.id
    debit.save!
    credit.update_attribute :partner_id, debit.id
  end
end