Skip to content

Commit

Permalink
cleaning specification monkeypatches
Browse files Browse the repository at this point in the history
  • Loading branch information
mpapis committed Apr 27, 2015
1 parent c0faf12 commit e03f5ba
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
10 changes: 6 additions & 4 deletions lib/gem-empty/specification.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module GemEmpty
# monkey patch rubygems specification to easily find gem version
module Specification
def self.installed_gems
if
Expand All @@ -9,11 +10,12 @@ def self.installed_gems
Gem.source_index.map{|name,spec| spec}
end
end
def self.find(name = "gem-empty")
@gem_empty_spec ||= installed_gems.find{|spec| spec.name == name}
def self.find_gem_spec(name)
installed_gems.find{|spec| spec.name == name}
end
def self.version
find ? find.version.to_s : nil
def self.gem_loaded?(name, version)
spec = find_gem_spec(name)
spec && spec.version.to_s == version
end
end
end
5 changes: 2 additions & 3 deletions lib/rubygems_plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@

# continue only if loaded and called versions all the same, and not shared gems disabled in bundler
if
( $:.include?(called_path) || GemEmpty::Specification.version == called_version ) and
( $:.include?(called_path) || GemEmpty::Specification.gem_loaded?("gem-empty", called_version) ) and
( !defined?(Bundler) || ( defined?(Bundler) && Bundler::SharedHelpers.in_bundle? && !Bundler.settings[:disable_shared_gems]) )

then
require 'gem-empty/command'
Gem::CommandManager.instance.register_command :empty

end
14 changes: 9 additions & 5 deletions test/gem-empty/specification_and_version_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@
end

it "finds specification" do
GemEmpty::Specification.find("gem-empty").name.must_equal("gem-empty")
GemEmpty::Specification.find_gem_spec("gem-empty").name.must_equal("gem-empty")
end

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

it "does not find imaginary gems" do
GemEmpty::Specification.find("imaginary-gem").must_equal(nil)
it "confirms specification version" do
GemEmpty::Specification.gem_loaded?("gem-empty", GemEmpty::VERSION).must_equal true
end

it "does not confirms specification version" do
GemEmpty::Specification.gem_loaded?("gem-empty", "0.0.0").wont_equal true
end

end

0 comments on commit e03f5ba

Please sign in to comment.