Skip to content

Instantly share code, notes, and snippets.

@janko
Last active August 29, 2015 14:09
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 janko/00973f3d269b8d2c870c to your computer and use it in GitHub Desktop.
Save janko/00973f3d269b8d2c870c to your computer and use it in GitHub Desktop.
A script that opens any gem in your EDITOR.
#!/usr/bin/env ruby
# USAGE: dive GEM_NAME
#
# Opens the lib/ directory of the specified gem in EDITOR. If you're in a Bundler
# project, it will open the locked-down version of the gem.
#
# You can supply only the first few letters of the gem, and the first gem that
# matches will be opened.
class Dive
def initialize(gem_name)
@gem_name = gem_name
end
def call
gem = find_gem
gem_lib_path = File.join(gem.full_gem_path, "lib")
open(gem_lib_path)
end
private
def find_gem
gem = gems.find { |gem| gem.name.start_with?(@gem_name) }
gem or fail
end
def gems
require "bundler"
Bundler.environment.specs | Gem::Specification.to_a
rescue Bundler::GemfileNotFound
Gem::Specification.to_a
end
def open(dir)
Dir.chdir(dir) do
system ENV["EDITOR"], dir
end
end
def fail
warn "Gem not found: #{@gem_name}"
exit
end
end
Dive.new(ARGV.first).call
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment