6

I want to dynamically include all modules within a certain folder into this other module. My code is as follows:

module Extensions
  module ProductExtension
    def add_included_extensions
      extensions = Pathname.glob("lib/extensions/merchant/*.rb")
        .map(&:basename)
        .collect{|x| 
          x.to_s.gsub(".rb", "")
          .titleize.gsub(" ","")
        }

      extensions.each do |merchant|
        include "Extensions::MerchantExtensions::#{merchant}".constantize
      end
    end
    def add_items
      add_included_extensions
      Merchant.all.each do |merchant|
        send("add_#{merchant.name.downcase}_items")
      end
    end
  end
end

However it doesn't seem to be actually including the files because when I call the send method, it says the method it's calling doesn't exist. Any idea what I could be doing wrong?

1
  • Have only fast look - you use titleize instead camelize Commented May 10, 2013 at 21:43

1 Answer 1

16

The problem is you include your modules inside instance methods, so they are not included in the class.

Try:

self.class.send(:include, "Extensions::MerchantExtensions::#{merchant}".constantize)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.