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

require 'rubygems'
require 'irb/completion'
require 'pp'
require 'what_methods'
unless (IRB.conf[:LOAD_MODULES] || []).join =~ /config\/environment/
  require 'active_support'
  require 'map_by_method'
  # require 'guessmethod'
end
# IRB.conf[:AUTO_INDENT]=true

class Object
  # Return a list of methods defined locally for a particular object.  Useful
  # for seeing what it does whilst losing all the guff that's implemented
  # by its parents (eg Object).
  def local_methods(obj = self)
    (obj.methods - obj.class.superclass.instance_methods).sort
  end
end

# From http://www.clarkware.com/cgi/blosxom/2007/09/03#ConsoleFindShortcut
# Creates shortcut methods for finding models.
def define_model_find_shortcuts
  model_files = Dir.glob("app/models/**/*.rb")
  table_names = model_files.map { |f| File.basename(f).split('.')[0..-2].join }
  table_names.each do |table_name|
    Object.instance_eval do
      define_method(table_name) do |*args|
        table_name.camelize.constantize.send(:find, *args)
      end
    end
  end
end

# Called when the irb session is ready, after
# the Rails goodies used above have been loaded.
# users(1) => User.find(1)
IRB.conf[:IRB_RC] = Proc.new { define_model_find_shortcuts }