class RequireDir::Loader

This class is meant to be instantiated per project/library, and then used to load en masse ruby files from a directory.

Attributes

stderr[RW]
options[RW]
project_root[RW]

Public Class Methods

new(root_dir, options = {}) click to toggle source
# File lib/require_dir/loader.rb, line 17
def initialize(root_dir, options = {})
  raise ArgumentError.new("Folder #{root_dir} is not found") unless Dir.exist?(root_dir)
  self.project_root = root_dir
  self.options = options
end

Public Instance Methods

debug?() click to toggle source
# File lib/require_dir/loader.rb, line 23
def debug?
  options[:debug] || ENV['REQUIRE_DIR_DEBUG']
end
dir(folder, recursive = false) click to toggle source
# File lib/require_dir/loader.rb, line 27
def dir(folder, recursive = false)
  folder = "/#{folder}" unless folder.start_with? '/'
  ::Dir.glob(project_root + folder + (recursive ? '/**/*.rb' : '/*.rb') ) do |file|
    puts "Loading #{file}" if debug?
    begin
      Kernel.require file
    rescue SyntaxError, LoadError => e
      report_error(e, file)
      raise(e)
    end
  end
end
Also aliased as: require_dir
dir_r(folder) click to toggle source
# File lib/require_dir/loader.rb, line 40
def dir_r(folder)
  dir(folder, true)
end
Also aliased as: require_dir_r
require_dir(folder, recursive = false)
Alias for: dir
require_dir_r(folder)
Alias for: dir_r

Private Instance Methods

outputs(*args) click to toggle source
# File lib/require_dir/loader.rb, line 59
def outputs(*args)
  self.class.stderr.puts(*args) if self.class.stderr
end
report_error(e, file) click to toggle source
# File lib/require_dir/loader.rb, line 49
def report_error(e, file)
  len = file.length + 6
  outputs '—' * len
  outputs "⇨  #{file.bold.yellow} ⇦".bold.white
  outputs '—' * len
  outputs e.message.bold.red
  outputs '—' * len
  outputs e.backtrace.join("\n").bold.black if e.backtrace && !e.backtrace.empty?
end