title: Using classes in a module from the lib directory - Rails 3
body: <p>I have a setup in the lib directory like so:</p>
  
  <pre><code>lib/
    copy_process.rb
    copy_process/
      processor.rb
  </code></pre>
  
  <p>The copy_process.rb and processor.rb contain the module definition CopyProcess. The copy_process.rb defines the CopyFile class as well:</p>
  
  <pre><code>module CopyProcess
    class CopyFile
    end
  end
  </code></pre>
  
  <p>The processor.rb is structured like so:</p>
  
  <pre><code>module CopyProcess
    class Processer
  
    end
  end
  </code></pre>
  
  <p>In one of its methods, it creates a new copy file object:</p>
  
  <pre><code>def append_file_if_valid(file_contents, headers, files, file_name)
    unless headers
      raise "Headers not found"
    else
      files &lt;&lt; CopyProcess::CopyFile.new()
    end
  end
  </code></pre>
  
  <p>When I used these files as part of a command line ruby program, it worked fine. However, i started putting it into a rails app, and I have written cucumber/capybara tests to hit the buttons and so forth where this is used. I initialize a Processor object from one of my AR models, and call the above method a few times. It cannot seem to find the CopyFile class, even though I have the following code in my application.rb</p>
  
  <pre><code>config.autoload_paths += %W(#{config.root}/lib)
  config.autoload_paths += Dir["#{config.root}/lib/**/"]
  </code></pre>
  
  <p>Any ideas?</p>
  
  <p>===============================================================</p>
  
  <p><strong>Edit</strong> Above was solved by extracting the copy file class into it's own file under lib.
  I now have another issue:</p>
  
  <p>The CopyFile class refers to module-level helper methods that sit in lib/copy_process.rb, like so:</p>
  
  <pre><code>module CopyProcess
    # Gets the index of a value inside an array of the given array
    def get_inner_index(value, arr)
      idx = nil
      arr.each_with_index do |e, i|
        if e[0] == value
          idx = i
        end
      end
      return idx
    end
  
    def includes_inner?(value, arr)
      bool = false
      arr.each { |e| bool = true if e[0] == value  }
      return bool
    end
  
  
    # Encloses the string in double quotes, in case it contains a comma
    # @param [String] - the string to enclose
    # @return [String]
    def enclose(string)
      string = string.gsub(/\u2019/, '&amp;rsquo;')
      if string.index(',')
        return "\"#{string}\""
      else
        return string
      end
    end
  end
  </code></pre>
  
  <p>When I run my cucumber tests, i get the following error: </p>
  
  <pre><code> undefined method `includes_inner?' for CopyProcess:Module (NoMethodError)
    ./lib/copy_process/copy_file.rb:64:in `set_element_name_and_counter'
  </code></pre>
  
  <p>Which refers to this method here: </p>
  
  <pre><code>def set_element_name_and_counter(element_names, name)
    if !CopyProcess::includes_inner?(name, element_names)
      element_names &lt;&lt; [name, 1]
    else
      # if it's in the array already, find it and increment the counter
      current_element = element_names[CopyProcess::get_inner_index(name, element_names)]
      element_names[CopyProcess::get_inner_index(name, element_names)] = [current_element[0], current_element[1]+1]
    end
    element_names
  end
  </code></pre>
  
  <p>I also tried moving the copy_file.rb and other files in the lib/copy_process/ directory up a level into the lib directory. I then received the following error: </p>
  
  <pre><code> Expected /Users/aaronmcleod/Documents/work/copy_process/lib/copy_file.rb to define CopyFile (LoadError)
    ./lib/processor.rb:48:in `append_file_if_valid'
  </code></pre>
  
  <p>The line that the error states creates an instance of CopyFile. I guess rails doesn't like loading the files in that fashion, and for the former setup, I think the copy_file.rb is having issues loading the rest of the module. I tried requiring it and so forth, but no luck. You can also find my most recent code here: <a href="https://github.com/agmcleod/Copy-Process/tree/rails" rel="nofollow">https://github.com/agmcleod/Copy-Process/tree/rails</a></p>