bar_1

contents_map

2018年12月21日金曜日

Thor 実行 Invocations

実行 Invocations

Thorにはまた実行依存性システムinvocation-dependency systemもあり, この仕組みで, タスクの発動を一度だけにすることもできる. たとえば:
Thor comes with an invocation-dependency system as well, that allows a task to be invoked only once. For example:
class Counter < Thor
  desc "one", "Prints 1 2 3"
  def one
    puts 1
    invoke :two     # 訳注: タスクtwoの中のthreeは実行されない!
    invoke :three
  end

  desc "two", "Prints 2 3"
  def two
    puts 2
    invoke :three
  end

  desc "three", "Prints 3"
  def three
    puts 3
  end
end
タスクoneを実行すると:
When invoking the task one:
thor counter:one
その出力は
The output is
1
2
3
となり, これはつまり, タスク three一度だけ実行されるということだ. 他のクラスからでも, この機能を使ったタスクの実行はできるので, Thorクラスについて ドキュメント を確認すること.
which means that the three task was invoked only once.
You can even invoke tasks from another class, so be sure to check the
documentation for the Thor class.
もろもろの実行invocationsは, 同一のオブジェクトを共有しないことに気づくだろう. すなわち, Thorはタスク one を実行するために Counter を一度インスタンス化し, その次に, タスク two の実行にもうひとつ, タスク three を実行するためにまたひとつ Counter をインスタンス化する. このことはもろもろのオプションと引数を, もう一度パースし直すことができるということだ. たとえば, もし twothree がそれぞれ異なったもろもろのオプションを持ち, 両方のオプション群がコマンドラインに与えられたような場合, invoke をコールすることで, それぞれのタスクに従って, タスク毎に毎回それらがパースされ, 使用される.
Notice invocations do not share the same object. i.e, Thor will instantiate Counter once to invoke the task one, then, it instantiates another to invoke the task two and another for task three. This allows options and arguments to be parsed again. For example, if two and three have different options and both of them were given to the command line, calling invoke enables them to be parsed each time and used accordingly by each task.

Thor メソッドオプション Method Options

メソッドオプション Method Options

Thorでは, タスクにいろいろなオプションを指定できる; これには, もろもろのオプションのHash変数を与える method_options を使うか, 個別のオプションについてより詳しく指定する method_option を使う. これらのメソッドの結果にはHash変数 options 経由でアクセスできる.
Thor allows you to specify options for its tasks, using method_options to supply a hash of options, or method_option to provide more detail about an individual option. You can access these options via the options hash.

可能なオプション Available Options

  • :aliases
    指定しようとしているオプションのエイリアスのリスト. 通常, オプションの省略形を与えるのに使うことになるだろう.
    A list of aliases for this option. Typically, you would use aliases to provide short versions of the option.
  • :banner
    これはオプションの短い説明で, 使い方の説明を表示する際に表示される. デフォルトでは, これはフラグの大文字バージョンである (from=FROM).
    The short description of the option, printed out in the usage description. By default, this is the upcase version of the flag (from=FROM).
  • :default
    設定しようとしているオプションが指定されなかった場合の, デフォルト値. :defaultをもつオプションは, :requiredは指定できない (訳注: 「デフォルト値 Default Values」の節に矛盾した記述がある. また、両方指定したときエラーとはならないようだ).
    The default value of this option if it is not provided. An option cannot be both :required and have a :default.
  • :lazy_default
    CLIオプションが値なしに渡された場合だけに使われるデフォルト値.
    A default that is only passed if the cli option is passed without a value.
  • :desc
    オプションの説明. CLIの help hello を使って, コマンドの使い方を全部表示させたときに, この説明がオプションの隣に表示される.
    A description for the option. When printing out full usage for a command using cli help hello, this description will appear next to the option.
  • :required
    必須オプションであることを示す.
    Indicates that an option is required
  • :type
    :string, :hash, :array, :numeric, もしくは :boolean (詳しくは後述)
    :string, :hash, :array, :numeric, or :boolean (see below for more details)
  • :enum
    オプションに許される値のリスト.
    A list of allowed values for this option.

method_options のタイプ Types for method_options

  • :boolean--option or --option=true としてパースされる
  • :string--option=VALUE としてパースされる
  • :numeric--option=N としてパースされる
  • :array--option=one two three としてパースされる
  • :hash--option=name:string age:integer としてパースされる
  • :boolean is parsed as --option or --option=true
  • :string is parsed as --option=VALUE
  • :numeric is parsed as --option=N
  • :array is parsed as --option=one two three
  • :hash is parsed as --option=name:string age:integer

デフォルト値 Default Values

method_option はデフォルト値を指定できる. たとえば:
method_option allows a default value to be given. Example:
method_option :value, :default => "some value"
#=> Creates a string option with a default value of "some value"
#=> デフォルト値が "some value" であるような 文字列タイプのオプション: --valueを作る.
また, (訳注: methodoption_sメソッドを使って, )オプション名に対してある値をデフォルトとして指定できる. たとえば:
You can also specify the default as a value to the option name. Examples:
# 訳注: 以下の例は, method_option*S*であることに注意.

method_options :force => false
#=> デフォルト値がfalseであるbooleanタイプのオプション: --forceを作る
#=> Creates a boolean option with default value false

method_options :aliases => "bar"
#=> エイリアス'bar'を設定する
#=> Creates an alias 'bar'

method_options :threshold => 3.0
#=> デフォルト値が3.0である数値タイプのオプション: --thresholdを作る
#=> Creates a numeric option with default value 3.0
あなたは(訳注: method_optionSを使っている場合) :option => :required を与えることで, オプション —option を必須のものとして設定することができる. このときのオプションのタイプは文字列とみなされる. (訳注: このときは—optionが必須のものであるという設定しかできないが)もし, オプションとして, 必須であり, かつデフォルト値をもったHash変数がほしい場合は, method_option を使った, より宣言的なスタイルを使うことができる (訳注: 下記のコードは期待した動作をしない. :required=>trueは無視されthor app nameは—attributes無しでもエラーなしで実行される. thor version 0.19.4で確認):
You can also supply :option => :required to mark an option as required. The
type is assumed to be string. If you want a required hash with default values
as option, you can use method_option which uses a more declarative style:
method_option :attributes, :type => :hash, :default => {}, :required => true
すべての引数argumentsは, no- もしくは skip- の変形引数を与えることで, nilに セットできる (必須の引数を除く). たとえば:
All arguments can be set to nil (except required arguments), by supplying a no- or
skip-variant. For example:
thor app name --no-attributes
以前のバージョンではオプションのエイリアスは自動で生成されたが, 今は, それらは明示的でなければならない. 下記のように, method_optionSを使った簡略スタイルでもmethod_optionを使った宣言的なスタイルでも, どちらででも与えることが出来る:
In previous versions, aliases for options were created automatically, but now
they should be explicit. You can supply aliases in both short and declarative
styles:
method_options %w( force -f ) => :boolean
または:
Or:
method_option :force, :type => :boolean, :aliases => "-f"
エイリアスはお望みの通りいくつでも与えることができる.
You can supply as many aliases as you want.
注意: Thorは1ダッシュ-1文字のオプション群という ひとつの 規則 に従う. よって “-something”のようなエイリアスはパースされない; “—something” もしくは “-s” を代わりに使うこと.
Note: Thor follows a convention of one-dash-one-letter options. Thus aliases like “-something” wont be parsed; use either “\—something” or “-s” instead.
注意: Thor 0.9.0 にあった:optionalタイプは不許可になった. 代わりに :string もしくは :boolean を使うこと.
NOTE: Type :optional available in Thor 0.9.0 was deprecated. Use :string or :boolean instead.

Thor はじめに Getting Started

始めるにあたり, thorについて知らなければならない2,3のことがある. 例えば, タスクをロードするためのファイル命名規則や, 通常はコマンドラインで thor コマンドによってあなたのスクリプトを呼び出すことである (これは他にも方法がある)
There are a few things that you need to know about thor to get started. For example, it uses a file name convention to load tasks, and you typically call your scripts via the thor command line, though this is not the only option.

ファイル名 File Names

thorタスクをひとつ作るには, 最初に, 拡張子 .thor をもつファイル (Thorfile というファイルでもよい) がひとつ必要となる. このファイルには, thorが提供する機能を含んだ任意の標準のRubyコードを, 入れることができる. thor コマンドライン・ツールが実行されると, もろもろの .thor ファイルを探し, ロードして, あなたが作成したタスク群が使用できるようになる.
To create a thor task, the first thing you need is a file ending with the .thor extension (a file named Thorfile works as well). This file can contain any standard ruby code, including the functionality provided by thor. When the thor command line tool is run, it will look for .thor files and load them up, making your tasks available.

シンプルな例 A Simple Example

test.thor というファイルを作って, 以下のコードを書く:
Create a file named test.thor and place the following code in it
class Test < Thor
  desc "example", "an example task"
  def example
    puts "I'm a thor task!"
  end
end
あなたは一旦これを作りさえすれば, コマンドラインから thor list を実行することで, 以下のような出力が得られる:
Once you have this created, you can run thor list from the command line and you should see the following output:
test
----
thor test:example  # an example task
さあ, コマンドラインから thor test:example を実行しよう. I'm a thor task! という出力が得られるはずだ.
Now run thor test:example from the command line. You should see I'm a thor task! output to the command line.

サンプルを深く理解する Breaking Down The Sample

このタスクには, 何が起きているかを理解するために試せる, 5,6個のパーツがある.
There are several parts of this task that can be examined to understand what is happening.
最初に, クラス名はタスクの名前空間として使われる. Thorは クラス名を名前空間に変えるために, もろもろのRuby標準命名規則を使う. 例えば, あなたがクラス名を FooBar に変えた場合, タスクはこのとき foo_bar:example としてリストされる.
First, the class name is used as the namespace for the task. Thor uses some standard ruby conventions to turn the class name into a namespace. If you change the class name to FooBar, for example, your task would now be listed as foo_bar:example.
次に, desc メソッドが使われているが, これはわれわれが作成しようとしているタスクを記述するのためのものだ. descメソッドには ふたつのパラメータ が ある. 第1のパラメータは 定義するタスク(メソッド)の名称を明示するためのもので, 任意のパラメータ群をそのメソッドに与えられる. 第2のパラメータは プレーンテキストで, このタスクが何をするものなのか説明するためのものだ.
Next, the desc method call is used to describe the task that we are creating. There are two parameters to the desc method. The first parameter should state the name of the task (the method) and any parameters that are provided to the method. The second option should be a plain text description of what this task does.
最後は, example メソッドそのものだ. Thorはこのメソッド名を実際のタスク名として使用する. それゆえ, exampleメソッドを widget にリネームした場合, タスクは test:widget としてリストされることになる.
Last, the example method itself. Thor uses this method name as the actual task name. Therefore, when you rename the example method to widget, you end up with a task listing of test:widget.

タスクのパラメータ Task Parameters

以下のthorタスクは, パラメータをひとつメソッドに加えることによって, タスクにパラメータをひとつ定義する:
The following thor task defines one parameter on the task by adding a parameter to the method:
class Test < Thor
  desc "example FILE", "an example task that does something with a file"
  def example(file)
    puts "You supplied the file: #{file}"
  end
end
descの第1パラメータを’FILE’という語を含むものにアップデートしたことに, 注意してほしい. 前述のように, タスクの記述にはタスク・パラメータを任意に含めてよい. これはエンドユーザが使いやすいようにヘルプ・テキストを提供するものだ.
Note that the description has been updated to include the word ‘FILE’ in the first parameter. As stated previously, you should include the name of any required task parameters in the task description. This is used to produce help text that is useful to the end user.
thor help test:example を実行してtest:exampleタスクのヘルプテキストを見てみると, 以下のような出力を見れるだろう:
Run thor help test:example to see the help text for the test:example task and you will see the following output:
Usage:
  thor test:example FILE

an example task that does something with a file
これはそのタスクにFILEパラメータが必要であることを示す.
This indicated the requirement of a FILE parameter for the task.
thor test:example my_file.rb でタスク自体を実行した場合, You supplied the file: my_file.rb と目にするはずだ.
Now when you run the task itself, with thor test:example my_file.rb you should see You supplied the file: my_file.rb.
もしファイル名を与えずにスクリプトを実行すれば, ひとつ与える必要があるという "example" was called incorrectly. Call as "thor test:example FILE". のメッセージを目にするだろう.
If you run the script without a file supplied, you’ll see a message saying you need to supply one: "example" was called incorrectly. Call as "thor test:example FILE".

タスクのオプション Options For A Task

Thorはまた, ひとつのタスクに対して, もろもろの名前付けしたオプションを与えることもできる. いくつかのメソッドがこれを行っており, より詳細については [[method options]] のWikiページを参照してほしい.
Thor also lets you provide named options for a task. There are several methods of doing this, and more detail can be found on the [[method options]] wiki page.
あなたのtest.thorファイルに以下を書いてみよ:
Put the following into your test.thor file:
class Test < Thor
  desc "example FILE", "an example task"
  method_option :delete, :aliases => "-d", :desc => "Delete the file after parsing it"
  def example(file)
    puts "You supplied the file: #{file}"
    delete_file = options[:delete]
    if delete_file
      puts "You specified that you would like to delete #{file}"
    else
      puts "You do not want to delete #{file}"
    end
  end
end
この例では, 5,6個のパラメータとともに method_option がひとつ指定されている. 第1のパラメータはオプションのフルネームだ. これはコマンドライン上で —オプションひとつに変換される. このケースでは --delete だ. :aliases オプションは, このオプションのために, ひとつかそれ以上の省略形のエイリアス群を提供する. このケースでは -d が省略形として提供される. 最後に :desc でこのオプションの説明を与えた.
In this example, a method_option has been supplied with several parameters. The first parameter is the full name of the option. this is translated into a — option on the command line. In this case, --delete. The :aliases option allows you to provide one or more short-hand aliases for the option. In this case, -d has been provided as a short-hand alias. And last, a description of the option has been provided.
thor help test:example を実行すると, 以下の出力がみれる:
Run thor help test:example and you will see the following output:
Usage:
  thor test:example FILE

Options:
  -d, [--delete=DELETE]  # Delete the file after parsing it

an example task
今, このタスクを —delete オプション有りでも無しでも実行できる.
Now you can run the task with or without the delete option.
thor test:example my_file.rb を実行すると, 以下のようになる:
Run thor test:example my_file.rb, you will see this output:
You supplied the file: my_file.rb
You do not want to delete my_file.rb
thor test:example my_file.rb --delete もしくは thor test:example my_file.rb -d で実行してみると:
Run thor test:example my_file.rb --delete or thor test:example my_file.rb -d and you will see:
You supplied the file: my_file.rb
You specified that you would like to delete my_file.rb
作成したタスク・メソッドひとつにつき, 多くのメソッド・オプションを必要なだけ指定できる. これらをメソッド名に対して積み上げていくだけで, タスク内に現れるだろう.
You can specify as many method options as you need, for a given task method. Just keep piling them on to the method name and they will show up in the task.

ハサミを持って突っ走る Running With Scissors

ここでの諸例は単純で不自然だったが, 今はなにかしら, 実際のthorタスクの機能を使ってみることができるだろう. thorのさまざまなオプションや能力のドキュメントについては, 他のwikiページ (訳注: githubにあるThorのWikiのこと. この訳文群のこと) もチェックしてみること.
Though the examples here are simple and contrived, you should be able to get started with some real functionality for your thor tasks, now. Be sure to check the other wiki pages for documentation on the various options and capabilities of thor.

2018年9月11日火曜日

中小企業向けサイバーセキュリティ対策の極意 (無料)


東京都産業労働局が, 『中小企業向けサイバーセキュリティ対策の極意』というPDF資料を配布している. 全193ページ, 5章からなる構成で, 無料である.
ページ下部のリンクからダウンロードできる.

2018年8月29日水曜日

"SafariBookmarksSyncAgentが予期しない理由で終了しました" の修正方法

いつのタイミングか忘れてしまったが, Safari を起動するとエラーメッセージが出るようになってしまってい, ブクマ登録もできなくなってしまった. これは ~/Library/Safari/Bookmarks.plist を作り直すことで, 直すことができる.
この記事では, 当該現象と原因, 修正方法を述べる.

2018年3月19日月曜日

国内・海外の証券会社とAPI公開状況 (2013年版)

下記は2013年に調べたものなので一部情報が古いかもしれない. 「海外」としたが, 時間的な制約から, 英語圏しか調査は行っていない.