-
Notifications
You must be signed in to change notification settings - Fork 7
/
.pryrc
156 lines (130 loc) · 3.91 KB
/
.pryrc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# alias 'q' for 'exit'
Pry.config.commands.alias_command "q", "exit-all"
# Load 'amazing_print'
begin
require 'amazing_print'
require 'amazing_print/ext/active_record'
require 'amazing_print/ext/active_support'
require 'amazing_print/ext/mongoid'
require 'amazing_print/ext/nokogiri'
require 'amazing_print/ext/ostruct'
AmazingPrint.pry!
rescue LoadError => err
end
# Load 'hirb'
begin
require 'hirb'
Hirb.enable
pry_print = Pry.config.print
Pry.config.print = proc do |*args|
Hirb::View.view_or_page_output(args[1]) || pry_print.call(*args)
end
rescue LoadError => err
end
# Launch Pry with access to the entire Rails stack
rails = File.join(Dir.getwd, 'config', 'environment.rb')
# Disable pry's shell integration (handle pasted Ruby code starting with a dot)
Pry.commands.delete /\.(.*)/
if File.exist?(rails) && ENV['SKIP_RAILS'].nil?
require rails
if Rails.version[0..0] == "2"
require 'console_app'
require 'console_with_helpers'
elsif Rails.version[0..0].in?(['3', '4', '5', '6'])
require 'rails/console/app'
require 'rails/console/helpers'
elsif Rails.version[0..0].in?(['7', '8'])
require 'rails/console/methods'
else
warn "[WARN] cannot load Rails console commands (Not using Rails v2-8?)"
end
# Rails' pry prompt
env = ENV['RAILS_ENV'] || Rails.env
rails_root = File.basename(Dir.pwd)
rails_env_prompt =
case env
when 'development'
'[DEV]'
when 'production'
'[PROD]'
else
"[#{env.upcase}]"
end
prompt = '%s %s %s:%s'
Pry.config.prompt = Pry::Prompt.new \
'Prompt',
'Custom prompt',
[
proc { |obj, _nest_level, pry_instance| "#{prompt}> " % [rails_root, rails_env_prompt, obj, pry_instance.input_ring.count] },
proc { |obj, _nest_level, pry_instance| "#{prompt}* " % [rails_root, rails_env_prompt, obj, pry_instance.input_ring.count] }
]
# [] acts as find()
ActiveRecord::Base.instance_eval { alias :[] :find } if defined?(ActiveRecord)
# Add Rails console helpers (like `reload!`) to pry
if defined?(Rails::ConsoleMethods)
extend Rails::ConsoleMethods
end
# r! to reload Rails console
if defined?(Rails) && Rails.version.to_f >= 3.0
def r!
reload!
end
end
# execute arbitrary SQL query through ActiveRecord
def execute query
ActiveRecord::Base.connection.execute query
end
# set logging to screen in Rails
if defined? Rails
# Rails 2.x
if ENV.include?('RAILS_ENV') && !Object.const_defined?('RAILS_DEFAULT_LOGGER')
require 'logger'
Object.const_set('RAILS_DEFAULT_LOGGER', Logger.new(STDOUT))
end
# Rails 3+
if Rails.logger && defined? ActiveRecord
Rails.logger = Logger.new(STDOUT)
ActiveRecord::Base.logger = Rails.logger
end
end
# .details method for pretty printing ActiveRecord's objects attributes
class Object
def details
if self.respond_to?(:attributes) and self.attributes.any?
max = self.attributes.keys.sort_by { |k| k.size }.pop.size + 5
puts
self.attributes.keys.sort.each do |k|
puts sprintf("%-#{max}.#{max}s%s", k, self.try(k))
end
puts
end
end
alias :detailed :details
end
# returns a collection of the methods that Rails added to the given class
# http://lucapette.com/irb/rails-core-ext-and-irb/
class Class
def core_ext
self
.instance_methods
.map { |m| [m, self.instance_method(m).source_location] }
.select { |m| m[1] && m[1][0] =~/activesupport/ }
.map { |m| m[0] }
.sort
end
end
# local methods helper
# http://rakeroutes.com/blog/customize-your-irb/
class Object
def local_methods
case self.class
when Class
self.public_methods.sort - Object.public_methods
when Module
self.public_methods.sort - Module.public_methods
else
self.public_methods.sort - Object.new.public_methods
end
end
end
end