getFlashHelper
各コントローラーにメソッドを追加するプラグインを開発しています。FlashHelper
このメソッドは、クラスのインスタンスを返す必要があります。
ただし、クラスのコンストラクターには、メソッドが呼び出されたコントローラーのインスタンスFlashHelper
を渡す必要があります。getFlashHelper
うまくいけば、次のコードが私が何をしているのかをもう少しうまく説明するでしょう
def doWithDynamicMethods = {ctx ->
application.controllerClasses*.metaClass*.getFlashHelper = {
def controllerInstance = delegate
// Avoid creating a new FlashHelper each time the 'flashHelper' property is accessed
if (!controllerInstance.metaClass.hasProperty('flashHelperInstance')) {
controllerInstance.metaClass.flashHelperInstance = new FlashHelper(controller: controllerInstance)
}
// Return the FlashHelper instance. There may be a simpler way, but I tried
// controllerInstance.metaClass.getMetaProperty('flashHelperInstance')
// and it didn't work
return controllerInstance.metaClass.getMetaProperty('flashHelperInstance').getter.invoke(controllerInstance, [] as Object[])
}
}
コードは機能しているように見えますが、もっと簡単な方法があるに違いないと感じずにはいられません。その最後の行は特に恐ろしいです。これを簡単にする方法はありますか?
ありがとう、ドン