Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use of extend ActiveSupport::Concern

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!

like image 244
rda3000 Avatar asked Feb 17 '13 01:02

rda3000


1 Answers

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.

like image 180
dmin7b5 Avatar answered Nov 16 '22 01:11

dmin7b5