我可以像在包含的模块中那样直接调用实例方法吗?
Can I call instance method directly like in included module?
如果可以,如何在included module中直接调用或rebuild SomePluggin
class调用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
您可以 delegate get_content
调用 pluggin
:
module Pluggin
delegate :get_content, to: :pluggin
# ...
end
如果可以,如何在included module中直接调用或rebuild SomePluggin
class调用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
您可以 delegate get_content
调用 pluggin
:
module Pluggin
delegate :get_content, to: :pluggin
# ...
end