module RockBooks::BookSetLoader
Entry point is `load` method. Loads files in a directory to instantiate a BookSet
.
Public Instance Methods
get_files_with_types(directory)
click to toggle source
@return a hash whose keys are the filespecs and values are the document types
# 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)
click to toggle source
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.
# 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) chart_of_account_files = select_files_of_type(files_with_types, 'chart_of_accounts') validate_chart_account_count(chart_of_account_files) 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(chart_of_account_files.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)
click to toggle source
# 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)
click to toggle source
# File lib/rock_books/helpers/book_set_loader.rb, line 19 def validate_chart_account_count(chart_of_account_files) size = chart_of_account_files.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: #{chart_of_account_files}.") end end
validate_journal_file_count(journal_files)
click to toggle source
# 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