Class: Generalis::Transaction

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/generalis/transaction.rb,
lib/generalis/transaction/dsl.rb,
lib/generalis/transaction/links.rb,
lib/generalis/transaction/preparation.rb,
lib/generalis/transaction/double_entry.rb

Direct Known Subclasses

Ledger::BaseTransaction

Defined Under Namespace

Modules: DSL, Links, Preparation Classes: DoubleEntry

Instance Method Summary collapse

Instance Method Details

#add_credit(attributes) ⇒ void

This method returns an undefined value.

Parameters:

  • attributes (Hash)


60
61
62
63
64
# File 'lib/generalis/transaction.rb', line 60

def add_credit(attributes)
  raise 'Cannot modify persisted transactions' if persisted?

  entries << Credit.new(attributes)
end

#add_debit(attributes) ⇒ void

This method returns an undefined value.

Parameters:

  • attributes (Hash)


68
69
70
71
72
# File 'lib/generalis/transaction.rb', line 68

def add_debit(attributes)
  raise 'Cannot modify persisted transactions' if persisted?

  entries << Debit.new(attributes)
end

#add_double_entry(credit_attributes, debit_attributes) ⇒ void

This method returns an undefined value.

Parameters:

  • credit_attributes (Hash)
  • debit_attributes (Hash)


77
78
79
80
81
82
# File 'lib/generalis/transaction.rb', line 77

def add_double_entry(credit_attributes, debit_attributes)
  pair_id = SecureRandom.uuid

  add_credit(credit_attributes.merge(pair_id: pair_id))
  add_debit(debit_attributes.merge(pair_id: pair_id))
end

This method returns an undefined value.

Parameters:

  • name (Symbol, String)
  • record (ActiveRecord::Base)


87
88
89
# File 'lib/generalis/transaction.rb', line 87

def add_link(name, record)
  links << Link.new(name: name, linkable: record)
end

#credit_amountsHash{String => Money}

Returns:

  • (Hash{String => Money})


92
93
94
# File 'lib/generalis/transaction.rb', line 92

def credit_amounts
  entries.select(&:credit?).group_by(&:currency).transform_values { |entries| entries.sum(&:amount) }
end

#debit_amountsHash{String => Money}

Returns:

  • (Hash{String => Money})


97
98
99
# File 'lib/generalis/transaction.rb', line 97

def debit_amounts
  entries.select(&:debit?).group_by(&:currency).transform_values { |entries| entries.sum(&:amount) }
end

#no_op?Boolean

Checks whether the transaction is would have any affect on account balances. A no-op transaction is one that has no entries with non-zero amounts.

Returns:

  • (Boolean)


105
106
107
# File 'lib/generalis/transaction.rb', line 105

def no_op?
  entries.all?(&:no_op?)
end