Sha256: 9a2ea25f30b1e8c7febcf7823d43a5e5eb657fd0f97fc80c79a7f6153e7dd8c0

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

require 'require_dir/version'

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

    attr_accessor :project_root, :options

    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

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

    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

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

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
require_dir-0.1.2 lib/require_dir/loader.rb