class MudratProjector::TransactionEntry

Attributes

account_id[RW]
amount[RW]

Public Class Methods

new(params = {}) click to toggle source
# File lib/mudrat_projector/transaction_entry.rb, line 46
def initialize params = {}
  @account_id      = params.fetch :account_id
  @scalar          = params.fetch :scalar
  @credit_or_debit = params.fetch :credit_or_debit
  validate!
end

Public Instance Methods

*(multiplier) click to toggle source
# File lib/mudrat_projector/transaction_entry.rb, line 53
def * multiplier
  return self if multiplier == 1
  self.class.new serialize.merge(scalar: scalar * multiplier)
end
calculate(chart_of_accounts = nil) click to toggle source
# File lib/mudrat_projector/transaction_entry.rb, line 58
def calculate chart_of_accounts = nil
  @amount = calculate_amount chart_of_accounts
  return if chart_of_accounts.nil?
  account_type = chart_of_accounts.fetch(account_id).type
  check = %i(asset expense).include?(account_type) ? :debit? : :credit?
  @delta = send(check) ? amount : -amount
end
calculate_amount(chart_of_accounts) click to toggle source
# File lib/mudrat_projector/transaction_entry.rb, line 66
def calculate_amount chart_of_accounts
  @amount = scalar
end
credit?() click to toggle source
# File lib/mudrat_projector/transaction_entry.rb, line 70
def credit?
  @credit_or_debit == :credit
end
debit?() click to toggle source
# File lib/mudrat_projector/transaction_entry.rb, line 74
def debit?
  @credit_or_debit == :debit
end
inspect() click to toggle source
# File lib/mudrat_projector/transaction_entry.rb, line 78
def inspect
  "#<#{self.class}: amount=#{fmt(scalar)}, account_id=#{account_id.inspect} type=#{@credit_or_debit.inspect}>"
end
serialize() click to toggle source
# File lib/mudrat_projector/transaction_entry.rb, line 82
def serialize
  {
    account_id:       account_id,
    scalar:           scalar,
    credit_or_debit:  @credit_or_debit,
  }
end
validate!() click to toggle source
# File lib/mudrat_projector/transaction_entry.rb, line 90
def validate!
  if scalar == 0
    raise ArgumentError, "You cannot supply a scalar of 0"
  end
  unless %i(credit debit).include? @credit_or_debit
    raise ArgumentError, "Must supply :credit or :debit, not #{@credit_or_debit.inspect}"
  end
end

Private Instance Methods

fmt(number) click to toggle source
# File lib/mudrat_projector/transaction_entry.rb, line 101
def fmt number
  number.respond_to?(:round) ? number.round(2).to_d : number
end