lib/polyglot.rb in polyglot-0.2.1 vs lib/polyglot.rb in polyglot-0.2.2
- old
+ new
@@ -1,9 +1,9 @@
$:.unshift File.dirname(__FILE__)
module Polyglot
- @registrations ||= {} # Guard against reloading
+ @registrations ||= {} # Guard against reloading
@loaded ||= {}
def self.register(extension, klass)
extension = [extension] unless Enumerable === extension
extension.each{|e|
@@ -11,29 +11,32 @@
}
end
def self.find(file, *options, &block)
extensions = @registrations.keys*","
- $:.each{|lib|
- matches = Dir[lib+"/"+file+".{"+extensions+"}"]
+ is_absolute = file[0] == File::SEPARATOR || file[0] == File::ALT_SEPARATOR || file =~ /\A[A-Z]:\\/i
+ (is_absolute ? [""] : $:).each{|lib|
+ base = is_absolute ? "" : lib+File::SEPARATOR
+ # In Windows, repeated SEPARATOR chars have a special meaning, avoid adding them
+ matches = Dir[base+file+".{"+extensions+"}"]
# Revisit: Should we do more do if more than one candidate found?
$stderr.puts "Polyglot: found more than one candidate for #{file}: #{matches*", "}" if matches.size > 1
if path = matches[0]
- return [ path, @registrations[path.gsub(/.*\./,'')]]
+ return [ path, @registrations[path.gsub(/.*\./,'')]]
end
}
return nil
end
def self.load(*a, &b)
return if @loaded[a[0]] # Check for $: changes or file time changes and reload?
begin
source_file, loader = Polyglot.find(*a, &b)
if (loader)
- loader.load(source_file)
- @loaded[a[0]] = true
+ loader.load(source_file)
+ @loaded[a[0]] = true
else
- raise LoadError
+ raise LoadError.new("Polyglot failed to load '#{a[0]}' either directly or using extensions #{@registrations.keys.sort.inspect}")
end
end
end
end