Module: RockBooks::BookSetLoader

Defined in:
lib/rock_books/helpers/book_set_loader.rb

Overview

Entry point is ‘load` method. Loads files in a directory to instantiate a BookSet.

Class Method Summary collapse

Class Method Details

.get_files_with_types(directory) ⇒ Object



10
11
12
13
14
15
# File 'lib/rock_books/helpers/book_set_loader.rb', line 10

def get_files_with_types(directory)
  files = Dir[File.join(directory, '*.txt')]
  files.each_with_object({}) do |filespec, files_with_types|
    files_with_types[filespec] = ParseHelper.find_document_type_in_file(filespec)
  end
end

.load(run_options) ⇒ Object

Uses all *.txt files in the specified directory; uses @doc_type to determine which is the chart of accounts and which are journals. To exclude a file, make the extension other than .rdt.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rock_books/helpers/book_set_loader.rb', line 46

def load(run_options)

  files_with_types = get_files_with_types(run_options.input_dir)

   = select_files_of_type(files_with_types, 'chart_of_accounts')
  ()

  journal_files = select_files_of_type(files_with_types, /journal/) # include 'journal' and 'general_journal'
  validate_journal_file_count(journal_files)

  chart_of_accounts = ChartOfAccounts.from_file(.first)
  journals = journal_files.map { |fs| Journal.from_file(chart_of_accounts, fs) }
  BookSet.new(run_options, chart_of_accounts, journals)
end

.select_files_of_type(files_with_types, target_doc_type_regex) ⇒ Object



38
39
40
# File 'lib/rock_books/helpers/book_set_loader.rb', line 38

def select_files_of_type(files_with_types, target_doc_type_regex)
  files_with_types.select { |filespec, doc_type| target_doc_type_regex === doc_type }.keys
end

.validate_chart_account_count(chart_of_account_files) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/rock_books/helpers/book_set_loader.rb', line 18

def ()
  size = .size

  if size == 0
    raise Error.new("Chart of accounts file not found in input directory.\n" +
                        " Does it have a '@doc_type: chart_of_accounts' line?")
  elsif size > 1
    raise Error.new("Expected only 1 chart of accounts file but found: #{}.")
  end
end

.validate_journal_file_count(journal_files) ⇒ Object



30
31
32
33
34
35
# File 'lib/rock_books/helpers/book_set_loader.rb', line 30

def validate_journal_file_count(journal_files)
  if journal_files.size == 0
    raise Error.new("No journal files found in directory #{directory}. " +
                        "A journal file must contain the line '@doc_type: journal'")
  end
end