How to specify version of a local gem in Gemfile? - ruby-on-rails

I have a local gem and a rails app that uses the local gem.
Is there a way to specify a specific version of the gem?
I know that 'rake install' generates *.gem in /pkg directory of the gem. But I don't know how to use it.
Here's how I use it now:
In Gemfile:
gem 'mygem', path: '~/shared/mygem'
Thanks.
Sam
Edit:
I think my question is almost a duplicate of How to tell the Gem File to use a specific local copy of a gem.
I just need to know how to specify a specific version of a local gem.

In Gemfile:
gem 'mygem', path: '~/shared/mygem', branch: 'branch name'
Or use ref: 'commit number' if your version is in a specific commit

To specify versions, or more specifically, a range of versions, you use operators like you would in ruby code.
The operators should be:
>= , <= , or ~>
Ranges allow that it may not be possible to match the exact version number (especially true for Windows-specific gem builds).
The pessimistic operator (~>) is often considered the best choice, as it stays within the same dot-range as the build you are using (1.0.4 would never reach 1.1.0, but could use anything between).
An example of the gemfile syntax:
gem 'sinatra', '~> 1.4.4'
or
gem 'uglifier', '>= 1.3.0'

Related

"Attaching" a gem to Rails to prevent "require" everywhere?

The name of a gem is not the same as the module. Right now I need to require it and include in various types of files, such as controllers and models. This is amounting to many requires which I don't have to do for other gems. It's a gem I can update. Is there a way the gem needs to be configured so it is "attached" to Rails, and if so, where may I find instructions to do this?
UPDATE: using required: "name-of-module" in Gemfile removes need for require everywhere. Still wondering, if gem could be configured to not require this in Gemfile?
In the Gemfile, you can do these things:
# Require a different file than the gem's name
gem 'foo', require: 'bar'
# Install but not require anything.
# You need to manually require the gem somewhere.
gem 'foo', require: false
You can still add version and platform specification if you want.
Real-world examples are ActiveSupport and rspec:
gem 'activesupport', '~> 5.2', require: 'active_support/all'
gem 'rspec', '~> 3.1', group: :test, require: false
You need to specify which files you want Ruby to load for you, so you either need specify the gem in your Gemfile (and run bundle exec ...) or put require in the right place(s) in your code. There's no way around that.
If it's a gem that you work on at the same time as you use it in another project, then you can specify a path to the gem. Like
# Gemfile
source "https://rubygems.org"
ruby '2.6.1'
gem 'my_gem', path: '/home/user/Development/my_gem'
This way, you can change your gem and use it directly without having to build and install it.

Conditions on gem requirement

I would like to prevent the updating of a gem on my windows (rmagick), so it sticks to 2.12.0 mswin32. Still, my coworker needs to have the gem on his Darwin install...
So, I tried to do something like this in the Gemfile:
if RUBY_PLATFORM =~ /darwin/i
gem 'rmagick', '~> 2.12.0'
else
gem 'rmagick', '=2.12.0.mswin32'
end
but bundle install complaints.
What is the right way of handling this properly?
You can't use conditionals on gemspec because gemspec is serialized
into YAML, which doesn't contain executable code.
I faced a related problem in the Gemfile of a local Rails project (not a
gem).
Currently, the Gemfile contains:
group :test do
...
# on Mac os X
gem 'rb-fsevent' if RUBY_PLATFORM.include?("x86_64-darwin")
gem 'ruby_gntp' if RUBY_PLATFORM.include?("x86_64-darwin")
# on Linux
gem 'rb-inotify' unless RUBY_PLATFORM.include?("x86_64-darwin")
gem 'libnotify' unless RUBY_PLATFORM.include?("x86_64-darwin")
end
This works (although it is ugly) for developing on Mac and Linux
systems.
But, we stopped checking in the Gemfile.lock since it changes every time
a developer with a different platform checks in the code.
So, a solution for multi-platform Gemfiles should also solve the
problem for Gemfile.lock.
The other solutions is building multiple .gemspec files for each target OS and change both platform and dependencies for each platform:
gemspec = Gem::Specification.new do |s|
s.platform = Gem::Platform::RUBY
end
# here build the normal gem
# Now for linux:
gemspec.platform = "linux"
gemspec.add_dependency ...
# build the newer gemspec
...
You should use the platforms option Bundler provides:
If a gem should only be used in a particular platform or set of
platforms, you can specify them. Platforms are essentially identical
to groups, except that you do not need to use the --without
install-time flag to exclude groups of gems for other platforms.
So in your specific case that would look something like this:
gem 'rmagick', '~> 2.12.0', :platforms => :ruby
gem 'rmagick', '=2.12.0.mswin32', :platforms => :mswin

alternative for github : <repo-name> in gem file

I am trying to build the docrails in my system. When I clone the repo and do bundle install as the guides say.
I get the following error
You passed :github as an option for gem 'rails/active_record_deprecated_finders'
, but it is invalid.
Entry in gemfile looks like this:
gem 'rails/active_record_deprecated_finders', github: 'rails/active_record_deprecated_finders'
To avoid that what I am doing is commenting the rest of the line like this:
gem 'rails/active_record_deprecated_finders'#, github: 'rails/active_record_deprecated_finders'
Then it foregoes that and the next problem arrives with the error message saying:
Could not find gem 'active_record_deprecated_finders (>= 0) x86-mingw32' in any
of the gem sources listed in your Gemfile.
Environment specs:
Bundler version 1.0.21
Rails 3.2.3
Win7 64bit
Question
I dont know why its looking for x86 when my System is 64bit.Is there any work around for this? or its a bug?
If gem file couldn't accept github: as parameter why is it there in the first place?
Please let me know if there are any workarounds to this problem
The :github option is just shorthand for a longer :git option:
gem :foo, :github => 'rails/foo'
Is just short for
gem :foo, :git => 'git://github.com/rails/foo.git'
This is new in bundler 1.1 which is why it doesn't work on your setup. You could rewrite the gemfile but it would probably be easier to update bundler. In addition bundler 1.1 is a lot faster than 1.0

How do I declare gemspec dependency as >= 3.1 but less than < 4.0

I am making changes to my ruby gem to make it asset pipeline compatible. In my gemspec I want to say that it requires rails version > 3.1 and < 4. How do I do that.
currently this is what I have.
s.add_dependency("rails", ">= 3.1")
But this is not ideal. This is saying that it will also work with rails 4.0 which might not be true.
You can use the pessimistic operator ~>
Using the pessimistic operator, you could write
s.add_dependency("rails", "~> 3.1")
which is equivalent to '>= 3.1', '< 4.0'

Rails - Understanding Gems

I'm using the rails devise gem. I noticed a case sensitivity bug which turns out is fixed in the latest version of devise so I'm thinking about upgrading.
In my gem file I have:
gem 'devise', '~> 1.1.3'
When I run bundle I get:
Using devise (1.1.9)
Why the difference. And what setting should I be using in my gem file to upgrade to the latest and greatest?
Thanks
The ~> in your Gem declaration says that Bundler can install any version up to the next major version, so in this case it could install any version of devise that is => 1.1.3 and < 1.2.0.
Including the ~> is good practice, as it means security updates are automatic if the gem is using versioning correctly; in a production environment, you'll probably want to drop this moniker, though, and just set your gem versions statically to avoid issues.
To update to the latest version of the gem, everytime, just use the following with no second version argument:
gem 'devise'
See more information on the Gemfile format at http://gembundler.com/gemfile.html.
Just use :
gem 'devise'
and you will be getting the latest stable gem :)
The difference is because you're telling to Bundler to use 1.1.3 or a major version of this gem in you system, if you want to use a specific version just put '1.1.9' in the version param.
use bundle update devise to update the devse gem and bundle update to update all the gems (which is not advisable)
http://jsbin.com/ihiqe4
if you know the version number you want, try this (assuming it's 1.2.3):
gem 'devise', '1.2.3'
or just leave out the version number
if it has not been released yet, you can point to it's github repository instead.

Resources