2

MiniProfilerをRubyに移植しており、Railsビューとパーシャルの自動計測を追加したかったのです。なぜこのメソッドは、ActionView :: Template:render?

私はexisting instrumentationのサポートについて認識していますが、理想的には「開始」と「停止」イベントを取得したいと考えています。通知をサポートしていない以前のバージョンのRailsもサポートしたいと考えています。

私は短いインスツルメンタをハッキングし、呼び出し後前でレンダリングフックテスト:

def prof(klass, method) 
    with_profiling = (method.to_s + "_with_profiling").intern 
    without_profiling = (method.to_s + "_without_profiling").intern 

    klass.send :alias_method, without_profiling, method 
    klass.send :define_method, with_profiling do |*args, &orig| 
    puts "before #{method} #{args}" 
    self.send without_profiling, *args, &orig 
    puts "after #{method}" 
    end 
    klass.send :alias_method, method, with_profiling 
end 

prof ActionView::Template, :render 

しかし、これが起動したら、renderが正しく計測されません。特に、これは、パーシャルの一部に動作しますが、と爆発:これと間違っている何

ActionView ::テンプレート::エラー(NilClass nilのための未定義のメソッド `html_safe」)メソッドフック?あなたは通常nilであるあなたの最終putsの結果を返すメソッドを再定義しました

+0

'(method.to_s + "_with_profiling")を有利にする何らかの理由'オーバーintern': "#{方法} _with_profiling" '? – tadman

+0

本当に、私はそれをきれいにするでしょう –

答えて

2

:彼らはこの問題には脆弱ではありませんので、メソッドをフックするための適切堅牢な方法は何ですか(prof klass, method些細なAPIを維持します)。修正かもしれません:。

klass.send :define_method, with_profiling do |*args, &orig| 
    puts "before #{method} #{args}" 
    result = self.send without_profiling, *args, &rig 
    puts "after #{method}" 

    result 
end 
+0

うわー...私はそれを逃したと信じることができない、それは私にルビーからの休憩を取ることを学ぶ:) –

+0

今私が尋ねる必要があるフォローアップの質問は、フックからの元気なビュー名 –

関連する問題