Bringing the Thunder with Thor & Rails Generators for the Command Line
description
Transcript of Bringing the Thunder with Thor & Rails Generators for the Command Line
Why Thor?
• You need a CLI options parser
• It’s already installed (Rails 3)
• Useful file and directory operations
• Creating a generator
Flavors of Code Gen
~/projects/myproject(master)$
rails g model Product name:string
~/projects$
rails new myproject -m stock_project.rb
~/projects/myproject(master)$
petstore upgrade
Key Resources
• Thor::Actions - http://rdoc.info/github/wycats/thor/master/Thor/Actions.html
• Rails::Generators - http://api.rubyonrails.org/classes/Rails/Generators/Actions.html
• Writing/Overriding Generators - http://guides.rubyonrails.org/generators.html
Key Points
• All public methods are invoked in order of declaration
• Private methods are available in the template
• Can have inline or external templates
• ‘working’ directory is always the root of the project
Thor CLI Apps
• Generating code can be done without a generator
rails new MY_APP• Packaging as gem executable
• Don’t need to clutter list of generators in projects
• Example: A tool to upgrade to Rails 3
Application Templates
• Just a .rb file with thor/rails generator commands
• Used to configure new rails projects
• Can be local or remote