-
Notifications
You must be signed in to change notification settings - Fork 1
/
pryrc
286 lines (252 loc) · 7.74 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#!/usr/bin/env ruby
# frozen_string_literal: true
# See https://github.com/pry/pry/wiki/Customization-and-configuration
# load this file please
Pry.config.should_load_rc = true
# config history file
if Gem::Version.new(Pry::VERSION) >= Gem::Version.new('0.13.0')
Pry.config.history_file = '~/.irb_history'
else
Pry.config.history.file = '~/.irb_history'
end
# config editor
Pry.config.editor = 'subl -w'
# Pry.commands.alias_command 'cc', 'continue'
# Pry.commands.alias_command 'ss', 'step'
# Pry.commands.alias_command 'nn', 'next'
# ==============================
# Helpers
# ==============================
#
require 'json'
def pp_json(object)
if object.is_a?(String)
puts JSON.pretty_generate(JSON.parse(object))
elsif object.is_a?(JSON)
puts JSON.pretty_generate(object)
else
puts JSON.pretty_generate(JSON.parse(object.to_json))
end
rescue StandardError => e
puts e
end
begin
extra_gem_locations = []
require 'bundler'
Bundler.with_unbundled_env do
extra_gem_locations << %x{dirname `gem which 'niceql'`}
end
# puts extra_gem_locations
extra_gem_locations.to_s.split("\n").each do |gem_location|
# puts gem_location
$LOAD_PATH.unshift(File.join(File.dirname(gem_location), 'lib'))
end
# puts $LOAD_PATH
#
::Kernel.require('niceql')
rescue LoadError => e
puts "oh well, maybe try rbenv exec gem install 'niceql'"
puts e.message if e.message
puts e.cause if e.cause
# puts e.backtrace if e.backtrace
end
def pp_sql(object)
if object.is_a?(String)
puts ::Niceql::Prettifier.prettify_sql(object)
end
end if defined?(::Niceql)
# ==============================
# Rails
# ==============================
#
if Kernel.const_defined?(:Rails) && ::Rails.env
begin
# rails 4 -> rails 6
require File.join(Rails.root, 'config', 'environment')
require 'rails/console/app'
require 'rails/console/helpers'
extend(Rails::ConsoleMethods)
puts 'Rails Console Helpers loaded'
rescue LoadError => e
# older rails < 4
require 'console_app'
require 'console_with_helpers'
puts 'Rails Console Helpers loaded'
rescue TypeError
# sometimes files don't exist to require,
# throwing TypeError: no implicit conversion of nil into String
# just move on
nil
end
## https://github.com/travisjeffery/dotfiles/blob/master/.railsrc
require 'logger'
if defined?(::ActiveRecord)
def pry_enable_logger
::ActiveRecord::Base.logger = Logger.new($stdout)
::ActiveRecord::Base.clear_active_connections!
nil
end
def pry_disable_logger
::ActiveRecord::Base.logger = nil
::ActiveRecord::Base.clear_active_connections!
nil
end
# prints nice information about a model
def pry_show_model(object)
if object.class == Class
y(object.column_names.sort)
else
y(object.class.column_names.sort)
end
end
def pry_show_tables
y(::ActiveRecord::Base.connection.tables)
end
# I use this one to dig into Rails core_ext
class Class
def core_ext
instance_methods.map do |m|
[m, instance_method(m).source_location]
end.select do |m|
m[1] && m[1][0] =~ /activesupport/
end.map do |m|
m[0]
end.sort
end
end
# Hit all models for auto-completion
def pry_enable_autocomplete
ApplicationRecord.descendants.each do |klass|
klass.name.constantize.column_names
rescue ActiveRecord::StatementInvalid
# do nothing, rails is having trouble loading
rescue StandardError => e
puts 'oh, okay then'
puts e.class if e.class
puts e.message if e.message
puts e.cause if e.cause
puts e.backtrace if e.backtrace
end
end
# logging into console by default
# pry_enable_logger
# pry_enable_autocomplete
end
end
# ==============================
# PRY DOC https://github.com/pry/pry-doc
# ==============================
# Try to force pry-doc
#
# gem install 'pry-doc'
begin
pry_doc_gem_locations = []
require 'bundler'
Bundler.with_unbundled_env do
# gem dependency pry-doc --version '>= 0.10'
pry_doc_gem_locations = %x(dirname `gem which 'pry-doc'`)
pry_doc_gem_locations << %x(dirname `gem which 'yard'`)
end
# puts pry_doc_gem_locations
pry_doc_gem_locations.split("\n").each do |gem_location|
# puts gem_location
$LOAD_PATH.unshift(File.join(File.dirname(gem_location), 'lib'))
end
# puts $LOAD_PATH
::Kernel.require('pry-doc')
rescue RunTimeError => e
puts "oh well, try gem install 'pry-doc'"
puts e.message if e.message
puts e.cause if e.cause
puts e.backtrace if e.backtrace
end
# ==============================
# nicer table printing
# ==============================
# env RAILS_USE_HIRB_GEM=true RAILS_PRINT_X_COLUMNS=12 bundle exec rails console
# env RAILS_USE_HIRB_GEM=true RAILS_PRINT_X_COLUMNS=12 bundle exec rescue rspec
if ENV['RAILS_USE_HIRB_GEM'] == 'true' && defined?(::Rails) && Rails.env
begin
hirb_gem_location = ''
Bundler.with_unbundled_env do
hirb_gem_location = %x(dirname `gem which hirb`)
end
# puts hirb_gem_location
$LOAD_PATH.unshift(File.join(File.dirname(hirb_gem_location), 'lib'))
# puts $LOAD_PATH
::Kernel.require('hirb')
if defined? Hirb
def hirb_enable_fancy_print
old_print = Pry.config.print
Pry.config.print = proc do |*args|
Hirb::View.view_or_page_output(args[1]) || old_print.call(*args)
end
end
def hirb_enable_fancy_print_options
Hirb.enable(
{
width: (ENV.fetch('RAILS_PRINT_X_COLUMNS', 6).to_i * 35),
height: 500,
output:
ActiveRecord::Base.descendants.reject do |klass_name|
klass_name.to_s == 'ActiveRecord::SchemaMigration'
end
.each_with_object({}) do |klass_name, tmp_hash|
tmp_hash[klass_name.to_s] =
begin
{
options: {
fields: klass_name.column_names.take(ENV.fetch(
'RAILS_PRINT_X_COLUMNS',
6,
).to_i) | ['created_at', 'updated_at'],
},
}
rescue
next
end
end,
},
)
nil
end
def pry_active_record_show_me_the_first(number_of_records)
ApplicationRecord.descendants.each do |klass_name|
puts "\n#{klass_name}\n"
Hirb::View.view_or_page_output(klass_name.first(number_of_records))
end
nil
end
def pry_active_record_show_me_the_last(number_of_records)
ApplicationRecord.descendants.each do |klass_name|
puts "\n#{klass_name}\n"
Hirb::View.view_or_page_output(klass_name.last(number_of_records))
end
nil
end
Hirb.enable
hirb_enable_fancy_print
hirb_enable_fancy_print_options
end
rescue StandardError => e
# oh well
puts e.message if e.message
puts e.cause if e.cause
puts e.backtrace.join("\n") if e.backtrace
end
# Worker.order(created_at: :desc).limit(2)
# Job.order(created_at: :desc).limit(2)
# Company.order(id: :desc).limit(2)
# Employer.order(created_at: :desc).limit(2)
# Customer.order(created_at: :desc).limit(2)
# Badge.order(ccreated_at: :desc).limit(2)
# Golf::Event.limit(2)
# RAILS_USE_HIRB_GEM=true RAILS_PRINT_X_COLUMNS=16 bundle exec rails console
# Baseball::Event.limit(2)
end
# pp Hirb::View.config
# RAILS_USE_HIRB_GEM=true RAILS_PRINT_X_COLUMNS=12 bundle exec rails console
puts "Ruby #{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}"
puts "Pry #{Pry::VERSION}"
puts "PryDoc #{PryDoc::VERSION}"
puts 'Loaded pryrc'