Skip to content

Instantly share code, notes, and snippets.

@jferris
Created June 29, 2009 21:28
Show Gist options
  • Save jferris/137824 to your computer and use it in GitHub Desktop.
Save jferris/137824 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'fileutils'
def usage
puts "Usage: schema -l [partial_model_name] OR schema <model_name>"
end
if ARGV.first == '-l'
def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
if first_letter_in_uppercase
lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
else
lower_case_and_underscored_word.first.downcase + camelize(lower_case_and_underscored_word)[1..-1]
end
end
model_names = []
FileUtils.cd(FileUtils.pwd + '/app/models') do
model_names = Dir['**/*.rb'].collect {|file| camelize(file.sub!(/\.rb$/, '')) }
end
if ARGV.last != ARGV.first
model_names = model_names.select {|model| model =~ /^#{ARGV.last}/ }
end
puts model_names.join("\n")
elsif ARGV.first
require FileUtils.pwd + '/config/environment'
model_name = ARGV.first
begin
model = model_name.constantize
table = model.table_name
puts model.columns.collect {|column| "#{column.name} #{column.sql_type}" }.join("\n")
rescue
puts "Unknown model: #{model_name}"
usage
end
else
usage
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment