how do I structure gem to avoid LoadError in my Rails project?

Hi,

I'm new to gem development. I just completed a gem for use with a Rails 3.0.7 project. The gem itself passes its tests. I've listed the gem in the Rails project's Gemfile...

Unfortunately, Rails is choking on some requires in my gem. Here is what I get in Rails:

activesupport-3.0.7/lib/active_support/dependencies.rb:239:in `require': no such file to load -- zip_code/config.rb (LoadError)

Meanwhile, the lib directory in the gem consists of zip_code.rb and /zip_code, where zip_code.rb contains the key lines:

$LOAD_PATH.unshift(File.dirname(__FILE__))

require 'zip_code/version.rb' require 'zip_code/config.rb' require 'zip_code/zip_code.rb'

Seems like Rails is stopping on config.rb and zip_code.rb -- it doesn't see them.

Any tips on how to properly require files in a gem so that Rails will see them?

Thanks,

Grar

Grary Stimon wrote in post #1019984:

Hi,

I'm new to gem development. I just completed a gem for use with a Rails 3.0.7 project. The gem itself passes its tests. I've listed the gem in the Rails project's Gemfile...

Unfortunately, Rails is choking on some requires in my gem. Here is what I get in Rails:

activesupport-3.0.7/lib/active_support/dependencies.rb:239:in `require': no such file to load -- zip_code/config.rb (LoadError)

$LOAD_PATH.unshift(File.dirname(__FILE__))

Try getting rid of that line. Do you have a .gemspec file?

Your directory structure works for me in rails 3.0.9, but I don't know if my .gemspec file is different than yours. This is your directory structure:

/lib     zip_code.rb -- require 'zip_code/config.rb'     /zip_code         version.rb         config.rb         zip_code.rb

Even though that works for me, do you think it's wise to use the file name zip_code.rb in the /zipcode directory?

Hey,

Still no joy.

I thought I'd follow 7stud's example and recreate the gem structure to see if I could load it after any reasonable modifications...

/lib     zipcode.rb -- require 'zipcode/config.rb'     /zipcode         version.rb         config.rb         cityandstate.rb

...where lib/zipcode.rb is...

require "zipcode/config.rb"

module Zipcode   # Your code goes here... end

I'll leave my gemspec farther below -- in case someone sees something above right away -- but I still get the kind kind of error when this gem is installed in my Rails 3.0.7 app...

gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:in `require': no such file to load -- zipcode/config.rb (LoadError) ... ruby-1.9.2-p290/gems/zipcode-0.0.1/lib/zipcode.rb:1:in `<top (required)>'

I'm using rvm 1.9.2 and Bundler in my attempt to create my first working gem...now here's the gemspec...

# -*- encoding: utf-8 -*- $:.push File.expand_path("../lib", __FILE__) require "zipcode/version"

Gem::Specification.new do |s|   s.name = "zipcode"   s.version = Zipcode::VERSION   s.authors = ["Grar"]   s.email = ["grary.stimon@gmail.com"]   s.homepage = ""   s.summary = %q{Provides city and state pair and zip data, and vice versa}   s.description = %q{Cities in states and their zip codes}

  s.rubyforge_project = "zipcode"

  s.files = `git ls-files`.split("\n")   s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")   s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }   s.require_paths = ["lib"] end

Thanks,

G

Hi,

I'm new to gem development. I just completed a gem for use with a Rails 3.0.7 project. The gem itself passes its tests. I've listed the gem in the Rails project's Gemfile...

Unfortunately, Rails is choking on some requires in my gem. Here is what I get in Rails:

activesupport-3.0.7/lib/active_support/dependencies.rb:239:in `require': no such file to load -- zip_code/config.rb (LoadError)

Do you have a valid gemspec in the root of your project?

$LOAD_PATH.unshift(File.dirname(__FILE__))

Don't play with your $LOAD_PATH.

Any tips on how to properly require files in a gem so that Rails will see them?

Take a look to RubyGems guides:

Luis,

Yes, the gemspec is as listed in my previous post...

Thanks for the reference to RubyGems doc, which I am reading now, in hopes of solving this.

Meanwhile, I'm wondering if there's interaction between rvm 1.9.2 and this problem?

In this connection, I modified zipcode/config.rb as follows...

require_relative "zipcode/config.rb"

module Zipcode   # Your code goes here... end

But I get the same kind of error...

.rvm/gems/ruby-1.9.2-p290/gems/zipcode-0.0.1/lib/zipcode/config.rb (LoadError)

Grar

OK, solved it.

Thanks to Luis's reference to the original rubygems guide, I was able to build a simple gem that worked with my Rails 3.0.7 project. So, I realized there was something wrong with my use of bundler in creating gems.

The problem was that I was not managing the gem's development with git after the initial automatic commit with gem bundler. Thus, when I executed 'git ls-files' at the command line, I saw that new files, like lib/zipcode/config.rb, were not being loaded to the gem specification's file list when gem bundler's 'rake install' was executed.

I guess a useful takeaway from this is that Gem::Specification.files is a vital part of the dependency loading for a gem...

Grar