Problems referencing code in bundled gems

48 views
Skip to first unread message

Clem Rock

unread,
Jan 10, 2014, 11:42:51 AM1/10/14
to rubyonra...@googlegroups.com
Hello,
I'm working on my first gem and I'm having a problem referencing
dependencies. In my gem, I need to reference code from some other
custom gems we've developed.
We'll call my Gem "test_company_models". In my test_company_models
gem, I need to include PricingExtensions from our test_company_libs Gem.

I'm dynamically requiring my models in my test_company_models.rb this
way:

[code]
Dir[File.join(File.dirname(__FILE__), '/test_company_models/*.rb')].each
do |file|
# skip previously loaded files to avoid duplicate warnings
next if File.basename(file, File.extname(file)) == 'version'
require File.join( File.dirname(__FILE__),
"vst_models/#{File.basename(file, File.extname(file))}")
end
[/code]

This works fine until I am pulling in a model which references
PricingExtensions from the test_company_libs Gem:

[code]
class CartItem < ActiveRecord::Base
include PricingExtensions
[/code]

Which gives me this error:

[code]
uninitialized constant CartItem::PricingExtensions
[/code]

I've tried adding the test_company_libs Gem to my Gem in these two ways:


Gemfile:
[code]
gem 'test_company_libs', :git =>
'g...@github.com:test_company/test_company-libs.git', :require => false
[/code]

and in the test_company_models.gemspec:

[code]
spec.add_runtime_dependency "test_company_libs"
[/code]
In either case, I get the "uninitialized constant
CartItem::PricingExtensions" error.


Any idea as to what I am doing wrong?

Thanks,
Eric

--
Posted via http://www.ruby-forum.com/.

Frederick Cheung

unread,
Jan 10, 2014, 1:01:46 PM1/10/14
to rubyonra...@googlegroups.com
On Friday, January 10, 2014 4:42:51 PM UTC, Ruby-Forum.com User wrote:
Hello,
   I'm working on my first gem and I'm having a problem referencing
dependencies.   In my gem, I need to reference code from some other
custom gems we've developed.
We'll call my Gem "test_company_models".   In my test_company_models
gem, I need to include PricingExtensions from our test_company_libs Gem.


If you need PricingExtensions then you need to require the file that contains it. You could either do this only in the models that require it, or another pattern would be for your test_company_libs gem to have a test_company_libs.rb file which would require al the bits of that gem that are commonly used, so a person using the gem only has to do require 'test_company_libs' (and possibly not even that, since by default bundler would require that file for you)

Fred



 

Clem Rock

unread,
Jan 13, 2014, 2:57:42 PM1/13/14
to rubyonra...@googlegroups.com
Fred,
Thanks for your help. I originally tried to include PriceExtensions
via require 'pricing_extensions' in test_company_libs.rb and kept
getting this:

`require': cannot load such file -- pricing_extensions

I should also mention that PricingExtensions is a module in the
test_company_libs Gem.

I just don't get what I'm missing here.

Frederick Cheung

unread,
Jan 14, 2014, 2:08:42 AM1/14/14
to rubyonra...@googlegroups.com
On Monday, January 13, 2014 7:57:42 PM UTC, Ruby-Forum.com User wrote:
> Fred,
>
> Thanks for your help. I originally tried to include PriceExtensions
>
> via require 'pricing_extensions' in test_company_libs.rb and kept
>
> getting this:
>
>
>
> `require': cannot load such file -- pricing_extensions
>
>
>
> I should also mention that PricingExtensions is a module in the
>
> test_company_libs Gem.
>
>
>
> I just don't get what I'm missing here.
>

Where is pricing_extensions.rb?

Fred

Clem Rock

unread,
Jan 14, 2014, 7:37:27 AM1/14/14
to rubyonra...@googlegroups.com
pricing_extensions.rb lives in the test_company_libs which is added to
the test_company_models's Gemfile:

gem 'test_company_libs', :git
=>'g...@github.com:test_company/test_company-libs.git', :require => false

Frederick Cheung

unread,
Jan 15, 2014, 2:14:19 AM1/15/14
to rubyonra...@googlegroups.com


On Tuesday, January 14, 2014 12:37:27 PM UTC, Ruby-Forum.com User wrote:
pricing_extensions.rb lives in the test_company_libs which is added to
the test_company_models's Gemfile:

Where precisely? If require 'pricing_extensions' doesn't work then it sounds like it's not in a folder on ruby's load path. A gem's lib folder (but not sub directories thereof) is usually added to the load path

Fred

Clem Rock

unread,
Jan 16, 2014, 10:21:25 AM1/16/14
to rubyonra...@googlegroups.com
Fred - pricing_extensions lives in the test_company_libs Gem's
lib/tc_libs folder. I'm not sure how to add this to the ruby load path
and I'm a bit confused that I would have to because we use the
test_company_libs Gem in nearly every project we have and we don't have
to 'tweak' anything to load the gemfiles.

Here's the load paths from the tc_libs.rb in the test_company_libs Gem:


$:.unshift File.join(File.dirname(__FILE__), 'tc_libs')

module TestCompanyLibs
end

Dir[File.join(File.dirname(__FILE__), '/tc_libs/*.rb')].each do |file|
next if File.basename(file, File.extname(file)) == 'version'
require File.basename(file, File.extname(file))
end

Frederick Cheung

unread,
Jan 16, 2014, 1:05:29 PM1/16/14
to rubyonra...@googlegroups.com


On Thursday, January 16, 2014 3:21:25 PM UTC, Ruby-Forum.com User wrote:
Fred - pricing_extensions lives in the test_company_libs Gem's
lib/tc_libs folder.

In that case you would do

require 'tc_libs/pricing_extensions'

Fred

Clem Rock

unread,
Jan 16, 2014, 1:12:58 PM1/16/14
to rubyonra...@googlegroups.com
I initially tried that and got:

"`require': cannot load such file"

My question is, does adding

gem 'test_company_libs', :git =>
'g...@github.com:test_company/test_company-libs.git', :require => false

to the Gem's Gemfile and doing a bundle install, ensure that gem's code
can be referenced at runtime?

Clem Rock

unread,
Jan 16, 2014, 1:14:08 PM1/16/14
to rubyonra...@googlegroups.com
I should also point out that test_comany_lib's PricingExtension is a
module:

module PricingExtensions

Frederick Cheung

unread,
Jan 16, 2014, 6:26:37 PM1/16/14
to rubyonra...@googlegroups.com
On Thursday, January 16, 2014 6:12:58 PM UTC, Ruby-Forum.com User wrote:
> I initially tried that and got:
>
>
>
> "`require': cannot load such file"
>
>
>
> My question is, does adding
>
>
>
> gem 'test_company_libs', :git =>
>
> 'g...@github.com:test_company/test_company-libs.git', :require => false
>
>
>
> to the Gem's Gemfile and doing a bundle install, ensure that gem's code
>
> can be referenced at runtime?

I don't think the gem's Gemfile is used in this case - normally a gem's dependencies on other gems should be in your gemspec. Then in your app you would add the above to the app's Gemfile to say how the test_company_libs gem should be found.

Fred

Clem Rock

unread,
Jan 21, 2014, 9:46:04 AM1/21/14
to rubyonra...@googlegroups.com
Fred,
Sorry for falling off the map. Picking this up again and I'm stumped
on how to make this work.

I've tried to adding test_company-libs as a add_development_dependency
and also as a add_runtime_dependency and neither seem to solve the
problem.

IE:
spec.add_development_dependency "test_company-libs" #doesn't work


spec.add_runtime_dependency "test_company-libs" #doesn't work

Frederick Cheung

unread,
Jan 23, 2014, 4:48:23 AM1/23/14
to rubyonra...@googlegroups.com
On Tuesday, January 21, 2014 3:46:04 PM UTC+1, Ruby-Forum.com User wrote:
Fred,
Sorry for falling off the map.   Picking this up again and I'm stumped
on how to make this work.

I've tried to adding test_company-libs as a add_development_dependency
and also as a add_runtime_dependency and neither seem to solve the
problem.

IE:
spec.add_development_dependency "test_company-libs"  #doesn't work


spec.add_runtime_dependency "test_company-libs"   #doesn't work

 
In what way did it not work? You might also try adding both

gem 'test_company-libs', :git => '...' 
gem 'your other gem', :git => '...' 

to the application's gemfile

and forget about formally declaring test_company-libs as a dependency of your gem.

Fred
Reply all
Reply to author
Forward
0 new messages