Class: Generalis::Transaction::DoubleEntry

Inherits:
Struct
  • Object
show all
Defined in:
lib/generalis/transaction/double_entry.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#amount_centsObject

Returns the value of attribute amount_cents

Returns:

  • (Object)

    the current value of amount_cents



5
6
7
# File 'lib/generalis/transaction/double_entry.rb', line 5

def amount_cents
  @amount_cents
end

#creditObject

Returns the value of attribute credit

Returns:

  • (Object)

    the current value of credit



5
6
7
# File 'lib/generalis/transaction/double_entry.rb', line 5

def credit
  @credit
end

#currencyObject

Returns the value of attribute currency

Returns:

  • (Object)

    the current value of currency



5
6
7
# File 'lib/generalis/transaction/double_entry.rb', line 5

def currency
  @currency
end

#debitObject

Returns the value of attribute debit

Returns:

  • (Object)

    the current value of debit



5
6
7
# File 'lib/generalis/transaction/double_entry.rb', line 5

def debit
  @debit
end

Instance Method Details

#amountObject



6
7
8
# File 'lib/generalis/transaction/double_entry.rb', line 6

def amount
  Money.from_cents(amount_cents || 0, currency)
end

#amount=(value) ⇒ Object



10
11
12
13
14
15
# File 'lib/generalis/transaction/double_entry.rb', line 10

def amount=(value)
  value = Money.from_amount(value, currency) unless value.is_a?(Money)

  self.amount_cents = value.cents
  self.currency     = value.currency.iso_code
end

#entriesArray<Entry>

Returns:



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/generalis/transaction/double_entry.rb', line 18

def entries
  credit, debit = self.credit, self.debit

  # Reverse the debit/credit accounts when supplied a negative amount.
  credit, debit = debit, credit if amount.negative?

  [
    # Flip the amount back to positive since we've already swapped accounts.
    Credit.new(account: credit, amount: amount.abs, pair_id: pair_id),
    Debit.new(account:  debit, amount:  amount.abs, pair_id: pair_id)
  ]
end

#pair_idString

Returns:

  • (String)


32
33
34
# File 'lib/generalis/transaction/double_entry.rb', line 32

def pair_id
  @pair_id ||= SecureRandom.uuid
end