Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
    
Have only fast look - you use titleize instead camelize –  MikDiet May 10 '13 at 21:43
add comment

1 Answer

up vote 1 down vote accepted

The problem is you include your modules inside instance methods, so them are not included into class.

Try:

self.class.send :include, "Extensions::MerchantExtensions::#{merchant}".constantize

share|improve this answer
    
This worked great! Thanks –  justNeph May 11 '13 at 19:49
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.