carolineartz
9/20/2017 - 3:10 PM

.pryrc

.pryrc

class Object
  def interesting_methods
    case self.class
    when Class
      public_methods.sort - Object.public_methods
    when Module
      public_methods.sort - Module.public_methods
    else
      public_methods.sort - Object.new.public_methods
    end
  end
end

if defined?(PryRemoteEm)
  Pry.config.object_pry_handler = proc do |target, *args|
   EM.run { target.remote_pry_em(*args) }
  end
end

def eap
  if defined?(AwesomePrint)
    AwesomePrint.pry!
  end
end

if defined?(PryByebug)
  Pry.commands.alias_command 'c', 'continue'
  Pry.commands.alias_command 's', 'step'
  Pry.commands.alias_command 'n', 'next'
  Pry.commands.alias_command 'f', 'finish'
end

# Hit Enter to repeat last command

Pry.config.color = true
Pry.config.ls.separator = "\n" # new lines between methods
Pry.config.ls.heading_color = :magenta
Pry.config.ls.public_method_color = :green
Pry.config.ls.protected_method_color = :yellow
Pry.config.ls.private_method_color = :bright_black

# === CUSTOM COMMANDS ===
# from: https://gist.github.com/1297510
default_command_set = Pry::CommandSet.new do
  command "copy", "Copy argument to the clip-board" do |str|
    IO.popen('pbcopy', 'w') { |f| f << str.to_s }
  end

  command "clear" do
    system 'clear'
    output.puts "Rails Environment: " + ENV['RAILS_ENV'] if  ENV['RAILS_ENV']
  end

  command "sql", "Send sql over AR." do |query|
    if ENV['RAILS_ENV'] || defined?(Rails)
      pp ActiveRecord::Base.connection.select_all(query)
    else
      pp "No rails env defined"
    end
  end

  command "caller_method" do |depth|
    depth = depth.to_i || 1
    if /^(.+?):(\d+)(?::in `(.*)')?/ =~ caller(depth + 1).first
      file   = Regexp.last_match[1]
      line   = Regexp.last_match[2].to_i
      method = Regexp.last_match[3]
      output.puts [file, line, method]
    end
  end

  Pry::Commands.command(/^$/, "repeat last command") do
    _pry_.run_command Pry.history.to_a.last
  end
end

Pry.config.commands.import default_command_set

class Array
  def self.toy(n = 10, &block)
    block_given? ? Array.new(n, &block) : Array.new(n) {|i| i + 1}
  end
end

class Hash
  def self.toy(n = 10)
    Hash[Array.toy(n).zip(Array.toy(n){|c| (96 + (c + 1)).chr})]
  end
end

if defined? Rails
  begin
    require 'factory_girl'
    require 'factory_girl_rails'
    Dir[File.expand_path("../factories/**/*.rb", Rails.root)].each { |f| require f }
    include FactoryGirl::Syntax::Methods
  rescue LoadError
  end
end

Pry.config.history.file = "./.pry_history"