Class: DoubleEntry::Transfer

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

Defined Under Namespace

Classes: Set

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Transfer

Returns a new instance of Transfer.



49
50
51
52
# File 'lib/double_entry/transfer.rb', line 49

def initialize(attributes)
  @meta_requirement = []
  attributes.each { |name, value| send("#{name}=", value) }
end

Instance Attribute Details

#codeObject

Returns the value of attribute code.



47
48
49
# File 'lib/double_entry/transfer.rb', line 47

def code
  @code
end

#descriptionObject

Returns the value of attribute description.



47
48
49
# File 'lib/double_entry/transfer.rb', line 47

def description
  @description
end

#fromObject

Returns the value of attribute from.



47
48
49
# File 'lib/double_entry/transfer.rb', line 47

def from
  @from
end

#meta_requirementObject

Returns the value of attribute meta_requirement.



47
48
49
# File 'lib/double_entry/transfer.rb', line 47

def meta_requirement
  @meta_requirement
end

#toObject

Returns the value of attribute to.



47
48
49
# File 'lib/double_entry/transfer.rb', line 47

def to
  @to
end

Class Method Details

.transfer(defined_transfers, 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.

Raises:



6
7
8
9
10
11
12
# File 'lib/double_entry/transfer.rb', line 6

def self.transfer(defined_transfers, amount, options = {})
  raise TransferIsNegative if amount < Money.empty
  from, to, code, meta, detail = options[:from], options[:to], options[:code],  options[:meta],  options[:detail]
  defined_transfers.
    find!(from, to, code).
    process!(amount, from, to, code, meta, detail)
end

Instance Method Details

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



54
55
56
57
58
59
60
61
62
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
# File 'lib/double_entry/transfer.rb', line 54

def process!(amount, from, to, code, meta, detail)
  if from.scope_identity == to.scope_identity and from.identifier == to.identifier
    raise TransferNotAllowed.new
  end

  meta_requirement.each do |key|
    if meta[key].nil?
      raise RequiredMetaMissing.new
    end
  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.meta,    debit.meta    = meta, meta
    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