Class: RequireDir::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/require_dir/loader.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_dir, options = {}) ⇒ Loader

Returns a new instance of Loader.

Raises:

  • (ArgumentError)


11
12
13
14
15
# File 'lib/require_dir/loader.rb', line 11

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

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



9
10
11
# File 'lib/require_dir/loader.rb', line 9

def options
  @options
end

#project_rootObject

Returns the value of attribute project_root.



9
10
11
# File 'lib/require_dir/loader.rb', line 9

def project_root
  @project_root
end

Instance Method Details

#debug?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/require_dir/loader.rb', line 17

def debug?
  options[:debug] || ENV['REQUIRE_DIR_DEBUG']
end

#dir(folder, recursive = false) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/require_dir/loader.rb', line 21

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
      len = file.length + 6
      STDERR.puts '' * len
      STDERR.puts "#{file.bold.yellow}".bold.white
      STDERR.puts '' * len
      STDERR.puts e.message.bold.red
      STDERR.puts '' * len
      STDERR.puts e.backtrace.join("\n").bold.black if e.backtrace && !e.backtrace.empty?
      exit 1
    end
  end


end

#dir_r(folder) ⇒ Object



42
43
44
# File 'lib/require_dir/loader.rb', line 42

def dir_r(folder)
  dir(folder, true)
end