-
Notifications
You must be signed in to change notification settings - Fork 2
/
irbrc
153 lines (125 loc) · 4.41 KB
/
irbrc
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
# =========================
# Prof. H's .irbrc file
# Last update: 2019-01-09
# =========================
# External gems required:
# -------------------------
# hirb
puts "loading..."
# Make gems available
require 'rubygems'
# include Date automatically as well as Time enhancements
require 'date'
require 'time'
# include my gem for doing Project Euler tasks and the like
# require 'boqwij'
# Create an alias for a quick exit
alias q exit
ANSI = {}
ANSI[:RESET] = "\e[0m"
ANSI[:BOLD] = "\e[1m"
ANSI[:UNDERLINE] = "\e[4m"
ANSI[:LGRAY] = "\e[0;37m"
ANSI[:GRAY] = "\e[0;90m"
ANSI[:RED] = "\e[31m"
ANSI[:GREEN] = "\e[32m"
ANSI[:YELLOW] = "\e[33m"
ANSI[:BLUE] = "\e[34m"
ANSI[:MAGENTA] = "\e[35m"
ANSI[:CYAN] = "\e[36m"
ANSI[:WHITE] = "\e[37m"
# Build a simple colorful IRB prompt
IRB.conf[:PROMPT][:SIMPLE_COLOR] = {
:PROMPT_I => "#{ANSI[:BLUE]}>>#{ANSI[:RESET]} ",
:PROMPT_N => "#{ANSI[:BLUE]}>>#{ANSI[:RESET]} ",
:PROMPT_C => "#{ANSI[:RED]}?>#{ANSI[:RESET]} ",
:PROMPT_S => "#{ANSI[:YELLOW]}?>#{ANSI[:RESET]} ",
:RETURN => "#{ANSI[:GREEN]}=>#{ANSI[:RESET]} %s\n",
:AUTO_INDENT => true }
# Load the readline module.
IRB.conf[:USE_READLINE] = true
# Replace the irb(main):001:0 with a simple >>
IRB.conf[:PROMPT_MODE] = :SIMPLE_COLOR
# Tab completion
require 'irb/completion'
# Automatic indentation
IRB.conf[:AUTO_INDENT]=true
# Save History between irb sessions
require 'irb/ext/save-history'
IRB.conf[:SAVE_HISTORY] = 100
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb-save-history"
# Loading extensions of the console. This is wrapped
# because some might not be included in your Gemfile
# and errors will be raised.
def extend_console(name, care = true, required = true)
if care
require name if required
yield if block_given?
$console_extensions << "#{ANSI[:GREEN]}#{name}#{ANSI[:RESET]}"
else
$console_extensions << "#{ANSI[:GRAY]}#{name}#{ANSI[:RESET]}"
end
rescue LoadError
$console_extensions << "#{ANSI[:RED]}#{name}#{ANSI[:RESET]}"
end
$console_extensions = []
# Hirb is a mini view framework for console applications, designed
# to make formatting of ActiveRecord objects easier on the eyes
# http://tagaholic.me/2009/03/13/hirb-irb-on-the-good-stuff.html
extend_console 'hirb' do
Hirb.enable
extend Hirb::Console
end
# Fancy IRB is a gem that colorizes irb and adds other helpers
# See https://github.com/janlelis/fancy_irb
#extend_console 'fancy_irb' do
# FancyIrb.start
#end
# # Trick I like from Thoughtbot's Dan Croak to show log info in console
# # http://robots.thoughtbot.com/post/159806033/irb-script-console-tips
# # Log to STDOUT if in Rails (useful for showing the SQL query run)
# if ENV.include?('RAILS_ENV') && !Object.const_defined?('RAILS_DEFAULT_LOGGER')
# require 'logger'
# RAILS_DEFAULT_LOGGER = Logger.new(STDOUT)
# end
# The above trick doesn't work in Rails 3, but this does...
# ActiveRecord::Base.logger = Logger.new(STDOUT) if defined? Rails::Console
# When you're using Rails 2 console, show queries in the console
extend_console 'rails2', (ENV.include?('RAILS_ENV') && !Object.const_defined?('RAILS_DEFAULT_LOGGER')), false do
require 'logger'
RAILS_DEFAULT_LOGGER = Logger.new(STDOUT)
end
# When you're using Rails 3 console, show queries in the console
extend_console 'rails3', defined?(ActiveSupport::Notifications), false do
$odd_or_even_queries = false
ActiveSupport::Notifications.subscribe('sql.active_record') do |*args|
$odd_or_even_queries = !$odd_or_even_queries
color = $odd_or_even_queries ? ANSI[:CYAN] : ANSI[:MAGENTA]
event = ActiveSupport::Notifications::Event.new(*args)
time = "%.1fms" % event.duration
name = event.payload[:name]
sql = event.payload[:sql].gsub("\n", " ").squeeze(" ")
puts " #{ANSI[:UNDERLINE]}#{color}#{name} (#{time})#{ANSI[:RESET]} #{sql}"
end
end
# Simple methods in Rails console to get names of tables, columns(given a table), and models
def tables
ActiveRecord::Base.connection.tables.sort!
end
def columns(table)
ActiveRecord::Base.connection.columns(table).map(&:name).sort!
end
def models
tables.select{|t| t != "schema_migrations"}.map{|t| t.underscore.singularize.camelize.to_sym}
end
# Automating the creating of contexts in rails console
def set_context
require 'factory_bot_rails'
require './test/contexts'
include Contexts
puts 'Contexts enabled'
if Contexts.respond_to?(:create_all)
create_all
puts 'Contexts built'
end
end