我正在通过 CodeSchool 的 RubyBits 进行练习,但我并不理解:“确保 AtariLibrary 类仅包含 LibraryUtils 模块,并让 ActiveSupport::Concern 负责加载其依赖项。然后,重构LibraryUtils 上的 self.included 方法以使用包含的方法。”
module LibraryLoader
extend ActiveSupport::Concern
module ClassMethods
def load_game_list
end
end
end
module LibraryUtils
def self.included(base)
base.load_game_list
end
end
class AtariLibrary
include LibraryLoader
include LibraryUtils
end
根据解决方案(如下),它似乎ActiveSupport::Concern
不负责加载依赖项 - 您需要在 LibraryUtils 中包含 LibraryLoader。
您能帮我理解ActiveSupport::Concern
正在做什么,以及为什么需要extend
在两个模块中调用它吗?
module LibraryLoader
extend ActiveSupport::Concern
module ClassMethods
def load_game_list
end
end
end
module LibraryUtils
extend ActiveSupport::Concern
include LibraryLoader
#result of refactoring the self.included method
included do
load_game_list
end
end
class AtariLibrary
include LibraryUtils
end
谢谢!