Forum: Ruby on Rails Problems referencing code in bundled gems

8d4c3605bb2853f5088f8719d352f3cd?d=identicon&s=25 Clem Rock (clemrock)
on 2014-01-10 17:42
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 =>
'git@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
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2014-01-10 19:03
(Received via mailing list)
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
8d4c3605bb2853f5088f8719d352f3cd?d=identicon&s=25 Clem Rock (clemrock)
on 2014-01-13 20:57
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.
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2014-01-14 08:09
(Received via mailing list)
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
8d4c3605bb2853f5088f8719d352f3cd?d=identicon&s=25 Clem Rock (clemrock)
on 2014-01-14 13:37
pricing_extensions.rb lives in the test_company_libs which is added to
the test_company_models's Gemfile:

gem 'test_company_libs', :git
=>'git@github.com:test_company/test_company-libs.git', :require => false
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2014-01-15 09:42
(Received via mailing list)
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
8d4c3605bb2853f5088f8719d352f3cd?d=identicon&s=25 Clem Rock (clemrock)
on 2014-01-16 16:21
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
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2014-01-16 19:07
(Received via mailing list)
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
8d4c3605bb2853f5088f8719d352f3cd?d=identicon&s=25 Clem Rock (clemrock)
on 2014-01-16 19:12
I initially tried that and got:

"`require': cannot load such file"

My question is, does adding

gem 'test_company_libs', :git =>
'git@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?
8d4c3605bb2853f5088f8719d352f3cd?d=identicon&s=25 Clem Rock (clemrock)
on 2014-01-16 19:14
I should also point out that test_comany_lib's PricingExtension is a
module:

module PricingExtensions
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2014-01-17 00:27
(Received via mailing list)
On Thursday, January 16, 2014 6:12:58 PM UTC, Ruby-Forum.com User wrote:
>
>
> gem 'test_company_libs', :git =>
>
> 'git@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
8d4c3605bb2853f5088f8719d352f3cd?d=identicon&s=25 Clem Rock (clemrock)
on 2014-01-21 15:46
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.
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2014-01-23 10:49
(Received via mailing list)
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
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.