演習 5 :最初の Lookup クラス
チュートリアル本編の演習の説明に戻るには、ここをクリックします。
-
VS Code - ObjectScript を使用し、新規クラスを作成します。パッケージ名に ObjectScript、クラス名に Lookup1 を使用します。以下のコードが表示されます。
Class ObjectScript.Lookup1 { }
-
{} 内に Main() メソッドを記述します。
Class ObjectScript.Lookup1 { /// main loop section, dispatch to different methods based on user input ClassMethod Main() { while ..GetInput(.type, .search) { if (type = "help") { do ..Help() } elseif (type = "dob") { do ..DOB(search) } } } }
-
次に、GetInput() メソッドを記述します。
Class ObjectScript.Lookup1 { /// prompt user for a lookup string, return search type and search string ClassMethod GetInput(output type as %String, output search as %String) as %Boolean { read !, "Lookup: ", lookup return:(lookup = "") 0 // user entered nothing so return FALSE if (lookup = "?") { set type = "help", search = "" } elseif (##class(ObjectScript.DataEntry4).ValidDOB(lookup, .convdate)) { set type = "dob", search = convdate } else { set (type, search) = "" } return 1 } }
-
次に、Help() メソッドを記述します。
Class ObjectScript.Lookup1 { /// display lookup options ClassMethod Help() { write !, "You can enter:", !?10, "* date of birth", ! } }
-
次に、内部 DOB を引数として取る DOB() メソッドを記述します。これはまず ^PersonI("DOB", intdob) に対して $Data を使用し、誕生日の内部形式がグローバルに存在するかどうかを確認します。$Data の前の一重引用符 (否定) に注意してください。一致する値がある場合、$Order ループは、該当する誕生日の ID をすべて検索します。ループは先頭 (set id = "") から始まり、最後 (quit:(id = "")) まで続きます。また、For を使用して一致する値の個数を数えます。
Class ObjectScript.Lookup1 { /// exact date of birth lookup ClassMethod DOB(intdob as %Date) { // is the date of birth in the index? if '$data(^PersonI("DOB", intdob) ) { write "...no matches" quit } write "...finding birthday matches" // loop through IDs, and number them set id = "" for count = 1:1 { set id = $order(^PersonI("DOB", intdob, id)) quit:(id = "") write !, count, ") " do ..DisplayLine(id) } } }
-
最後に、DisplayLine() メソッドを記述します。これは、ObjectScript.DataEntry4 の Display() と同様です。
Class ObjectScript.Lookup1 { //// given an ID, retrieve data and write it on a line ClassMethod DisplayLine(id as %Integer) { set $listbuild(name, phone, intdob) = ^PersonD(id) /* the line above is equivalent to set answers = ^PersonD(id), name = $list(answers, 1), phone = $list(answers, 2), intdob = $list(answers, 3) */ write name, ?20, phone, ?35, $zdate(intdob, 2) } }
-
[保存] ボタン、[コンパイル] ボタンをクリックします。
-
ターミナルを開始し、do ##class(ObjectScript.Lookup1).Main() と入力してメソッドを実行します。