2016-07-29T10:21:08+09:00

ModuleLoader のマニュアルのドラフト - ライブラリからライブラリをロードする

現在執筆中の ModuleLoader のマニュアルのドラフト第3弾です。

ライブラリが別のライブラリをロードする必要がある場合は、トップレベルのスクリプトと同様に、property の宣言で module コマンドを記述します。次のライブラリのサンプルでは、ロード時に "SimpleTextLib" が property SimpleTextLib に設定されます。

property SimpleTextLib : module

on replace_text_in_list(a_list, target, replacement)
set new_list to {}
repeat with a_text in a_list
tell SimpleTextLib
set end of new_list to replace_text(a_text, target, replacement)
end tell
end repeat
return new_list
end replace_text_in_list

次のサンプルは上のライブラリを読み込むトップレベルのスクリプトです。「property SimpleListLib : module 」でライブラリが必要であることを宣言して、「boot (module loader) for me」が実行されると、SimpleListLib がロードされるとともに、SimpleTextLib もロードされます。

property SimpleListLib : module
property _ : boot (module loader) for me

SimpleListLib's replace_text_in_list({"cain", "cine"}, "c", "p")
-- result : {"pain", "pine"}

どこかで「boot (module loader) for me」が実行されるまで、property SimpleTextLib にはライブラリが設定されません。ライブラリのテストの為にスクリプトを実行する為には、ライブラリの中で「boot (module loader) for me」を実行する必要があります。

次のように、run ハンドラで「boot (module loader) for me」を実行及びテストコードを記述すると良いと思います。

property SimpleTextLib : module

on replace_text_in_list(a_list, target, replacement)
set new_list to {}
repeat with a_text in a_list
tell SimpleTextLib
set end of new_list to replace_text(a_text, target, replacement)
end tell
end repeat
return new_list
end replace_text_in_list

on run -- test code
boot (module loader) for me
replace_text_in_list({"cain", "cine"}, "c", "p")
end run