Module: RequirePatternXt::Core

Defined in:
lib/require_pattern_xt/core.rb

Class Method Summary collapse

Class Method Details

.log_error(require_map) ⇒ Object

Raises:

  • (LoadError)


29
30
31
32
33
34
35
36
37
# File 'lib/require_pattern_xt/core.rb', line 29

def self.log_error(require_map)
  STDERR.puts Coloring.red("\nSome files failed:")
  require_map.each { |file, e|
    msg = Coloring.red("    #{file}: ") + 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

.require_pattern(*patterns) ⇒ Object

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.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/require_pattern_xt/core.rb', line 6

def self.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?
      log_error(require_map)
    end
  end
end