Thor Presentation - Meetup

tunisianbromidrosisInternet και Εφαρμογές Web

5 Φεβ 2013 (πριν από 4 χρόνια και 9 μήνες)

176 εμφανίσεις

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/Gener
ators/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

Product Generator

Templates

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::Actions

Rails::

Generators::
Actions

Stupid Generator Tricks

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

Gemfile & gemspec

Hello World

Showing off

Multiple Commands

Running Thor Commands

Application Templates


Just a .rb file with thor/rails generator
commands


Used to configure new rails projects


Can be local or remote

Sample Template

Easier kickstarting

Questions?