如果可能,如何调用或重建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
答案 0 :(得分:3)
您可以delegate get_content
致电pluggin
:
module Pluggin
delegate :get_content, to: :pluggin
# ...
end