File to import not found or unreadable when using Rails asset gem - ruby

I'm trying to create a rails asset gem whose source is here. The problem is that when I use the gem in try to require or import the asset files in my Rails 4 project, I get the error:
File to import not found or unreadable: jquery.xdan.datetimepicker
I've inspected the paths it is looking through, and it looks like it's not actually looking in the assets directory for my gem (the gem folder isn't anywhere in the list). So, it seems like the gem isn't getting required, since if it was, the Engine would be getting loaded, indicating to Rails that I have an assets folder somewhere. Can anyone see what I'm missing? From what I can tell, I'm doing everything that other asset gems are doing.

I looked at your Gem. In order to get it working, rename lib/xdan_datetimepicker_rails.rb to lib/xdan-datetimepicker-rails.rb.
In your Rails project, uninstall your old gem and install the new one. To make sure it's installed properly:
$ rails c
> pp MyRailsApp::Application.assets.paths
You should see an array of paths that are in the asset pipeline. Look for:
/gem/path/to/xdan-datetimepicker-rails-0.0.1/app/assets/javascripts
/gem/path/to/xdan-datetimepicker-rails-0.0.1/app/assets/stylesheets
NOTE: Restart rails c and/or rails s if you're not seeing it.
Finally, in order to use the assets:
app/assets/javascript/application.js
//= require jquery.xdan.datetimepicker
app/assets/stylesheets/stylesheet.css.scss
*= require jquery.xdan.datetimepicker

Related

How to replace a Ruby gem with code from a local source directory

I've been using a Ruby package which I installed as a gem. Now I'd like to modify the code, to try my hand at fixing bugs/adding features. I can download the source for the package from GitHub, however, I'm not sure what to do next.
Is there an easy way I can replace a particular gem with code from a local source directory? Ideally, the process would be simple enough that I'd be able to continuously update as I modify the code.
Also, this package is used as a dependency for other gems, and ideally the other packages which use this gem would then use the updated version. (As the program I'm ultimately running is from one of those other gems.) Is there a way to do the install without also installing those other packages from source?
(This would be on Linux, if it makes things easier.)
Assuming your using bundler you can set this in your Gemfile. If your not sure your using bundler look in the root of your project. There should be one file called Gemfile with no extension. The presence of this file will generally indicate that the project's author is using bundler. All changes described below should be made inside that file.
The :path and :git keys in the gem hashmap can be used to point rubygems to different locations. When I am using :path I will have two different ruby projects. The first project is the active project. The project that I am currently working on. This project requires the gem in question that I need to update. The second project will the the checked out source code of the gem I wish to change. With these two projects setup I can edit the Gemfile of the first project and point it at the second project. This is done using :path.
# The Gemfile of the first project
gem 'the_gem_in_question', :path => '/the/path/to/the/second/project'
There are two ways to modify these files and have the changes show up.
One is using the Gemfile to define a path. For example if you wanted the redis gem locally, you can git clone git#github.com:redis/redis-rb.git then as Stewart pointed above, put it into your Gemfile this line gem 'redis', :path => './pathtoredis/redis' instead of gem 'redis'
Another way, which is a little quicker but harder to track changes and stuff is to just gem open redis to open it in a text editor.

LoadError when requiring a renamed gem

I started building Ruby bindings for the Marvel Comics API earlier this month. At the time, there was no gem named marvel on RubyGems, so I fired up jeweler, created a project, and began making a crude, but useable first release. I tested it by rake installing it locally and requiring it in a dummy project that let me play with it in pry:
require 'marvel'
require 'dotenv'
require 'pry'
Dotenv.load
#client = Marvel::Client.new
#client.configure do |config|
config.api_key = ENV['API_KEY']
config.private_key = ENV['PRIVATE_KEY']
end
binding.pry
When I got to the point where I had exposed several endpoints (at this commit) I attempted releasing it to rubygems.org, but discovered someone by then had released a marvel gem. I hastily altered my Rakefile and renamed mine to marvel_api and released it.
I let it sit for a few days before I came back and started experimenting with adding in Faraday middleware to try and clean it up. However, I never seem to have tested whether the name change to marvel_api worked. Now, whenever I try to require marvel_api, I'm met with this LoadError:
/Users/Raevynheart/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- marvel_api (LoadError)
from /Users/Raevynheart/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from test.rb:1:in `<main>'
I'm trying to understand if this is happening because my process for renaming the gem was incorrect, or if this is some separate issue. The gem's source is here: https://github.com/O-I/marvel. Note that the repo name and gem name are different — I don't know if that is an issue. Let me know if there is any other information I need to add to help troubleshoot this. Thanks for any help!
I think you are facing this issue since within your gem's lib directory, you still have the file named as marvel.rb.
From http://guides.rubygems.org/make-your-own-gem/ :
Code for your package is placed within the lib directory. The convention is to have one Ruby file with the same name as your gem, since that gets loaded when require 'hola' is run. That one file is in charge of setting up your gem’s code and API.
So, I believe your issue will resolve by changing your filename within lib to marvel_api.rb.

Why is bundler using the wrong gemspec file?

I've got a custom gem that has been working just fine with regards to bundling, building, distributing, & implementing. The gem is the core of a framework from which other gems are derived. Since most derived gems will have the same basic structure, I want to include a Ruby script in the bin path of the gem that can be used to basically copy files from a template folder into a new folder where the user will develop their own gem.
The problem I'm having is that the template folder has a gemspec file named $name$.gemspec with similarly named classes/modules in the file (e.g.: module $Name$), where the $name$ gets replaced with a name provided by the user.
Unfortunately, when I run bundle install from my gem's top-most path, I get an error:
There was a SyntaxError while evaluating $name$.gemspec:
C:/my_gem/template/$name$.gemspec:8: syntax error, unexpected tGVAR
gem.version = MyGem::$Name$::VERSION
It looks like Bundler is using the wrong Gemfile, even if I explicitly pass the Gemfile or path via one of the following:
bundle install --gemfile=Gemfile
bundle install --path=C:\my_gem
I also tried updating the gemspec line of my Gemfile to no avail:
gemspec name: 'my_gem'
Lastly, I've ensured that the template folder isn't even included in my_gem.gemspec, but that doesn't seem to matter:
gem.files = Dir.glob("lib/**/*") + %w(LICENSE.txt README.md)
Does anyone know why Bundler is trying to read the ./template/$name$.gemspec instead of ./my_gem.gemspec?
Inspecting the Bundler source, I may have spotted the culprit in lib/bundler/source/path.rb. There's GLOB used to find gemspecs in load_spec_files. The default glob is "{,*,*/*}.gemspec". This will find *.gemspec in the root directory of your gem or any directory one descendant from root (which will include your template dir).
If this is indeed the culprit, you could work around this by placing your template directory deeper in your gem's dir hierarchy or changing the name of the template file so it doesn't end in .gemspec. The Bundler::Source::Pathobject looks like it can take a different glob at initilization but I haven't dug deep enough to see if there's a viable way to specify this alternative glob in bundle execution via config or cmdline options.

Creating a gem with Bundler

I'm trying to create a gem with Bundler, following this guide: http://rakeroutes.com/blog/lets-write-a-gem-part-one/. In it, it says:
I incorrectly thought after taking my first look through the gemspec
that I would need to add more require statements as I developed my
gem. That isn’t the case: the files just need to be in git.
I am trying to clean up some of my old gems to use this convention, but when I install my gem, the classes from the other files are not available. I have a couple directories nested under my /lib dir, but I wouldnt think that would be an issue. Is there anything simple to overlook that would prevent my other files from being required? Any help would be appreciated.
In the link, when he says he doesn't need to add a lot of "require" statements, he must mean adding files to the s.files, s.executables, and s.test_files arrays--these determine what files get packaged up into the gem and what files get ignored. As you can see from the gem spec, whatever's tracked by git in certain directories is going to be included in the packaged gem.
Ruby's require is a different story. Standard require rules still apply.
Ruby's gem system works by adding a bunch of different places for Ruby to look for "foo.rb" when you run require "foo". If "lib" is your only require path for your gem, when you require "my_gem" Ruby is only going to run the code in lib/my_gem.rb. If lib/my_gem.rb doesn't require any other files in your gem, then Ruby hasn't seen them and so you'll get undefined constant errors when you try to use the classes from those files.
For examples, you might take a look at two simple gems I've written; both were started with bundle gem: HashToHiddenFields and SimpleStats. In both gems, main Ruby file in lib/ requires everything that needs to be loaded for the gem to work correctly. For example, hash_to_hidden_fields.rb requires action_view/helpers/hash_to_hidden_fields so that the ActionView::Helpers::HashToHiddenFields constant+module exists so we can include it into ActionView::Base.
Hope that answers the question. I know Ruby requiring was pretty fuzzy to me for a while.

What is the modern way to structure a ruby gem?

Has much changed with the release of Bundler? Is there a template that can be used as a base? What are the best practices?
Some posts that I have found useful:
http://chneukirchen.github.com/rps/
http://tomayko.com/writings/require-rubygems-antipattern
http://yehudakatz.com/2009/07/24/rubygems-good-practice/
http://weblog.rubyonrails.org/2009/9/1/gem-packaging-best-practices
Edit (2012-01-10): An excellent all-around guide to gem best practices is RubyGems Guides. I would highly recommend starting here now.
To summarize the key points:
Use the basic lib/gem.rb and lib/gem/ structure for code.
Put any executables in bin, any data files in data and tests in test or spec.
Don't require or depend upon files outside of the load path. (VERSION files often seem to live in odd places in gems.)
Do not require 'rubygems'.
Do not tamper with the $LOAD_PATH.
If you find yourself writing require File.join(__FILE__, 'foo', 'bar'), you're doing it wrong.
The simplest way it's to use bundler:
bundle gem <gem_name>
You may even use it in an existing project from the parent directory.
When writing fat (binary) gems the structure is usually this:
lib/1.8/binary.so
lib/1.9/binary.so
lib/my_gem.rb (this file simply chooses which binary.so to load depending on ruby version)
And for native extensions:
lib/ext/my_gem/my_sources.*
lib/my_gem.rb
I also usually put a version.rb file here:
lib/my_gem/version.rb
and it simply contains something like:
module MyGem
VERSION = "0.1.0"
end
Also, IMO, don't put any .rb files except the file you want people to use to load the gem, in the lib/ directory. Instead put all auxiliary files in lib/my_gem/
This rubygems guide provides information about the structure of a gem and then goes into detail about what should be included in your gemspec
You may find it easier to use bundler to create the folder structure of the gem for you:
bundle gem <gem_name>
my_gem$ bundle gem my_gem
create my_gem/Gemfile
create my_gem/Rakefile
create my_gem/LICENSE.txt
create my_gem/README.md
create my_gem/.gitignore
create my_gem/my_gem.gemspec
create my_gem/lib/my_gem.rb
create my_gem/lib/my_gem/version.rb
Initializing git repo in /Users/keith/projects/my_gem/my_gem
Telemachus's advice is good. If you follow it your gem will be setup to play nicely with bundler.
You might also try using jeweler. It's a gem that generates skeletons for gems. The default skeleton that it spits out complies with all of the conventions Telemachus mentioned and it will also do some nice things like add your favorite test framework or create a GitHub repository.

Resources