Module: Polyglot

Defined in:
lib/gems/polyglot-0.2.3/lib/polyglot/version.rb,
lib/gems/polyglot-0.2.3/lib/polyglot.rb

Overview

:nodoc:

Defined Under Namespace

Modules: VERSION

Class Method Summary collapse

Class Method Details

.find(file, *options, &block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/gems/polyglot-0.2.3/lib/polyglot.rb', line 14

def self.find(file, *options, &block)
  extensions = @registrations.keys*","
  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(/.*\./,'')]]
    end
  }
  return nil
end

.load(*a, &b) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/gems/polyglot-0.2.3/lib/polyglot.rb', line 30

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
    else
      msg = "Failed to load #{a[0]} using extensions #{(@registrations.keys+["rb"]).sort*", "}"
      if defined?(MissingSourceFile)
        raise MissingSourceFile.new(msg, a[0])
      else
        raise LoadError.new(msg)
      end
    end
  end
end

.register(extension, klass) ⇒ Object



7
8
9
10
11
12
# File 'lib/gems/polyglot-0.2.3/lib/polyglot.rb', line 7

def self.register(extension, klass)
  extension = [extension] unless Enumerable === extension
  extension.each{|e|
    @registrations[e] = klass
  }
end