class MudratProjector::Projector
Constants
- AccountDoesNotExist
- AccountExists
- BalanceError
- InvalidAccount
- InvalidTransaction
Attributes
from[R]
Public Class Methods
new(params = {})
click to toggle source
# File lib/mudrat_projector/projector.rb, line 14 def initialize params = {} @chart = params.fetch :chart, ChartOfAccounts.new @from = params.fetch :from, ABSOLUTE_START @transactions = [] @transaction_with_ids = {} @validator = Validator.new projector: self, chart: @chart end
Public Instance Methods
account_exists?(account_id)
click to toggle source
# File lib/mudrat_projector/projector.rb, line 31 def account_exists? account_id @chart.exists? account_id end
accounts=(accounts)
click to toggle source
# File lib/mudrat_projector/projector.rb, line 25 def accounts= accounts accounts.each do |account_id, params| add_account account_id, params end end
add_account(account_id, **params)
click to toggle source
# File lib/mudrat_projector/projector.rb, line 35 def add_account account_id, **params validate_account! account_id, params @chart.add_account account_id, params end
add_transaction(params)
click to toggle source
# File lib/mudrat_projector/projector.rb, line 40 def add_transaction params if params.is_a? Transaction transaction = params elsif id = params.delete(:id) return add_transaction_with_id(id, params) else transaction = build_transaction params end @transactions.push transaction end
add_transaction_with_id(id, params)
click to toggle source
# File lib/mudrat_projector/projector.rb, line 51 def add_transaction_with_id(id, params) @transaction_with_ids[id] = build_transaction params end
alter_transaction(id, effective_date: nil, scale: nil)
click to toggle source
# File lib/mudrat_projector/projector.rb, line 55 def alter_transaction(id, effective_date: nil, scale: nil) orig = remove_transaction id old, new = orig.slice(effective_date - 1) old.each do |t| add_transaction t; end unless scale == 0 new_transaction = new.clone_transaction multiplier: scale add_transaction_with_id id, new_transaction end end
balanced?()
click to toggle source
# File lib/mudrat_projector/projector.rb, line 83 def balanced? balance.zero? end
each_transaction(&block)
click to toggle source
# File lib/mudrat_projector/projector.rb, line 78 def each_transaction(&block) @transactions.each &block @transaction_with_ids.values.each(&block) end
end_scheduled_transaction(id, effective_date: nil)
click to toggle source
# File lib/mudrat_projector/projector.rb, line 74 def end_scheduled_transaction(id, effective_date: nil) alter_transaction id, effective_date: (effective_date + 1), scale: 0 end
fetch_transaction(id)
click to toggle source
# File lib/mudrat_projector/projector.rb, line 65 def fetch_transaction(id) @transaction_with_ids.fetch id end
net_worth()
click to toggle source
# File lib/mudrat_projector/projector.rb, line 87 def net_worth @chart.net_worth.round(2).to_f end
project(to: end_of_projection, build_next: false, &block)
click to toggle source
# File lib/mudrat_projector/projector.rb, line 91 def project to: end_of_projection, build_next: false, &block must_be_balanced! projection = Projection.new range: (from..to), chart: @chart handler = TransactionHandler.new projection: projection if build_next handler.next_projector = self.class.new from: to + 1, chart: @chart end each_transaction do |transaction| handler << transaction; end projection.project! &block handler.next_projector end
remove_transaction(id)
click to toggle source
# File lib/mudrat_projector/projector.rb, line 69 def remove_transaction(id) @transaction_with_ids.fetch id # generate KeyError @transaction_with_ids.delete id end
transactions=(transactions)
click to toggle source
# File lib/mudrat_projector/projector.rb, line 103 def transactions= transactions transactions.each do |transaction| add_transaction transaction; end end
Private Instance Methods
build_transaction(params)
click to toggle source
# File lib/mudrat_projector/projector.rb, line 109 def build_transaction params return params if params.is_a? Transaction klass = params.has_key?(:schedule) ? ScheduledTransaction : Transaction klass.new(params).tap do |transaction| validate_transaction! transaction end end