Module: DirectoryLoader

Defined in:
lib/asker/loader/directory_loader.rb

Class Method Summary collapse

Class Method Details

.call(dirname) ⇒ Object

Load input data from one directory

Parameters:

  • dirname (String)

    Directory name



10
11
12
13
14
15
16
# File 'lib/asker/loader/directory_loader.rb', line 10

def self.call(dirname)
  DirectoryLoader.check_dir(dirname)
  files = (Dir.new(dirname).entries - [".", ".."]).sort
  # Accept only HAML or XML files
  accepted = files.select { |f| %w[.xml .haml].include? File.extname(f) }
  DirectoryLoader.load_files(accepted, dirname)
end

.check_dir(dirname) ⇒ Object



18
19
20
21
22
23
# File 'lib/asker/loader/directory_loader.rb', line 18

def self.check_dir(dirname)
  return if Dir.exist? dirname

  Logger.error "DirectoryLoader: #{dirname} directory dosn't exist!"
  exit 1
end

.load_files(filenames, dirname) ⇒ Object

Load files from dirname directory

Parameters:

  • filenames (Array)

    File name list

  • dirname (String)

    Base directory



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/asker/loader/directory_loader.rb', line 29

def self.load_files(filenames, dirname)
  data = {concepts: [], codes: [], problems: []}
  filenames.each do |filename|
    filepath = File.join(dirname, filename)
    loaded = FileLoader.call(filepath)
    data[:concepts] += loaded[:concepts]
    data[:codes] += loaded[:codes]
    data[:problems] += loaded[:problems]
  end
  data
end