Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Roll our own Gem#latest_load_paths and use it when Bundler not active
- Loading branch information
1 parent
27f07fb
commit 50c670d
Showing
2 changed files
with
36 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
module Spork::GemHelpers | ||
extend self | ||
|
||
class GemPath | ||
attr_reader :name, :version, :path, :version_numbers | ||
include Comparable | ||
def initialize(p) | ||
@path = p | ||
@name, @version = File.basename(p).scan(/^(.+?)-([^-]+)$/).flatten | ||
@version_numbers = @version.split(/[^0-9]+/).map(&:to_i) | ||
end | ||
|
||
def <=>(other) | ||
raise "Not comparable gem paths ('#{name}' is not '#{other.name}')" unless name == other.name | ||
@version_numbers <=> other.version_numbers | ||
end | ||
end | ||
|
||
def latest_load_paths | ||
if defined?(Bundler) | ||
$LOAD_PATH.uniq | ||
else | ||
Dir["{#{Gem.paths.path.join(',')}}" + "/gems/*"].inject({}) do |h,f| | ||
gem_path = GemPath.new(f) | ||
if h[gem_path.name] | ||
h[gem_path.name] = gem_path if gem_path > h[gem_path.name] | ||
else | ||
h[gem_path.name] = gem_path | ||
end | ||
h | ||
end | ||
end | ||
end | ||
end |