2015-01-14 46 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

相关问题