Class: MudratProjector::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/mudrat_projector/validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(projector: projector, chart: chart) ⇒ Validator

Returns a new instance of Validator.



5
6
7
8
# File 'lib/mudrat_projector/validator.rb', line 5

def initialize projector: projector, chart: chart
  @projector = projector
  @chart     = chart
end

Instance Attribute Details

#chartObject (readonly)

Returns the value of attribute chart.



3
4
5
# File 'lib/mudrat_projector/validator.rb', line 3

def chart
  @chart
end

Instance Method Details

#must_be_balanced!Object



10
11
12
13
14
15
# File 'lib/mudrat_projector/validator.rb', line 10

def must_be_balanced!
  unless projector.balanced?
    raise Projector::BalanceError, "Cannot project unless the accounts "\
      "are in balance"
  end
end

#validate_account!(account_id, params) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/mudrat_projector/validator.rb', line 17

def validate_account! , params
  if chart.exists? 
    raise Projector::AccountExists, "Account #{.inspect} exists"
  end
  unless Account::TYPES.include? params[:type]
    raise Projector::InvalidAccount, "Account #{.inspect} has "\
      "invalid type #{params[:type].inspect}"
  end
end

#validate_transaction!(transaction) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/mudrat_projector/validator.rb', line 27

def validate_transaction! transaction
  if transaction.date < projector.from
    raise Projector::InvalidTransaction, "Transactions cannot occur before "\
      "projection start date. (#{projector.from} vs. #{transaction.date})"
  end
  unless transaction.balanced?
    raise Projector::BalanceError, "Credits and debit entries both "\
      "must be supplied; they cannot amount to zero"
  end
  if transaction.credits.empty? || transaction.debits.empty?
    raise Projector::InvalidTransaction, "You must supply at least a debit "\
      "and a credit on each transaction"
  end
end