Class: MudratProjector::Validator
- Inherits:
-
Object
- Object
- MudratProjector::Validator
- Defined in:
- lib/mudrat_projector/validator.rb
Instance Attribute Summary collapse
-
#chart ⇒ Object
readonly
Returns the value of attribute chart.
Instance Method Summary collapse
-
#initialize(projector: projector, chart: chart) ⇒ Validator
constructor
A new instance of Validator.
- #must_be_balanced! ⇒ Object
- #validate_account!(account_id, params) ⇒ Object
- #validate_transaction!(transaction) ⇒ Object
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
#chart ⇒ Object (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 26 27 28 29 30 31 |
# File 'lib/mudrat_projector/validator.rb', line 17 def validate_account! account_id, params if chart.exists? account_id raise Projector::AccountExists, "Account #{account_id.inspect} exists" end unless Account::TYPES.include? params[:type] raise Projector::InvalidAccount, "Account #{account_id.inspect} has "\ "invalid type #{params[:type].inspect}" end if params.has_key?(:open_date) && params[:open_date] > projector.from if params.has_key? :opening_balance raise Projector::InvalidAccount, "Account #{account_id.inspect} opens "\ "after projector, but has an opening balance" end end end |
#validate_transaction!(transaction) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/mudrat_projector/validator.rb', line 33 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 |