mbulat/plutus — pull request #15 by willglynn (2013-02-18 03:05:08+00:00)
Title: Balance refactoring

I refactored the way balances are calculated in Plutus in three commits:

1. There was a lot of duplication: each `Plutus::Account` child class had its own definition of `credits_balance` and `debits_balance`, even though these are completely identical. I pushed these onto a single definition in `Plutus::Account`.
2. `Plutus::Account` now contained identical logic for both `credits_balance` and `debits_balance` function, operating on different `has_many` relationships. I took this as a call to refactor balance calculation into an association extension, whereby `credit_amounts.balance` and `debit_amounts.balance` both exist, backed by a single `Plutus::AmountsExtension#balance`. I then updated the `credit_`/`debit_balance` functions to defer to the relations' `balance` methods.
3. `Plutus::Transaction` contains similar `has_many Amount` relations, so I made them use the same `AmountsExtension`, in hopes of removing another balance calculation. Besides eliminating more code, this secondarily eliminated an integer truncation error in the current implementation, which I wrapped in a now-passing test case.