-
-
Save adejoux/db9f594319536e529582 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
require 'rubygems' unless defined? Gem # rubygems is only needed in 1.8 | |
### START debundle.rb ### | |
# Copyright (c) Conrad Irwin <conrad.irwin@gmail.com> -- MIT License | |
# Source: https://github.com/ConradIrwin/pry-debundle | |
# Pry references removed by Jan Lelis <mail@janlelis.de> | |
module Debundle | |
VERSION = '0.8.d' | |
end | |
class << Debundle | |
# Break out of the Bundler jail. | |
# | |
# This can be used to load files in development that are not in your Gemfile (for | |
# example if you want to test something with a tool that you have locally). | |
# | |
# @example | |
# Debundle.debundle! | |
# require 'all_the_things' | |
# | |
# Normally you don't need to cal this directly though, as it is called for you when Pry | |
# starts. | |
# | |
# See https://github.com/carlhuda/bundler/issues/183 for some background. | |
# | |
def debundle! | |
return unless defined?(Bundler) | |
if rubygems_18_or_better? | |
if Gem.post_reset_hooks.reject!{ |hook| hook.source_location.first =~ %r{/bundler/} } | |
Bundler.preserve_gem_path | |
Gem.clear_paths | |
Gem::Specification.reset | |
remove_bundler_monkeypatches | |
end | |
# Rubygems 1.6 — TODO might be quite slow. | |
elsif Gem.source_index && Gem.send(:class_variable_get, :@@source_index) | |
Gem.source_index.refresh! | |
remove_bundler_monkeypatches | |
else | |
raise "No hacks found :(" | |
end | |
rescue => e | |
puts "Debundling failed: #{e.message}" | |
puts "When reporting bugs to https://github.com/ConradIrwin/pry-debundle, please include:" | |
puts "* gem version: #{Gem::VERSION rescue 'undefined'}" | |
puts "* bundler version: #{Bundler::VERSION rescue 'undefined'}" | |
puts "* ruby version: #{RUBY_VERSION rescue 'undefined'}" | |
puts "* ruby engine: #{RUBY_ENGINE rescue 'undefined'}" | |
end | |
private | |
def rubygems_18_or_better? | |
defined?(Gem.post_reset_hooks) | |
end | |
def rubygems_20_or_better? | |
Gem::VERSION.to_i >= 2 | |
end | |
# Ugh, this stuff is quite vile. | |
def remove_bundler_monkeypatches | |
if rubygems_20_or_better? | |
load 'rubygems/core_ext/kernel_require.rb' | |
else | |
load 'rubygems/custom_require.rb' | |
end | |
if rubygems_18_or_better? | |
Kernel.module_eval do | |
def gem(gem_name, *requirements) # :doc: | |
skip_list = (ENV['GEM_SKIP'] || "").split(/:/) | |
raise Gem::LoadError, "skipping #{gem_name}" if skip_list.include? gem_name | |
spec = Gem::Dependency.new(gem_name, *requirements).to_spec | |
spec.activate if spec | |
end | |
end | |
else | |
Kernel.module_eval do | |
def gem(gem_name, *requirements) # :doc: | |
skip_list = (ENV['GEM_SKIP'] || "").split(/:/) | |
raise Gem::LoadError, "skipping #{gem_name}" if skip_list.include? gem_name | |
Gem.activate(gem_name, *requirements) | |
end | |
end | |
end | |
end | |
end | |
Debundle.debundle! | |
def load_gem(gem, &block) | |
begin | |
if require gem | |
yield if block_given? | |
end | |
rescue Exception => err | |
warn "Couldn't load #{gem}: #{err}" | |
end | |
end | |
# Highlighting and other features | |
load_gem 'irbtools' | |
# Show log in Rails console | |
if defined? Rails | |
require 'logger' | |
if Rails.version =~ /^2\./ # Rails 2.3 | |
Object.const_set('RAILS_DEFAULT_LOGGER', Logger.new(STDOUT)) | |
else # Rails 3 | |
ActiveRecord::Base.logger = Logger.new(STDOUT) if defined? ActiveRecord | |
end | |
end | |
# Enable route helpers in Rails console | |
if defined? Rails | |
include Rails.application.routes.url_helpers | |
end | |
# Benchmarking helper (http://ozmm.org/posts/time_in_irb.html) | |
if defined? Benchmark | |
def benchmark(times = 1) | |
ret = nil | |
Benchmark.bm { |x| x.report { times.times { ret = yield } } } | |
ret | |
end | |
end | |
# loading bond for autocompletion | |
load_gem 'bond' do | |
Bond.start | |
end | |
# Random time method | |
class Time | |
def self.random(from=Time.at(0), to=Time.now) | |
Time.at rand(to.to_f - from.to_f) + from.to_f | |
end | |
end | |
# return a sorted list of methods minus those which are inherited from Object | |
class Object | |
def interesting_methods | |
(self.methods - Object.instance_methods).sort | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment