Class: RVGP::Base::JournalValidation

Inherits:
Validation show all
Includes:
Application::DescendantRegistry
Defined in:
lib/rvgp/base/validation.rb

Overview

A base class, from which your journal validations should inherit. For more information on validations, and your options, see the documentation notes on JournalValidation.

Instance Attribute Summary collapse

Attributes inherited from Validation

#errors, #warnings

Instance Method Summary collapse

Methods inherited from Validation

#error!, #valid?, #warning!

Methods included from Pta::AvailabilityHelper

#hledger, #ledger, #pta

Constructor Details

#initialize(reconciler) ⇒ JournalValidation

Create a new Journal Validation



179
180
181
182
# File 'lib/rvgp/base/validation.rb', line 179

def initialize(reconciler)
  super()
  @reconciler = reconciler
end

Instance Attribute Details

#reconcilerRVGP::Reconcilers::CsvReconciler, RVGP::Reconcilers::JournalReconciler (readonly)

The reconciler whose output will be inspected by this journal validation instance.

Returns:



169
170
171
# File 'lib/rvgp/base/validation.rb', line 169

def reconciler
  @reconciler
end

Instance Method Details

#validate_no_balance(with_error_msg, account) ⇒ Object

This helper method will supply the provided account to pta.balance. And if there is a balance returned, the supplied error message will be added to our :errors colection, citing the balance that was encountered.

Parameters:

  • with_error_msg (String)

    A description of the error that corresponds to the returned balances.

  • account (Array<String>)

    This arguments is supplied directly to Pta::AvailabilityHelper#pta‘s #balance method



209
210
211
212
213
214
215
216
217
218
219
# File 'lib/rvgp/base/validation.rb', line 209

def validate_no_balance(with_error_msg, )
  results = pta.balance , file: reconciler.output_file

  error_citations = results.accounts.map do |ra|
    ra.amounts.map { |commodity| [ra.fullname, RVGP.pastel.red(''), commodity.to_s].join(' ') }
  end

  error_citations.flatten!

  error! with_error_msg, error_citations unless error_citations.empty?
end

#validate_no_transactions(with_error_msg, *args) ⇒ Object

This helper method will supply the provided arguments to pta.register. And if there are any transactions returned, the supplied error message will be added to our :errors colection, citing the transactions that were encountered.

Parameters:

  • with_error_msg (String)

    A description of the error that corresponds to the returned transactions.

  • args (Array<Object>)

    These arguments are supplied directly to Pta::AvailabilityHelper#pta‘s #register method



190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/rvgp/base/validation.rb', line 190

def validate_no_transactions(with_error_msg, *args)
  ledger_opts = args.last.is_a?(Hash) ? args.pop : {}

  results = pta.register(*args, { file: reconciler.output_file }.merge(ledger_opts))

  transactions = block_given? ? yield(results.transactions) : results.transactions

  error_citations = transactions.map do |posting|
    format '%<date>s: %<payee>s', date: posting.date.to_s, payee: posting.payee
  end

  error! with_error_msg, error_citations unless error_citations.empty?
end