Skip to content

Instantly share code, notes, and snippets.

@chrismo
Last active December 14, 2015 20:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrismo/5145856 to your computer and use it in GitHub Desktop.
Save chrismo/5145856 to your computer and use it in GitHub Desktop.
private method refactor

= Private Method Refactor

From a discussion on rubyparley.

##
#
# Gem::PathSupport facilitates the GEM_HOME and GEM_PATH environment settings
# to the rest of RubyGems.
#
class Gem::PathSupport
##
# The default system path for managing Gems.
attr_reader :home
##
# Array of paths to search for Gems.
attr_reader :path
##
#
# Constructor. Takes a single argument which is to be treated like a
# hashtable, or defaults to ENV, the system environment.
#
def initialize(env=ENV)
@env = env
@home = calculate_home
@path = calculate_path
end
##
# Determine the Gem home directory (as reported by Gem.dir).
def calculate_home
# note 'env' vs 'ENV'...
home = @env["GEM_HOME"] || ENV["GEM_HOME"] || Gem.default_dir
home.gsub!(File::ALT_SEPARATOR, File::SEPARATOR) if File::ALT_SEPARATOR
home
end
##
# Determine the Gem search path (as reported by Gem.path).
def calculate_path
# FIX: it should be [home, *path], not [*path, home]
gem_path = []
# FIX: I can't tell wtf this is doing.
gpaths = @env["GEM_PATH"] || ENV["GEM_PATH"]
gpaths ||= (ENV['GEM_PATH'] || "").empty? ? nil : ENV["GEM_PATH"]
if gpaths
if gpaths.kind_of?(Array)
gem_path = gpaths.dup
else
gem_path = gpaths.split(Gem.path_separator)
end
if File::ALT_SEPARATOR then
gem_path.map! do |this_path|
this_path.gsub File::ALT_SEPARATOR, File::SEPARATOR
end
end
gem_path << @home
else
gem_path = Gem.default_path + [@home]
if defined?(APPLE_GEM_HOME)
gem_path << APPLE_GEM_HOME
end
end
gem_path.uniq
end
end
require 'test/unit'
require 'fileutils'
require 'tmpdir'
require File.dirname(__FILE__) + '/path_support'
class TestGemPathSupport < Test::Unit::TestCase
def setup
@tempdir = Dir.mktmpdir
ENV["GEM_HOME"] = @tempdir
ENV["GEM_PATH"] = [@tempdir, "something"].join(File::PATH_SEPARATOR)
end
def teardown
FileUtils.remove_entry_secure @tempdir
super
end
def test_initialize
ps = Gem::PathSupport.new
assert_equal ENV["GEM_HOME"], ps.home
expected = util_path
assert_equal expected, ps.path, "defaults to GEM_PATH"
end
def test_initialize_home
ps = Gem::PathSupport.new "GEM_HOME" => "#{@tempdir}/foo"
assert_equal File.join(@tempdir, "foo"), ps.home
expected = util_path + [File.join(@tempdir, 'foo')]
assert_equal expected, ps.path
end
if defined?(File::ALT_SEPARATOR) and File::ALT_SEPARATOR
def test_initialize_home_normalize
alternate = @tempdir.gsub(File::SEPARATOR, File::ALT_SEPARATOR)
ps = Gem::PathSupport.new "GEM_HOME" => alternate
assert_equal @tempdir, ps.home, "normalize values"
end
end
def test_initialize_path
ps = Gem::PathSupport.new "GEM_PATH" => %W[#{@tempdir}/foo #{@tempdir}/bar]
assert_equal ENV["GEM_HOME"], ps.home
expected = [
File.join(@tempdir, 'foo'),
File.join(@tempdir, 'bar'),
ENV["GEM_HOME"],
]
assert_equal expected, ps.path
end
def test_initialize_home_path
ps = Gem::PathSupport.new("GEM_HOME" => "#{@tempdir}/foo",
"GEM_PATH" => %W[#{@tempdir}/foo #{@tempdir}/bar])
assert_equal File.join(@tempdir, "foo"), ps.home
expected = [File.join(@tempdir, 'foo'), File.join(@tempdir, 'bar')]
assert_equal expected, ps.path
end
def util_path
ENV["GEM_PATH"].split(File::PATH_SEPARATOR)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment