2015-01-14 17 views
0

如果可以的話,如何調用或重建SomePluggin類直接在包含模塊中調用get_content方法,以減少模塊中多餘的get_content模塊?我可以直接調用像包含模塊中的實例方法嗎?

module Cms 
    class SomePluggin 
    def get_content 
     puts "please call me directly" 
    end 
    end 

    module Pluggin 
    extend ActiveSupport::Concern 

    included do 
     after_initialize :pluggin 
    end 

    attr_writer :pluggin 

    def pluggin 
     @pluggin ||= SomePluggin.new 
    end 

    def get_content # Is there any possibility to do this directly? 
     pluggin.get_content 
    end 
    end 
end 

回答

3

您可以delegateget_content調用pluggin

module Pluggin 
    delegate :get_content, to: :pluggin 
    # ... 
end 
+0

最好的,謝謝! – luzny

相關問題