Class: Generalis::Transaction
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Generalis::Transaction
- 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
Defined Under Namespace
Modules: DSL, Links, Preparation Classes: DoubleEntry
Instance Method Summary collapse
- #add_credit(attributes) ⇒ void
- #add_debit(attributes) ⇒ void
- #add_double_entry(credit_attributes, debit_attributes) ⇒ void
- #add_link(name, record) ⇒ void
- #credit_amounts ⇒ Hash{String => Money}
- #debit_amounts ⇒ Hash{String => Money}
-
#no_op? ⇒ Boolean
Checks whether the transaction is would have any affect on account balances.
Instance Method Details
#add_credit(attributes) ⇒ void
This method returns an undefined value.
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.
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.
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 |
#add_link(name, record) ⇒ void
This method returns an undefined value.
87 88 89 |
# File 'lib/generalis/transaction.rb', line 87 def add_link(name, record) links << Link.new(name: name, linkable: record) end |
#credit_amounts ⇒ 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_amounts ⇒ 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.
105 106 107 |
# File 'lib/generalis/transaction.rb', line 105 def no_op? entries.all?(&:no_op?) end |