Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
/thorPublic

Generators

Yuji Hanamura edited this pageAug 20, 2018 ·4 revisions

A great use for Thor is creating custom generators. CombiningThor::Group,Thor::Actions and ERB templates makes this very easy. Here is an example:

classNewgem <Thor::GroupincludeThor::Actions# Define arguments and optionsargument:nameclass_option:test_framework,:default=>:test_unitdefself.source_rootFile.dirname(__FILE__)enddefcreate_lib_filetemplate('templates/newgem.tt',"#{name}/lib/#{name}.rb")enddefcreate_test_filetest=options[:test_framework] =="rspec" ?:spec ::testcreate_file"#{name}/#{test}/#{name}_#{test}.rb"enddefcopy_licenceifyes?("Use MIT license?")# Make a copy of the MITLICENSE file at the source rootcopy_file"MITLICENSE","#{name}/MITLICENSE"elsesay"Shame on you…",:redendendend

Doing athor -T will show how to run our generator. It should read:thor newgem NAME. This shows that we have to supply a NAMEargument for our generator to run.

Thecreate_lib_file uses an ERB template. This is what it looks like:

class <%=name.capitalize %>end

The arguments that you set in your generator will automatically be passed inwhentemplate gets called. Be sure to read thedocumentation for more options.

Running the generator withthor newgem devise will create two files: "devise/lib/devise.rb", and "devise/test/devise_test.rb". The user will then be asked (via a prompt by theyes? method) whether or not they would like to copy the MIT License. If you want to change the test framework, you can add the option:thor newgem devise --test-framework=rspec.

This will generate two files: "devise/lib/devise.rb" and "devise/spec/devise_spec.rb".

Clone this wiki locally

[8]ページ先頭

©2009-2025 Movatter.jp