Sha256: 7b7868f6603ff5cca3fc8875b375ac3252db32c0d3ffd36a1f4a0ebdd6bbc18f

Contents?: true

Size: 1.97 KB

Versions: 113

Compression:

Stored size: 1.97 KB

Contents

require 'pathname'

module Polyglot
  @registrations ||= {} # Guard against reloading
  @loaded ||= {}

  class PolyglotLoadError < LoadError; end

  class NestedLoadError < LoadError
    def initialize le
      @le = le
    end
    def reraise
      raise @le
    end
  end

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

  def self.find(file, *options, &block)
    is_absolute = Pathname.new(file).absolute?
    (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}{,.#{@registrations.keys*',.'}}"]
      # 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

  def self.load(*a, &b)
    file = a[0].to_str
    return if @loaded[file] # Check for $: changes or file time changes and reload?
    begin
      source_file, loader = Polyglot.find(file, *a[1..-1], &b)
      if (loader)
        begin
          loader.load(source_file)
          @loaded[file] = true
        rescue LoadError => e
          raise Polyglot::NestedLoadError.new(e)
        end
      else
        raise PolyglotLoadError.new("Failed to load #{file} using extensions #{(@registrations.keys+["rb"]).sort*", "}")
      end
    end
  end
end

module Kernel
  alias polyglot_original_require require

  def require(*a, &b)
    polyglot_original_require(*a, &b)
  rescue LoadError => load_error
    begin
      Polyglot.load(*a, &b)
    rescue Polyglot::NestedLoadError => e
      e.reraise
    rescue LoadError
      # Raise the original exception, possibly a MissingSourceFile with a path
      raise load_error
    end
  end
end

Version data entries

113 entries across 105 versions & 12 rubygems

Version Path
classiccms-0.7.5 vendor/bundle/gems/polyglot-0.3.3/lib/polyglot.rb
classiccms-0.7.4 vendor/bundle/gems/polyglot-0.3.3/lib/polyglot.rb
classiccms-0.7.3 vendor/bundle/gems/polyglot-0.3.3/lib/polyglot.rb
active_mailer-0.0.10 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/polyglot-0.3.3/lib/polyglot.rb
judge-2.0.5 vendor/bundle/ruby/2.1.0/gems/polyglot-0.3.3/lib/polyglot.rb
challah-1.0.0 vendor/bundle/gems/polyglot-0.3.3/lib/polyglot.rb
classiccms-0.7.2 vendor/bundle/gems/polyglot-0.3.3/lib/polyglot.rb
classiccms-0.7.1 vendor/bundle/gems/polyglot-0.3.3/lib/polyglot.rb
swipe-rails-0.0.5 vendor/bundle/gems/polyglot-0.3.3/lib/polyglot.rb
active_mailer-0.0.9 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/polyglot-0.3.3/lib/polyglot.rb
active_mailer-0.0.8 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/polyglot-0.3.3/lib/polyglot.rb
active_mailer-0.0.7 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/polyglot-0.3.3/lib/polyglot.rb
active_mailer-0.0.6 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/polyglot-0.3.3/lib/polyglot.rb
classiccms-0.7.0 vendor/bundle/gems/polyglot-0.3.3/lib/polyglot.rb
challah-1.0.0.beta3 vendor/bundle/gems/polyglot-0.3.3/lib/polyglot.rb
fc-webicons-0.0.4 vendor/bundle/ruby/1.9.1/gems/polyglot-0.3.3/lib/polyglot.rb
challah-1.0.0.beta2 vendor/bundle/gems/polyglot-0.3.3/lib/polyglot.rb
challah-1.0.0.beta vendor/bundle/gems/polyglot-0.3.3/lib/polyglot.rb
fc-webicons-0.0.3 vendor/bundle/ruby/1.9.1/gems/polyglot-0.3.3/lib/polyglot.rb
fc-webicons-0.0.2 vendor/bundle/ruby/1.9.1/gems/polyglot-0.3.3/lib/polyglot.rb