Skip to content

Commit e03f5ba

Browse files
committed
cleaning specification monkeypatches
1 parent c0faf12 commit e03f5ba

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

lib/gem-empty/specification.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module GemEmpty
2+
# monkey patch rubygems specification to easily find gem version
23
module Specification
34
def self.installed_gems
45
if
@@ -9,11 +10,12 @@ def self.installed_gems
910
Gem.source_index.map{|name,spec| spec}
1011
end
1112
end
12-
def self.find(name = "gem-empty")
13-
@gem_empty_spec ||= installed_gems.find{|spec| spec.name == name}
13+
def self.find_gem_spec(name)
14+
installed_gems.find{|spec| spec.name == name}
1415
end
15-
def self.version
16-
find ? find.version.to_s : nil
16+
def self.gem_loaded?(name, version)
17+
spec = find_gem_spec(name)
18+
spec && spec.version.to_s == version
1719
end
1820
end
1921
end

lib/rubygems_plugin.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55

66
# continue only if loaded and called versions all the same, and not shared gems disabled in bundler
77
if
8-
( $:.include?(called_path) || GemEmpty::Specification.version == called_version ) and
8+
( $:.include?(called_path) || GemEmpty::Specification.gem_loaded?("gem-empty", called_version) ) and
99
( !defined?(Bundler) || ( defined?(Bundler) && Bundler::SharedHelpers.in_bundle? && !Bundler.settings[:disable_shared_gems]) )
10-
10+
then
1111
require 'gem-empty/command'
1212
Gem::CommandManager.instance.register_command :empty
13-
1413
end

test/gem-empty/specification_and_version_test.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,19 @@
99
end
1010

1111
it "finds specification" do
12-
GemEmpty::Specification.find("gem-empty").name.must_equal("gem-empty")
12+
GemEmpty::Specification.find_gem_spec("gem-empty").name.must_equal("gem-empty")
1313
end
1414

15-
it "gets specification version" do
16-
GemEmpty::Specification.version.must_equal(GemEmpty::VERSION)
15+
it "does not find imaginary gems" do
16+
GemEmpty::Specification.find_gem_spec("imaginary-gem").must_equal(nil)
1717
end
1818

19-
it "does not find imaginary gems" do
20-
GemEmpty::Specification.find("imaginary-gem").must_equal(nil)
19+
it "confirms specification version" do
20+
GemEmpty::Specification.gem_loaded?("gem-empty", GemEmpty::VERSION).must_equal true
21+
end
22+
23+
it "does not confirms specification version" do
24+
GemEmpty::Specification.gem_loaded?("gem-empty", "0.0.0").wont_equal true
2125
end
2226

2327
end

0 commit comments

Comments
 (0)