Require_relative cannot load such file version - ruby-on-rails

i'm trying to load a new gem that i recently created, and don't have have any code yet, because when i tried to load just for test this error appeared.
My code:
My gem name is tracky.
tracky.gemspec
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'tracky/version'
Gem::Specification.new do |spec|
spec.name = "tracky"
spec.version = Tracky::VERSION
spec.authors = ["Matheus Mendes"]
spec.email = ["matheus.mendescf#gmail.com"]
spec.summary = %q{TODO: Write a short summary. Required.}
spec.description = %q{TODO: Write a longer description. Optional.}
spec.homepage = ""
spec.license = "MIT"
spec.files = [`git ls-files -z`.split("\x0")]
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]
spec.add_development_dependency "bundler", "~> 1.7"
spec.add_development_dependency "rake", "~> 10.0"
end
My folder:
http://postimg.org/image/ve7nlyja7/
Error:
irb(main):001:0> require_relative 'lib/tracky'
LoadError: cannot load such file -- tracky/version
from /home/matheus/.rbenv/versions/2.2.2/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /home/matheus/.rbenv/versions/2.2.2/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /home/matheus/Ruby/tracky/lib/tracky.rb:1:in `<top (required)>'
from (irb):1:in `require_relative'
from (irb):1
from /home/matheus/.rbenv/versions/2.2.2/bin/irb:11:in `<main>'
I'm using irb in the root of tracky

Inside the lib/tracky.rb file, change require "tracky/version" to require_relative "tracky/version".
This is because the version file is located relative to the lib/tracky.rb file. I would advise you reserve require for gem dependencies.
Check this answer for the difference between require and require_relative.
Late to the party but I guess it could help the next person. I came across this question while facing the same issue.

Related

Unable to load classes from my gem to my rails app

I have some common code that I want to share between couple of my rails app.
I've generated my gem like so :
bundle gem document-common
My gemspec looks like so :
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'document_common/version'
Gem::Specification.new do |spec|
spec.name = "document_common"
spec.version = DocumentCommon::VERSION
spec.authors = ['Author']
spec.email = ['test#domain.com']
spec.summary = 'Common Models and Lib'
spec.description = 'Common Models and Lib'
spec.homepage = 'google.com'
spec.license = 'WTFPL'
spec.files = Dir["{app,config,db,lib}/**/*", "LICENSE", "Rakefile", "README.rdoc"]
spec.test_files = Dir["spec/**/*"]
spec.test_files.reject! { |file| file.match(/[.log|.sqlite3]$/) }
spec.require_paths = ["lib"]
spec.add_development_dependency "bundler", "~> 1.11"
spec.add_development_dependency "rake", "~> 10.0"
end
In my lib folder I have this :
document_common
document_common.rb
Folder document_common and ruby file document_common.rb, and the ruby file has this content :
require 'document_common/version'
module DocumentCommon
# Your code goes here...
end
Then inside document_common there is a version.rb with this content :
module DocumentCommon
VERSION = '0.1.0'
end
And document.rb with this content:
module DocumentCommon
class Document < ActiveRecord::Base
end
end
So I push this gem to my git repo. And then I add this gem info to my Gemfile in the rails 4 app do a bundle install, but when I refer to DocumentCommon::Document I get this error :
NameError: uninitialized constant DocumentCommon::Document
However if I retrieve the info about the version DocumentCommon::VERSION I get no errors and get the actual version. Also when I do DocumentCommon.constants I get [:VERSION]. What am I doing wrong here? What do I need to do to have access in my main rails app to DocumentCommon::Document model?
You can use the autoload, check out the devise gem does exactly that:
https://github.com/plataformatec/devise/blob/master/lib/devise.rb

Invalid gemspec in ...: wrong number of arguments (0 for 1+)

In my first attempt to create a gem, I have run into this error
Invalid gemspec in [rulers.gemspec]: wrong number of arguments (0 for 1+)
when I try to build it (gem build rulers.gemspec).
I have pasted the code of the gemspec file below.
Where have I made errors?
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'rulers/version'
Gem::Specification.new do |spec|
spec.name = "rulers"
spec.version = Rulers::VERSION
spec.authors = ["Noffica"]
spec.email = ["email#domain.com"]
spec.summary = %q{An attempt to create a Rails-like framework...}
spec.description = %q{... written in Ruby.}
spec.homepage = ""
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
# delete this section to allow pushing this gem to any host.
if spec.respond_to?(:metadata)
spec.metadata['allowed_push_host'] = ""
else
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
end
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
spec.add_development_dependency "bundler", "~> 1.10"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec"
gem.add_runtime_dependency "rack"
end
The second last line should be
spec.add_runtime_dependency "rack"
The argument is spec, not gem.

cannot load such file -- mygem

I have created a gem called "mygem"(dummy name).
I have built the gem using cmd
gem build mygem
and I have installed the gem as well.
I checked the installation by running
gem list
My gem is listed correctly
But in the irb I am not able to load the gem. Running
require "mygem"
is thorughing the following error
cannot load such file -- myGem
Below is my gemspec
Gem::Specification.new do |spec|
spec.name = "mygem"
spec.version = Mygem::VERSION
spec.authors = ["ABC"]
spec.email = ["ABC#example.com"]
spec.summary = %q{TODO:}
spec.description = %q{TODO:}
spec.homepage = ""
spec.license = "MIT"
spec.files = `git ls-files`.split("\n")
spec.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
spec.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
spec.require_paths = ["lib"]
spec.add_development_dependency "bundler", "~> 1.6"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec"
end
It will be great if you can help me out in this. Thanks in advance
It seems that you have created myGem.rb (or like) file, containing myGem class and forget to add it to git.
Why git? Because you use git to build the list of files for your gem:
spec.files = `git ls-files`.split("\n")
To solve the issue you should either specify files to include explicitly, or add all the necessary files to git (so that git ls-files command returns everything needed for gem to be run properly.)
Hope this helps.
May be some file or class name is wrong?
The fact that the error says "myGem" instead of "mygem" makes me think that you have something like my_gem.rb or class MyGem somewhere

Distribute a Rails Generator as a Ruby Gem [duplicate]

This question already has answers here:
Rails 3 generators in gem
(2 answers)
Closed 8 years ago.
I am fairly new to Ruby/Rails framework. I have a simple generator in my Rails application at lib/generators. The generator's function is to copy one of the files in the templates/myfile.yml directory into config/myfile.yml.
Now I would like to use this generator in another app. So, I would like to package this as a gem. I have generated gem using bundler and it contains the following file structure:
lib/
mygem/
version.rb
generators/
mygem/
install/
templates/
somefile.yml
install_generator.rb
USAGE
mygem.rb
mygem.gemspec
Gemfile
Gemfile.lock
LICENSE.txt
Rakefile
README.md
Content of install_generator is as follows:
require 'rails/generators'
module Mygem
module Generators
class InstallGenerator < Rails::Generators::Base
source_root File.expand_path('../templates', __FILE__)
def generate_something
copy_file "something.yml", "#{Rails.root}/config/something.yml"
end
end
end
end
Following is the content of mygem.gemspec:
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'mygem/version'
Gem::Specification.new do |spec|
spec.name = "mygem"
spec.version = MyGem::VERSION
spec.authors = ["Swaroop SM"]
spec.email = ["myemail#gmail.com"]
spec.description = %q{Something}
spec.summary = %q{Something}
spec.homepage = ""
spec.license = "MIT"
spec.files = `git ls-files`.split($/)
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]
spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "rake"
end
I reckon there is something missing in my lib/mygem.rb
require "mygem/version"
module Mygem
# Your code goes here...
end
I have no problem in building and pushing it to rubygems.org.
Inorder to use this in one of my rails app's, I added this to the Gemfile and run bundle install. The problem is now when I run rails g mygem:install it throws an error that says:
Could not find generator mygem:install.
What am I doing wrong?
I solved the issue. I had forgotten to add rails as a dependency in my gemspec file. i.e., I had to include the following in mygem.gemspec:
spec.add_development_dependency "rails", "~> RAILS_VERSION"

How to require ''pg" gem on iron.io with iron_worker

.worker file
runtime "ruby"
name "UserMailer"
merge_gem "activerecord", "=3.2.8"
merge_gem 'actionmailer', '=3.2.8'
merge_gem 'devise', '=2.1.2'
merge_gem 'pg', "=0.14.0"
merge_file "../app/views/user_mailer/new_user.html.erb" , "user_mailer"
merge_file "../app/mailers/user_mailer.rb"
merge_dir "../app/models"
merge_exec "user_mailer_worker.rb"
.rb file
require 'action_mailer'
require 'active_record'
require 'pg'
require 'bcrypt'
require 'devise'
require 'user_mailer.rb'
require 'models/user.rb'
def init_mailer
#Need to convert to proper hashes
mailer_config = params['mailer'].inject({}) { |memo, (k, v)| memo[k.to_sym] = v; memo }
# set default views dir to current dir
ActionMailer::Base.prepend_view_path('.')
ActionMailer::Base.smtp_settings = mailer_config
ActionMailer::Base.delivery_method = :smtp
end
def setup_database
puts "Database connection details:#{params['database'].inspect}"
return unless params['database']
# estabilsh database connection
ActiveRecord::Base.establish_connection(params['database'])
end
init_mailer
setup_database
puts "I got '#{params.inspect}' parameters"
#send email!
# you could use here any active_record queries with models
UserMailer.new_user(User.find(params[:id])).deliver!
I have uploaded code to iron worker but i got following output
Skipping ruby gem with name='pg' and version='0.14.0' as it contains native extensions, switching to full remote build should fix this
and after i queue task on iron.io i got error
/usr/local/lib/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- pg (LoadError)
from /usr/local/lib/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /task/user_mailer_worker.rb:4:in `<top (required)>'
from /usr/local/lib/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /usr/local/lib/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from __runner__.rb:190:in `<main>'
how to require 'pg' gem on iron.io ??
Add this line to .worker file:
full_remote_build true

Resources