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:
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
This works fine until I am pulling in a model which references
PricingExtensions from the test_company_libs Gem:
class CartItem < ActiveRecord::Base
include PricingExtensions
Which gives me this error:
uninitialized constant CartItem::PricingExtensions
I’ve tried adding the test_company_libs Gem to my Gem in these two ways:
Gemfile:
gem 'test_company_libs', :git =>
'[email protected]:test_company/test_company-libs.git', :require => false
and in the test_company_models.gemspec:
spec.add_runtime_dependency "test_company_libs"
In either case, I get the “uninitialized constant
CartItem::PricingExtensions” error.
Any idea as to what I am doing wrong?
Thanks,
Eric
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
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.
pricing_extensions.rb lives in the test_company_libs which is added to
the test_company_models’s Gemfile:
gem ‘test_company_libs’, :git
=>‘[email protected]:test_company/test_company-libs.git’, :require => false
On Monday, January 13, 2014 7:57:42 PM UTC, Ruby-Forum.com User wrote:
`require’: cannot load such file – pricing_extensions
Where is pricing_extensions.rb?
Fred
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
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
I initially tried that and got:
“`require’: cannot load such file”
My question is, does adding
gem ‘test_company_libs’, :git =>
‘[email protected]: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 should also point out that test_comany_lib’s PricingExtension is a
module:
module PricingExtensions
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
On Thursday, January 16, 2014 6:12:58 PM UTC, Ruby-Forum.com User wrote:
gem ‘test_company_libs’, :git =>
‘[email protected]: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
On Tuesday, January 21, 2014 3:46:04 PM UTC+1, Ruby-Forum.com User
wrote:
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
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
Thanks again for all your help.