I'm working through CodeSchool's RubyBits and I've come to an exercise I'm just not comprehending: "Make sure the AtariLibrary class includes only the LibraryUtils module and let ActiveSupport::Concern take care of loading its dependencies. Then, refactor the self.included method on LibraryUtils to use the included method."
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
Based on the solution (below) it seems like ActiveSupport::Concern
doesn't take care of loading the dependencies - you need to include LibraryLoader inside of LibraryUtils.
Can you help me understand just what ActiveSupport::Concern
is doing, and why it needs to be called via extend
in both modules?
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
Thanks!
When you call extend ActiveSupport::Concern
it will look for a ClassMethods inner-module and will extend your 'host' class with that. Then it will provide you with an included
method which you can pass a block to:
included do
some_function
end
The included
method will be run within the context of the included class. If you have a module that requires functions in another module, ActiveSupport::Concern
will take care of the dependencies for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With