1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
module Backports
module StdLib
class LoadedFeatures
if RUBY_VERSION >= "1.9"
# Full paths are recorded in $LOADED_FEATURES.
@@our_loads = {}
# Check loaded features for one that matches "#{any of the load path}/#{feature}"
def include?(feature)
return true if @@our_loads[feature]
# Assume backported features are Ruby libraries (i.e. not C)
@loaded ||= $LOADED_FEATURES.group_by{|p| File.basename(p, ".rb")}
if fullpaths = @loaded[File.basename(feature, ".rb")]
fullpaths.any?{|fullpath|
base_dir, = fullpath.partition("/#{feature}")
$LOAD_PATH.include?(base_dir)
}
end
end
def self.mark_as_loaded(feature)
@@our_loads[feature] = true
# Nothing to do, the full path will be OK
end
else
# Requested features are recorded in $LOADED_FEATURES
def include?(feature)
# Assume backported features are Ruby libraries (i.e. not C)
$LOADED_FEATURES.include?("#{File.basename(feature, '.rb')}.rb")
end
def self.mark_as_loaded(feature)
$LOADED_FEATURES << "#{File.basename(feature, '.rb')}.rb"
end
end
end
class << self
attr_accessor :extended_lib
def extend_relative relative_dir="stdlib"
loaded = Backports::StdLib::LoadedFeatures.new
dir = File.expand_path(relative_dir, File.dirname(caller.first.split(/:\d/,2).first))
Dir.entries(dir).
map{|f| Regexp.last_match(1) if /^(.*)\.rb$/ =~ f}.
compact.
each do |f|
path = File.expand_path(f, dir)
if loaded.include?(f)
require path
else
@extended_lib[f] << path
end
end
end
end
self.extended_lib ||= Hash.new{|h, k| h[k] = []}
end
end
|