Custom gem - how to extract? - ruby-on-rails

I am new to Ruby/Rails, and I have received a copy of a Rails project.
From what i have read online the gemfile contains the dependencies - i.e other libraries.
So if I have a "gem 'mycustomgem'" in the gemfile, and this gem is not available as open source, is there a way I can extract this gem from the project I have?
I have asked to write a new and alternative version of the rails project, where I am going to be needing this custom gem. Or do I have to contact the guy who wrote the custom gem?
Or have I completely missed something??

open you Gemfile and replace row
gem 'your_gem'
with
gem 'your_gem', :path => '/absolute_path_to_you_local_folder_with_gem'

Related

How to rename ruby gem after build is made?

I am trying to push into rubygems.orge a simple gem following this tutorial. Basically I am using bundler and I have write a simple Hello World class. Then, I try to push the gem as follows:
bundle gem my_first_gem
gem build my_first_gem-0.0.1.gem
and I get:
Signed in.
Pushing gem to https://rubygems.org...
Repushing of gem versions is not allowed.
Please use `gem yank` to remove bad gem releases.
So, I have checked and there is already a gem with such name. So, is there an easy way to rename the gem I have including changing the gem name in all generate by bulder files:
or if I should rename the files by hand, could you tell which are the critical ones?
Instead of renaming files by hand. As it is just a tutorial gem, I would suggest you to create a new gem with
bundle gem gotqn_first_gem
and just move your HelloWorld class in lib. And follow the rest of the commands suggested in Railscasts.
Don't forget that after renaming you need to call git add -A to update your files list.
The reason for that is because (unless that you have changed) your my_first_gem.gemspec have a line like that:
spec.files = `git ls-files -z`.split("\x0")
So, when you call gem build my_first_gem-0.0.1.gem, the above command will search for your older files and ignore the renamed ones.

Where are the gem files located?

Where are the gem files located ?
I'm new to rails and trying o understand how the whole gem functionality works.
My question is how can i follow a gem installation in order to confirm a gem is been installed ?
Where are the installed files located ?
From within your rails app, you can list out all of the gems being used, their versions, and the local path:
bundle show --paths
There's no reason to modify any of these files though. Configuration is typically done through an initializer in /app/initializers, but it depends on the gem being used.
If you need to modify something about the gem, you should fork it on Github and then reference the git location in your Gemfile until your pull request makes it back into the gem:
gem 'some_gem', '4.1.1', git: 'https://github.com/some_github_repo/some_gem.git'

Whats the best way to tweak ruby gems for code reading

To help understand the source code of various gems I often want to place various puts statements in the source code or even try using the ruby debugger.
But whats the best way of doing this?
Do you clone the project from github and make changes locally, if so how do you "force" the use of the local cloned code over the local gem on your machine. Do I just create some scripts that explicitly require the path of the cloned repos folder?
Or do should I use rvm to create a temp gemset, download the gem and modify it directly?
Are there any other methods ive overlooked? How would this change for gems designed for use within rails projects.
The way I usually do it when I want to make changes to a Gem:
Fork the repository on Github
Check it out and create a new branch for local changes
Use Bundler to manage dependencies for the project which uses the Gem
Change one line in the Gemfile to make it use the forked version of the Gem:
gem "thegem", :git => "git://github.com/name/thegem.git", :branch => 'mybranch'
or
gem "thegem", :git => "file:///path/to/thegem", :branch => 'mybranch'
with /path/to/thegem being the path to your local working copy.
The advantage is that you now already have a the perfect infrastructure set up for contributing your changes through a pull request :)
With Bundler.
In a Rails app simply edit the Gemfile and add:
gem "gem_name", :path => "~/MyGems/gem_name"
PS: Bundler work with any Ruby project.
You can use rvm to create a temp gemset, download the gem and modify it directly. A fast way to view/modify a gem is using gemedit :
Install:
gem install gemedit
Usage:
gem edit devise
or: gem edit devise -e mate

how to set gem dependencies without having to declare them on the Gemfile that uses it?

I'm building a gem locally, suppose it name is "MyGem".
Now suppose that MyGem depends on other already built gem, for example "cancan".
So, i've added in my mygem.gemspec the line:
add_runtime_dependency("cancan")
Here is the problem: if I installl MyGem into a new project by adding it into my gemfile like this:
gem "mygem", :path => "path/to/my/local/gem"
then this new project is not being able to use cancan methods, and I have to explicity declare cancan on the new project gemfile in order to use it.
I tried also using gemspec method, but didn't solve my problem either.
Any ideas?
Update
I just wanted to add that when i only have myGem declared in my new application gemfile, after I run bundle install all the dependencies are installed.
That is, if i run gem list the "cancan" gem is displayed, but I still can't access it methods from the application level.
Thanks for the help.
Ok, i've solved this. I'm not sure if this is the best solution but it did work.
Making the application level developer to explicity add the dependencies in his gemfile didn't make any sense. So, as the gems did were being installed, i just required all the gem dependencies in my my_engine.rb file inside my gem.
Following the example, in my my_engine.rb I added the following line
require "cancan"
And that'it...
Even better you could do:
autoload :CanCan, "cancan"
So the module would be loaded only when it is called.
And even better than that, you could load only the file from cancan that you are using (maybe you don't need to load all of it).
You can add that line in you my_gem.rb file or your engine.rb file if you are using engines.
That worked for me, I hope this help someone.
Use add_dependency instead of add_runtime_dependency (this may help with cancan) then run bundle update on your new project.
Does the "cancan" gem name actually match the name to be require'd?
Example for yajl-ruby gem:
Gem::Specification.new do |s|
s.name = %q{yajl-ruby}
...
But when require'ing the library, you'd use a different string:
require 'yajl'
That means in your Gemfile, you have to explicitly require the dependency (which you said you wanted to avoid).
gem 'yajl-ruby', '>=1.0', :require => 'yajl'
To avoid needing to do this, and if you're the author of the "cancan" dependency, you should make the gem name matches the require name.
The only alternative I can think of is require'ing the dependency directly in one of your source files (like you did in your solution).

gem version dilemma with rails 3.1

I want to use ebayapi gem (https://github.com/codyfauser/ebay) with my rails 3.1 application.
If I add the gem in the Gemfile, rails doesn't run.
/Users/ssk/.rvm/gems/ree-1.8.7-2011.03/gems/money-1.7.1/lib/support/cattr_accessor.rb:7:in `cattr_reader': undefined method `id2name' for {:instance_writer=>true}:Hash (NoMethodError)
I removed the ebayapi gem and tried "require 'ebay'" but it said that "no such file to load".
Ebayapi gem works only with money 1.7.1 and I think that conflicts with rails 3.1 (maybe 3.0 as well).
Is there a way to workaround?
Thanks.
Sam
If it's truly incompatible, and you're up to fixing it yourself, then fork the projects in question on github, and update your Gemfile to point to your git repo (or even a local path to make editing a lot easier).
Here's an example:
gem 'money', :path => "~/dev/ruby/gems/money"
# or
gem 'money', :git => "git://github.com/my_account/money.git"
Once you've fixed it, send a pull request to the original project so they can include the fix.

Resources