Sha256: 70cad14463eca26501e3aabd734479bd603c26fa0f4ba6122fcd7c03ee979c6b

Contents?: true

Size: 1.34 KB

Versions: 1

Compression:

Stored size: 1.34 KB

Contents

require 'coloring'

module Kernel
  # Requires all files matching the glob patterns provided.
  # If a file fails to load, it keeps retrying until all remaining files fail to load, in which case the exception is
  # output to STDERR for each failed file.
  def require_pattern(*patterns)
    loaded = false # Indicates whether any files were loaded at all
    require_map = patterns.map { |pattern| Dir.glob(pattern) }.flatten.reduce({}) { |m,v|  m[v] = nil; m }
    loop do
      remaining = require_map.reject! do |file,|
        begin
          loaded = true if require File.expand_path(file)
          true
        rescue => require_map[file]
          false
        end
      end

      if require_map.empty?
        break loaded
      elsif remaining.nil?
        STDERR.puts ReqPat::Coloring.red("\nSome files failed:")
        require_map.each { |file, e|
          msg = ReqPat::Coloring.red("    #{file}: ") +
                ReqPat::Coloring.yellow("#{e.backtrace[0]} #{e}")
          STDERR.puts msg
        }
        STDERR.puts
        raise LoadError, "One or more files failed to load. See STDERR output for details."
      end
    end
  end

  def require_relative_pattern(*patterns)
    caller_path = caller_locations(1,1)[0].path
    patterns.map! { |pattern| File.join(File.dirname(caller_path), pattern) }
    require_pattern(*patterns)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
require_pattern-1.1.1 lib/require_pattern.rb