Class: RequireDir::Loader
- Inherits:
-
Object
- Object
- RequireDir::Loader
- 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
-
#options ⇒ Object
Returns the value of attribute options.
-
#project_root ⇒ Object
Returns the value of attribute project_root.
Instance Method Summary collapse
- #debug? ⇒ Boolean
- #dir(folder, recursive = false) ⇒ Object
- #dir_r(folder) ⇒ Object
-
#initialize(root_dir, options = {}) ⇒ Loader
constructor
A new instance of Loader.
Constructor Details
#initialize(root_dir, options = {}) ⇒ Loader
Returns a new instance of Loader.
11 12 13 14 15 |
# File 'lib/require_dir/loader.rb', line 11 def initialize(root_dir, = {}) raise ArgumentError.new("Folder #{root_dir} is not found") unless Dir.exist?(root_dir) self.project_root = root_dir self. = end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
9 10 11 |
# File 'lib/require_dir/loader.rb', line 9 def @options end |
#project_root ⇒ Object
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
17 18 19 |
# File 'lib/require_dir/loader.rb', line 17 def debug? [: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..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 |