260 likes | 470 Views
Bringing the Thunder with Thor & Rails Generators for the Command Line. *Boom*. Actual Summary. Thor (the ruby framework) Rails Generators Application Templates. What is Thor?. *In practice, not so much. Why Thor?. You need a CLI options parser It’s already installed (Rails 3)
E N D
Bringing the Thunder with Thor & Rails Generators for the Command Line *Boom*
Actual Summary • Thor (the ruby framework) • Rails Generators • Application Templates
What is Thor? *In practice, not so much.
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
Using Generators rails generate model Turtle name:string
Rolling your own rails g generator product
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
Making a Gem http://asciicasts.com/episodes/245-new-gem-with-bundler
Application Templates • Just a .rb file with thor/rails generator commands • Used to configure new rails projects • Can be local or remote