Skip to content

Instantly share code, notes, and snippets.

@tspangler
Created January 8, 2013 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tspangler/4485306 to your computer and use it in GitHub Desktop.
Save tspangler/4485306 to your computer and use it in GitHub Desktop.
Convert ugly CamelCase model schemas to lcase and underscores. Input: User(id: integer, FirstName: string, LastName: string, StreetAddress: string, City: string, State: string, Zip: string, PhoneNumber: string, EmailAddress: string, created_at: datetime, updated_at: datetime) Output: rails g scaffold User id:integer first_name:string last_name:s…
# encoding: utf-8
puts 'What should I name the model?'
model_name = gets
puts 'Enter model schema:'
sch = gets.match(/\((.*)\)/)[1]
puts 'Analyzing schema string...'
scaffold_string = 'rails g scaffold {replace_me}'
split_schema = sch.split(', ')
split_schema.each do |s|
scaffold_string.concat(' ').concat(s.gsub(': ', ':'))
end
# Convert CamelCase to lowercase+underscores
capitals = scaffold_string.scan(/\p{Lu}/)
capitals.each do |c|
scaffold_string.gsub!(c, '_' + c.downcase)
end
# Final gsub to get rid of underscores after spaces
scaffold_string.gsub!(' _', ' ').gsub!('{replace_me}', model_name)
puts 'How does this look?'
puts scaffold_string.gsub("\n", '')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment