我可以像包含模块一样直接调用实例方法吗?

时间:2015-01-14 10:03:03

标签: ruby-on-rails ruby class methods module

如果可能,如何调用或重建SomePluggin类直接在包含的模块中调用get_content方法,以减少get_content模块中的冗余Pluggin

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

1 个答案:

答案 0 :(得分:3)

您可以delegate get_content致电pluggin

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