Module: Librarian

Defined in:
tomes/spells/librarian/librarian.rb

Overview

Note:

This module only handles directories and ‘.rb’ files.

A module to extend DragonRuby’s file handling capabilities.

Class Method Summary collapse

Class Method Details

.dredge_directory(directory, files = []) ⇒ Array<Strings>

Recursively gathers the file paths for all files in a given directory and its subdirectories.

Parameters:

  • directory (String)

    The directory name (relative to ‘mygame/’) with no trailing forward-slash.

  • files (Array) (defaults to: [])

    This is the memo variable. Leave it empty if calling this method manually.

Returns:

  • (Array<Strings>)


26
27
28
29
30
31
32
33
34
35
# File 'tomes/spells/librarian/librarian.rb', line 26

def dredge_directory(directory, files = [])
  header = "#{directory}/"

  files << list_file_paths(directory)
  folders = list_directories(directory)

  return files.flatten if folders.empty?

  folders.map { |folder| dredge_directory(header + folder, files.flatten) }.flatten.uniq
end

.list_directories(root_directory) ⇒ Array<Strings>

Lists any entries in a directory that lack a dot in its name. Does not list the root directory.

Parameters:

  • root_directory (String)

    The root directory name (relative to ‘mygame/’) with no trailing forward-slash.

Returns:

  • (Array<Strings>)


49
50
51
# File 'tomes/spells/librarian/librarian.rb', line 49

def list_directories(root_directory)
  $gtk.list_files(root_directory).reject { |file| file.include? '.' }.flatten
end

.list_file_paths(directory) ⇒ Array<Strings>

Lists the full (DragonRuby) path for all .rb files in a given directory.

Parameters:

  • directory (String)

    The directory name (relative to ‘mygame/’) with no trailing forward-slash.

Returns:

  • (Array<Strings>)


40
41
42
43
44
# File 'tomes/spells/librarian/librarian.rb', line 40

def list_file_paths(directory)
  $gtk.list_files(directory).select { |file| file.include? '.rb' }
      .map { |file| "#{directory}/#{file}" }
      .flatten
end

.require_directory(directory = 'app', ignore: ['app/main.rb', 'app/tick.rb']) ⇒ Object

Note:

This method ‘requires’ the files in each directory in alphabetical order. There is no easy way around this.

Requires all .rb files in a given directory. Files can be ignored via the ignore parameter or by including them in a folder with a dot in its name. Search starts at the ‘app’ directory by default. (DragonRuby will not accept files outside of ‘mygame/’.) Ignores this file and ‘main.rb’ by default. This is the method you’re most likely to use from this module.

Parameters:

  • directory (String) (defaults to: 'app')

    The directory that contains the files to be required. Defaults to ‘app’.

  • ignore (Array<String>) (defaults to: ['app/main.rb', 'app/tick.rb'])

    A list of all the files you want ignored. Defaults to ‘app/main.rb’ @note The full file path (starting with ‘app/’) must be specified.



17
18
19
20
# File 'tomes/spells/librarian/librarian.rb', line 17

def require_directory(directory = 'app', ignore: ['app/main.rb', 'app/tick.rb'])
  dredge_directory(directory).reject { |file| ignore.include? file }
                             .each { |file| require file }
end