2016-10-16 52 views
-2

我希望知道何時調用任何類或任何類的方法。可能嗎?是否有可能知道何時在Ruby或Ruby on Rails中調用模型?

UPDATE

對於爲例

class Example 

    def obeserver_method 
    p "this class has been called." 
    end 

end 

UPDATE

我試圖實現一個觀察者,基於在Ruby的觀察者模式,而是返回一個錯誤...

require "observer" 

class AAnyClass 
    extend Observable 

    changed 

end 

module AnObserver 

    extend self 

    def call constant 
    p "Constant #{constant} has been called." 
    end 

    def observe constant 
    constant.add_observer(self, call(constant)) 
    end 

end 
#=> returns "Constant AAnyClass has been called." 
AnObserver.observe AAnyClass 
# must return "Constant AAnyClass has been called." 
AAnyClass 
# must return "Constant AAnyClass has been called." 
AAnyClass 

這是錯誤:`add_observer': observer does not respond to `Constant AAnyClass has been called.' (NoMethodError)

UPDATE

新的實現

class AAnyClass 
    extend Observable 
    changed 
    notify_observers self 
end 

module AnObserver 

    extend self 

    def call constant 
    p "Constant #{constant} has been called." 
    end 

    def observe constant 
    constant.add_observer(self, :call) 
    end 

end 

不返回一個錯誤,但沒有看到。

+0

當你調用它,你知道吧:)添加更多信息的問題,指定它 –

+0

@AndreyDeineko我添加了信息。 – rplaurindo

+0

add_observer中的第二個參數是與將被調用的方法相對應的符號 - 您正在傳遞調用「call」的結果,這是字符串「Constant AAnyClass has called。」。你需要寫它'constant.add_observer(self,:call)'。但是這不會產生預期的結果。在你的類定義中,你調用'changed',這隻會在類實例化時觸發(與我其他評論中的原因相同) – sgbett

回答

1

您可以使用logger

class Example 
    def obeserver_method 
    # some logic 
    Rails.logger.info "this class has been called." 
    end 
end 

比,在你的日誌文件,你會看到有關方法調用的信息。

編輯

從評論:

Sorry, suppose that run rails c on terminal, and suppose that my project have a model Example with attributes, id , attr1 , attr2 ..., when I type Example and press Enter I would like that a observer run a routine, for example, connect to a specific database.

雖然我不很清楚的看到如何鍵入控制檯Example是一個很好的觸發器的一些行動,但無論如何,你基本上叫你的問題的工具:觀察員

退房Observable class這便於實施觀察者模式

+0

我認爲這並不完全。不存儲或顯示信息,它是執行例程。如果我調用'''Example'''方法必須知道。 – rplaurindo

+0

@rplaurindo 1.我不明白,你打算上課怎麼樣? 2.方法「知道」什麼? 3.期望的行爲是什麼? –

+0

@rplaurindo你想'obeserver_method'來存儲有關'Example'被引用次數的信息嗎? –

0

如果您正在尋找一種方法來跟蹤整個應用程序中的所有方法調用,而無需通過日誌文件解析或添加記錄器調用,則可以安裝Coverband。 Coverband會對您在Rails應用程序中調用的所有方法進行非常詳細的跟蹤。您可以通過源文件查看統計信息並向下鑽取。

coverband high level view

+0

雖然這會有所幫助,但它也會減慢代碼的執行速度。 –

+0

根據文檔,您可以配置設置以調整高位 「*允許採樣以避免每個請求上的性能開銷*」 –

+0

謝謝,但這不是我想要的。 – rplaurindo

0

定義一個類的方法,然後調用它...在控制檯

class Example 

    def self.instantiate 
    puts "Hello World!" 
    end 

    instantiate 

end 

測試...

$ rails c 
Loading development environment (Rails 3.2.22.4) 
2.2.5 :001 > Example 
Hello World! 
=> Example 
+0

謝謝,差不多了,但是試試兩次或更多次,「Hello World!」將只顯示一次。 :( – rplaurindo

+0

這是因爲一個類的定義是常量。請參閱http://guides.rubyonrails.org/autoloading_and_reloading_constants.html#class-and-module-definitions-are-constant-assignments類方法是「實例化」時調用對象被創建,這個對象的行爲就像是一個單例,後續的例子調用只會返回常量。 – sgbett

+0

你可以銷燬常量並重新定義它,例如'Object.send(:remove_const,:Example)'then'require'example' ',這將重新運行實例化類的方法,但似乎過多。 – sgbett