Class: RVGP::Validations::DuplicateTagsValidation
- Inherits:
-
Base::JournalValidation
- Object
- Base::Validation
- Base::JournalValidation
- RVGP::Validations::DuplicateTagsValidation
- Defined in:
- lib/rvgp/validations/duplicate_tags_validation.rb
Overview
This class implements a journal validation that ensures a given transfer, hasn’t been tagged more than once, with the same tag.
Instance Attribute Summary
Attributes inherited from Base::JournalValidation
Attributes inherited from Base::Validation
Instance Method Summary collapse
-
#validate ⇒ Object
Reviews every transfer, and post, to ensure that there are no tags occurring more than once, in any given entry.
Methods inherited from Base::JournalValidation
#initialize, #validate_no_balance, #validate_no_transactions
Methods inherited from Base::Validation
#error!, #initialize, #valid?, #warning!
Methods included from Pta::AvailabilityHelper
Constructor Details
This class inherits a constructor from RVGP::Base::JournalValidation
Instance Method Details
#validate ⇒ Object
Reviews every transfer, and post, to ensure that there are no tags occurring more than once, in any given entry. Unlike most of the validations in RVGP, this one doesn’t use ledger or hledger to validate. This validation parses the file itself, in ruby, and ensures based on the contents.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/rvgp/validations/duplicate_tags_validation.rb', line 11 def validate journal = RVGP::Journal.parse File.read(reconciler.output_file) = [] journal.postings.each do |posting| posting_tag_names = posting..map(&:key) found_dupes = posting_tag_names.find_all { |tag| posting_tag_names.count(tag) > 1 }.uniq if found_dupes.empty? posting.transfers.each do |transfer| transfer_tag_names = transfer..map(&:key) + posting_tag_names found_dupes = transfer_tag_names.find_all { |tag| transfer_tag_names.count(tag) > 1 }.uniq next if found_dupes.empty? << format('Line %<line>d: %<date>s %<desc>s (Transfer: %<tags>s)', line: posting.line_number, date: posting.date, desc: posting.description, tags: found_dupes.join(', ')) end else << format('Line %<line>d: %<date>s %<desc>s (%<tags>s)', line: posting.line_number, date: posting.date, desc: posting.description, tags: found_dupes.join(',')) end end unless .empty? error! 'These postings have been tagged with the same tag, more than once', end end |