bar_1

contents_map

2018年12月21日金曜日

Thor グループ Groups

グループ Groups

Thorには Thor::Group という特別なクラスがある. ひとつの Thor::Group に 定義されたもろもろのタスクはすべて, それらが定義された順に, ひとつのシーケンスとして実行される. [[実行 Invocations]] で使った例は Thor::Group を使って, このように書ける:
Thor has a special class called Thor::Group. All of the tasks defined in a Thor::Group are invoked together as a sequence, in the order that they were defined. The example from [[Invocations]] could be rewritten using
Thor::Group as:
myawesomegem/lib/mycounter_file.rb
require 'thor/group'

module MyAwesomeGem                 # 訳注: 名前空間のためのmodule
  class MyCounter < Thor::Group     # 訳注: ThorではなくThor::Group
    desc "Prints 1 2 3"

    def one
      puts 1
    end

    def two
      puts 2
    end

    def three
      puts 3
    end
  end
end
実行すると:
When invoked:
thor my_awesome_gem:my_counter
# prints "1 2 3"

実行ファイルexecutableのコマンド・リストに, サブクラスThor::Groupを追加するためには, 一番最初に, あなたはこの登録をしなければならないという点に, 注意すること.
Note that in order to add the Thor::Group subclass to the commands list of your executable, you have to register it first.
実行ファイルを利用するには, 説明が [[実行ファイルの作成 Making An Executable]] にあり, たとえばこのようなファイルがあるとする:
Building on the executables explanation found in [[Making An Executable]], let’s say that you have a file:
myawesomegem/bin/command
#!/usr/bin/env ruby
require File.expand_path('lib/command', Dir.pwd)
MyAwesomeGem::MyCommand.start
そうして, コマンドの実装を含むファイルは以下:
And then, the file containing the actual command:
myawesomegem/lib/command.rb
require "thor"
module MyAwesomeGem
  class MyCommand < Thor
    desc "foo", "Prints foo"
    def foo
      puts "foo"
    end
  end
end
実行ファイルをテストする:
Test executable file:
cd myawesomegem
chmod a+x bin/command # make permission
bin/command foo
クラス MyCounterサブコマンドとして 使えるようにするには, Thor.registerメソッド をコールする必要がある:
To make MyCounter available as a subcommand, you need to call the register method:
myawesomegem/lib/command.rb
require "thor"
require File.expand_path("lib/mycounter_file", Dir.pwd)
module MyAwesomeGem
  class MyCommand < Thor
    desc "foo", "Prints foo"
    def foo
      puts "foo"
    end

    # register(class_name, subcommand_alias, usage_list_string, description_string)
    register(MyAwesomeGem::MyCounter, "counter", "counter", "Prints some numbers in sequence")
  end
end
今, クラス MyAwesomeGem::MyCounter < Thor::Group は, mycommand の中のサブタスクとして現れ, そのエイリアスは counter となり, 使い方は:
Now the class MyCounter < Thor::Group will appear as a sub-task within mycommand with the alias counter, usage:
bin/command
# Commands:
#  command counter         # Prints numbers in sequence
#  command foo             # Prints foo

bin/command counter
#=> 1 2 3

注意: Thor::Group を使う場合は, あなたが (descメソッドで) 用意した説明書きは, それぞれのタスク毎にひとつの説明が用意されるのではなく, クラスそのものの説明書きとなる.
Note: when using Thor::Group, the description you provide (using the method desc) is for the entire class, as opposed to providing a description for each task.
また, Thor::Group はもろもろの引数 arguments とオプションをThorのタスクとしてパースできる:
Also, Thor::Group can parse arguments and options as Thor tasks:
class Counter < Thor::Group
  # number will be available as attr_accessor
  argument :number, :type => :numeric, :desc => "The number to start counting"
  desc "Prints the 'number' given upto 'number+2'"

  def one
    puts number + 0
  end

  def two
    puts number + 1
  end

  def three
    puts number + 2
  end
end
上記counterはひとつのパラメータを期待するものであり, 下記のような出力が得られる:
The counter above expects one parameter and has the following outputs:
thor counter 5
# Prints "5 6 7"

thor counter 11
# Prints "11 12 13"
あなたはまた, もろもろのオプションを Thor::Group に与えることができるが, method_optionmethod_options の代わりに, class_optionclass_options を使ってもいい. argument メソッドと class_optionsメソッドは両方とも, Thorクラスにも使用可能だ.
You can also give options to Thor::Group, but instead of using method_option
and method_options, you should use class_option and class_options.
Both argument and class_options methods are available to Thor class as well.
Thor::Group は, 定義された順に実行される5,6個のステップで定義できるので, ジェネレータを作るのにすぐれたツールのひとつだ (Thor::Group は Rails 4.x のジェネレータに使われているツールだ. 訳注: Rails 5.xでもジェネレータとして使われている痕跡が残っている)
Thor::Group is a great tool to create generators, since you can define several steps which are invoked in the order they are defined (Thor::Group is the tool used for generators in Rails 4.x).

0 件のコメント:

コメントを投稿

何かありましたら、どうぞ: