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

Returns a hash whose keys are the filespecs and values are the document types.

Returns:

  • a hash whose keys are the filespecs and values are the document types



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

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 something other than .txt.



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

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 { |filespec| Journal.from_file(chart_of_accounts, filespec) }
  BookSet.new(run_options, chart_of_accounts, journals)
end

.select_files_of_type(files_with_types, target_doc_type_regex) ⇒ Object



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

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



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

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



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

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