Class: Generalis::Transaction::DoubleEntry
- Inherits:
-
Struct
- Object
- Struct
- Generalis::Transaction::DoubleEntry
- Defined in:
- lib/generalis/transaction/double_entry.rb
Instance Attribute Summary collapse
-
#amount_cents ⇒ Object
Returns the value of attribute amount_cents.
-
#credit ⇒ Object
Returns the value of attribute credit.
-
#currency ⇒ Object
Returns the value of attribute currency.
-
#debit ⇒ Object
Returns the value of attribute debit.
Instance Method Summary collapse
Instance Attribute Details
#amount_cents ⇒ Object
Returns the value of attribute amount_cents
5 6 7 |
# File 'lib/generalis/transaction/double_entry.rb', line 5 def amount_cents @amount_cents end |
#credit ⇒ Object
Returns the value of attribute credit
5 6 7 |
# File 'lib/generalis/transaction/double_entry.rb', line 5 def credit @credit end |
#currency ⇒ Object
Returns the value of attribute currency
5 6 7 |
# File 'lib/generalis/transaction/double_entry.rb', line 5 def currency @currency end |
#debit ⇒ Object
Returns the value of attribute debit
5 6 7 |
# File 'lib/generalis/transaction/double_entry.rb', line 5 def debit @debit end |
Instance Method Details
#amount ⇒ Object
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 |
#entries ⇒ Array<Entry>
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_id ⇒ String
32 33 34 |
# File 'lib/generalis/transaction/double_entry.rb', line 32 def pair_id @pair_id ||= SecureRandom.uuid end |