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.



68
69
70
71
72
73
74
75
76
# File 'lib/double_entry/transfer.rb', line 68

def initialize(attributes)
  @code = attributes[:code]
  @from = attributes[:from]
  @to = attributes[:to]
  if Transfer.code_max_length && 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

Returns the value of attribute code_max_length.



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

def code_max_length
  @code_max_length
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.



11
12
13
# File 'lib/double_entry/transfer.rb', line 11

def transfers
  @transfers ||= Set.new
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



66
67
68
# File 'lib/double_entry/transfer.rb', line 66

def code
  @code
end

#fromObject (readonly)

Returns the value of attribute from.



66
67
68
# File 'lib/double_entry/transfer.rb', line 66

def from
  @from
end

#toObject (readonly)

Returns the value of attribute to.



66
67
68
# File 'lib/double_entry/transfer.rb', line 66

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.



16
17
18
19
20
21
22
# File 'lib/double_entry/transfer.rb', line 16

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

Instance Method Details

#create_line_metadata(credit, debit, metadata) ⇒ Object



123
124
125
126
127
128
129
130
# File 'lib/double_entry/transfer.rb', line 123

def (credit, debit, )
  .each_pair do |key, value|
    Array(value).each do |each_value|
      LineMetadata.create!(line: credit, key: key, value: each_value)
      LineMetadata.create!(line: debit, key: key, value: each_value)
    end
  end
end

#create_lines(amount, code, detail, from_account, to_account, metadata) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/double_entry/transfer.rb', line 98

def create_lines(amount, code, detail, , , )
  credit, debit = Line.new, Line.new

  credit_balance = Locking.()
  debit_balance  = Locking.()

  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. = , 
  credit.code, debit.code       = code, code
  credit.detail, debit.detail   = detail, detail
  credit.balance, debit.balance = credit_balance.balance, debit_balance.balance
  credit., debit. = ,  if DoubleEntry.config.

  credit., debit. = , 

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

#process(amount, options) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/double_entry/transfer.rb', line 78

def process(amount, options)
  credit = debit = nil
   = options[:from]
   = options[:to]
  code = options[:code]
  detail = options[:detail]
   = options[:metadata]
  if .scope_identity == .scope_identity && .identifier == .identifier
    fail TransferNotAllowed, 'from account and to account are identical'
  end
  if .currency != .currency
    fail MismatchedCurrencies, "Mismatched currency (#{.currency} <> #{.currency})"
  end
  Locking.lock_accounts(, ) do
    credit, debit = create_lines(amount, code, detail, , , )
    (credit, debit, ) if  && !DoubleEntry.config.
  end
  [credit, debit]
end