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.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Loader.

Raises:

  • (ArgumentError)


17
18
19
20
21
# 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

Class Attribute Details

.stderrObject

Returns the value of attribute stderr.



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

def stderr
  @stderr
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)


23
24
25
# File 'lib/require_dir/loader.rb', line 23

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

#dir(folder, recursive = false) ⇒ Object Also known as: require_dir



27
28
29
30
31
32
33
34
35
36
37
38
# 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

#dir_r(folder) ⇒ Object Also known as: require_dir_r



40
41
42
# File 'lib/require_dir/loader.rb', line 40

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