Class: RVGP::Validations::DuplicateTagsValidation

Inherits:
Base::JournalValidation show all
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

#reconciler

Attributes inherited from Base::Validation

#errors, #warnings

Instance Method Summary collapse

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

#hledger, #ledger, #pta

Constructor Details

This class inherits a constructor from RVGP::Base::JournalValidation

Instance Method Details

#validateObject

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)
  dupe_messages = []

  journal.postings.each do |posting|
    posting_tag_names = posting.tags.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.tags.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?

        dupe_messages << 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
      dupe_messages << 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 dupe_messages.empty?
    error! 'These postings have been tagged with the same tag, more than once', dupe_messages
  end
end