forked from ehrenmurdick/config
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathirbrc
195 lines (169 loc) · 4.26 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
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
# WHERE ARE WE?
script_console_running = (ENV.include?('RAILS_ENV') and IRB.conf[:LOAD_MODULES].include?('console_with_helpers'))
rails_running = (ENV.include?('RAILS_ENV') and !IRB.conf[:LOAD_MODULES].include?('console_with_helpers'))
irb_standalone_running = (!script_console_running and !rails_running)
MODEL_NAMES = %w{User Event Network}
FAKE_FALSE = ENV["FALSE"] || "kumquat"
FAKE_TRUE = ENV["TRUE"] || "banana"
require 'rubygems'
require 'irb/completion'
def working! indicator = :spinner, iterations = nil
case indicator
when :spinner
print " " unless @_status
@_status ||= ['/', '-', '\\']
@index ||= 0
@index += 1 if @index < @_status.size
@index = 0 if @index == @_status.size
print "\b#{@_status[@index]}"
when :dots
print '.'
when :bar
raise ArgumentError, "Progress Bar requires number of iterations! (second argument)" if iterations.nil?
unless @one_bar
@total_bars = iterations
@one_bar = 1
while @total_bars > 30
@total_bars /= 2
@one_bar *= 2
end
while @total_bars < 30
@total_bars *= 2
@one_bar /= 2.0
end
@bars_to_print = 0
@current = 0
end
@current += 1
if @current % @one_bar == 0
@bars_to_print += 1 / @one_bar if @one_bar < 1
@bars_to_print += 1 if @one_bar >= 1
end rescue false
bars = @bars_to_print
print "\r|#{'=' * bars}#{' ' * (@total_bars - bars.truncate)}|"
if @current == iterations
@one_bar = nil
print "\n"
end
end
end
# Setup permanent history.
HISTFILE = "~/.irb_history"
MAXHISTSIZE = 500
begin
histfile = File::expand_path(HISTFILE)
if File::exists?(histfile)
lines = IO::readlines(histfile).collect { |line| line.chomp }
puts "Read #{lines.nitems} saved history commands from '#{histfile}'." if $VERBOSE
Readline::HISTORY.push(*lines)
else
puts "History file '#{histfile}' was empty or non-existant." if $VERBOSE
end
Kernel::at_exit do
lines = Readline::HISTORY.to_a.reverse.uniq.reverse
lines = lines[-MAXHISTSIZE, MAXHISTSIZE] if lines.nitems > MAXHISTSIZE
puts "Saving #{lines.length} history lines to '#{histfile}'." if $VERBOSE
File::open(histfile, File::WRONLY|File::CREAT|File::TRUNC) { |io| io.puts lines.join("\n") }
end
end
@can_be_numeric = Object.new
def @can_be_numeric.===(other)
other.to_i if other =~ /^\d*$/ or Numeric === other
end
def m obj
(obj.methods - obj.class.instance_methods).sort
end
def i(obj)
(obj.class.instance_methods - obj.class.superclass.instance_methods).sort
end
module Finder
def columns_which_are(t)
types = nil
case t
when :string
types = [:string, :text]
when :numeric
types = [:integer]
when :datetime
types = [:date, :time, :datetime]
end
columns.find_all {|c| types.include?(c.type) }.map(&:name)
end
end
def run_finders(model, colgroup, arg)
result = nil
colgroup.each do |col|
begin
result = model.send("find_by_#{col}", arg)
rescue NoMethodError
end
break if result
end
result
end
MODEL_NAMES.each do |model_name|
if defined?(model_name)
src =<<-END
def #{model_name}(arg)
#{model_name}.extend(Finder) unless #{model_name}.included_modules.include?(Finder)
result = nil
case arg
when @can_be_numeric
run_finders(#{model_name}, #{model_name}.columns_which_are(:numeric), arg)
when String
run_finders(#{model_name}, #{model_name}.columns_which_are(:string), arg)
end
end
END
eval src
end
end
class Object
def meow?
!blank?
%x{ say meow } if !blank?
end
end
class Numeric
def meows
self.times do
%x{ say meow }
end
end
end
class Time
class << self
def meow
now
end
end
end
class Object
define_method(FAKE_TRUE.to_sym) do
true
end
define_method(FAKE_FALSE.to_sym) do
false
end
end
class TrueClass
def to_s
FAKE_TRUE
end
end
class FalseClass
def to_s
FAKE_FALSE
end
end
unless rails_running
require 'pp'
require 'irb/completion'
require 'tempfile'
require 'yaml'
# Redirect the Rails Logger to the STDOUT when in script/console
if script_console_running
require 'logger'
Object.const_set(:RAILS_DEFAULT_LOGGER, Logger.new(STDOUT))
end
end