Help us understand the problem. What is going on with this article?

[rails] rails generate xxx:install(カスタム Generator の作り方)

More than 5 years have passed since last update.

目的/動機

Rails用gemを作成した時に
作成したgem用のファイルを新規作成したり、
.gitignoreに特定のファイルを追加したかった。

他のgem(figaro, devise, rspec)だと
rails generate xxx:install
でそれらしいことをしているが、
調べても簡単に目的のページに辿り着けなかったので、ここにまとめた。

事前定義

自分が作ったgemのモジュール名を仮に Xxx としておく。

xxx:install の定義

xxx/lib/generators/xxx/install/install_generator.rb
require "rails"

module Xxx
  # Base=引数は自由に取れる NamedBase=引数を一つ取る
  class InstallGenerator < ::Rails::Generators::Base
    # インスタンスメソッドが順番に実行されるので、
    # test1
    # test2
    # test3
    # と標準出力に表示される
    def test1
      puts "test1"
    end

    def test2
      puts "test2"
    end

    def test3
      puts "test3"
    end
  end
end

Generatorの実行

Gemfile
gem "xxx"
bundle install
rails generate xxx:install

参考URL

rails でカスタム generator 作る話
Railsのカスタムgeneratorの、グループ分けをする

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした