How to Require a Custom Gem - ruby

I'm writing a gem that depends on another gem I've created.
In my host gem, I'm requiring my gem as a dependency like this:
$:.push File.expand_path('../lib', __FILE__)
Gem::Specification.new do |s|
s.require_path = "lib"
s.files = Dir["lib/**/*"]
s.test_files = Dir["spec/**/*"]
s.add_dependency "my_other_gem"
end
My gemfile looks like this:
source "http://rubygems.org"
gem 'my_other_gem' path: '../my_other_gem', require: 'my_other_gem'
gemspec
And inside the host gem, I've got a class that requires my_other_gem:
require 'my_other_gem'
In my_other_gem, inside lib/my_other_gem.rb, I've got two more require classes. So it looks like this:
require 'my_other_gem/foo'
require 'my_other_gem/bar'
When I spin up IRB in the host app and run require 'my_other_gem', I get this error
LoadError: cannot load such file -- my_other_gem
When I'm playing in the my_other_gem directory and I spin up IRB, the same require 'my_other_gem' command does not error out. Everything runs normally. But for some reason I can't require my_other_gem when I'm in my host gem.
What step am I missing?

How do you start irb? You need to run it in the bundler context with bundle exec.
I just tried and if I just run irb, I get the same error.
But if I run bundle exec irb, it works.

Related

Ruby gem dependency loading

I'm putting some shared models for a rails app inside it's own gem. It's just models, so I'm not using an engine. Getting it set up seemed to work fine until I added the "acts_as_list" gem.
# gem - domain.gemspec
spec.add_dependency "acts_as_list"
# gem - lib/domain.rb
require "acts_as_list"
# gem - lib/domain/models/page.rb
acts_as_list scope: [:ancestry]
This works fine in the console for my gem, I can run methods specific to acts_as_list as usual. However, when I add my gem to another project, it gives me an error.
# project - Gemfile
gem "www_domain", path: "../www_domain"
Bundler::GemRequireError: There was an error while trying to load the gem 'domain'.
NoMethodError: undefined method `acts_as_list' for #<Class:0x0055da70121ab0>
/home/shaun/sites/demo/domain/lib/domain/models/page.rb:32:in `<class:Page>'
Is there something special I have to do in this case to access the acts_as_list method because my model is inside a gem?
Update: Here is my complete lib/domain.rb file for the gem:
require "yaml"
require "active_record"
require "acts_as_list"
require "ancestry"
# rbfiles = File.join(File.dirname(__FILE__), "lib", "**", "*.rb")
# Dir.glob(rbfiles).each do |file|
# require file.gsub("lib/", "")
# end
module Domain
# Your code goes here...
def self.root
File.dirname(__dir__)
end
def self.db_config
YAML.load_file("#{Domain.root}/db/config.yml")["development"]
end
end
require "domain/version"
require "domain/models/page"
require "domain/models/menu"
require "domain/models/article"
require "domain/models/page_part"
I can use acts_as_list and ancestry methods in the console of my gem (running bin/console from the gem directory). But the project console (running bundle exec rails console from the project directory) will not start because of the gem error I mentioned.
This might result from a load order issue if the model being required before the require :acts_as_list statement. Check to see if the gem specifies the load order. If not, you could try something like:
# gem - lib/domain.rb
require "acts_as_list"
require "models/page"
If the load order is unclear, I find it helpful to simply add a
puts __FILE__
at the top of the relevant source files.

Trying to run migrations in Sinatra but can't load Sinatra app

I'm looking to run migrations for a Sinatra app called "sinatra_active_record_start" but can't get my settings right.
When I run bundle exec rake -T I get:
LoadError: cannot load such file -- sinatra_active_record_start
/Users/jasonnappy/ga_wdi/exisiting_resources/wdi_london/resources/materials/local/06-server-applications/ruby/sinatra/active-record/sinatra_active_record_start/Rakefile:1:in `require'
Same as when I run:
bundle exec rake db:create_migration first_migration
My Gemfile is:
source "https://rubygems.org"
gem "sinatra"
gem "activerecord"
gem "sinatra-activerecord"
gem "rake"
gem "thin"
My Rakefile is:
require "sinatra_active_record_start"
require "sinatra/activerecord/rake"
namespace :db do
desc "Migrate the database"
task(:migrate => :environment) do
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Migration.verbose = true
ActiveRecord::Migrator.migrate("db/migrate")
end
end
The top of app.rb is:
require "bundler/setup"
require "sinatra"
require "activerecord"
require "sinatra/activerecord"
I know there are some redundancies, but at this point, I'm just trying to plug in and make something work that I found on the internet.
First, it doesn't look like you are requiring an adapter for your database. Adding one, like
gem "sqlite3"
to your Gemfile, should fix that.
Second, sinatra/activerecord creates migrations in a directory called "db/migrate" by default. That's where your migrations should live, not the root directory.
Move your migration there and remove
require "sinatra_active_record_start"
from your Rakefile. That's the code that is causing the immediate error. You shouldn't need to require each migration in the Rakefile.
Following these steps should make your migrations run, although you should rename the file to follow ActiveRecord convention. Run
rake db:create_migration NAME='sinatra_active_record_start'
to create a new one with a timestamp.
"Sinatra Active Record Starter Kit" is an example repo to help you get started.

How do I include gems in Rake files without specifying them in a Gemfile?

I am trying to write my first Rake file. It looks something like this:
lib\tasks\routes_check.rake:
require 'curb'
task :route_test do
puts "checking routes"
end
Running rake route_test fails with the message:
rake aborted!
no such file to load -- curb
I ran it with --trace and the trace is here.
I can't figure out why this does not work. In the same lib/tasks directory, I saved a .rb file which looks like:
task.rb:
require 'curb'
puts "Hello"
When I run this file with ruby task.rb, it works fine. What am I doing wrong here?
Rails applications by default use Bundler, so you need to add gem "curb" to your Gemfile and then ”install” (register) the new gem to Rails bundle with gem install.

Strange require statement errors in Ruby?

I've got a wrapper for my Gem, socks, inside socks.rb. The entire file is made up of require statements, and a module declaration:
# lib/socks.rb
require 'socks/version'
require 'socks/base_controller'
require 'socks/templates'
require 'socks/tasks'
require 'socks/rake_tasks'
module Socks
end
However, require 'socks/tasks' and socks/rake_tasks is giving me a LoadError: no such file to load -- socks/tasks / rake_tasks error.
Is this a problem with the alignment of the require statements, or just the code?
Code is on Github: https://github.com/Beakr/socks
EDIT: require './socks/tasks' is now working, however require './socks/rake_tasks' is not.
Ruby load files using its $LOAD_PATH. This global array is changed by e.g. rubygems and bundler to allow to find libraries in various locations. In your sock.gemspec you have defined
gem.require_paths = ["lib"]
which means that rubygems will add the lib directory of your gem to ruby's $LOAD_PATH. But it odes so only if you have installed the gem and the gemspec is thus evaluated. If you don't want to install your gem, you can test your gem using
bundle exec irb
in your gem directory, or alternatively by first adapting your $LOAD_PATH in your irb session like so:
$LOAD_PATH.push "/path/to/your/gem/lib"
require 'socks'

Ruby: What does 'require: false' in Gemfile mean?

Does this:
gem 'whenever', require: false
mean that the gem needs to be installed, or does it mean it is not required?
This means install the gem, but do not call require when you start Bundler. So you will need to manually call
require "whenever"
if you want to use the library.
If you were to do
gem "whenever", require: "whereever"
then bundler would download the gem named whenever, but would call
require "whereever"
This is often used if the name of library to require is different than the name of the gem.
You use :require => false when you want the gem to be installed but not "required".
So in the example you gave:
gem 'whenever', :require => false
when someone runs bundle install the whenever gem would be installed as with gem install whenever. Whenever is used to create cron jobs by running a rake task but isn't usually used from within the rails (or other framework if not rails) application.
So you can use :require => false for anything that you need to run from the command line but don't need within your code.
require: false tells Bundler.require not to require that specific gem: the gem must be required explicitly via require 'gem'.
This option does not affect:
bundle install: the gem will get installed regardless
the require search path setup by bundler.
Bundler adds things to the path when you do either of:
Bundle.setup
which is called by require bundler/setup
which is called by bundle exec
Example
Gemfile
source 'https://rubygems.org'
gem 'haml'
gem 'faker', require: false
main.rb
# Fail because we haven't done Bundler.require yet.
# bundle exec does not automatically require anything for us,
# it only puts them in the require path.
begin Haml; rescue NameError; else raise; end
begin Faker; rescue NameError; else raise; end
# The Bundler object is automatically required on `bundle exec`.
Bundler.require
Haml
# Not required because of the require: false on the Gemfile.
# THIS is what `require: false` does.
begin Faker; rescue NameError; else raise; end
# Faker is in the path because Bundle.setup is done automatically
# when we use `bundle exec`. This is not affected by `require: false`.
require 'faker'
Faker
Then the following won't raise exceptions:
bundle install --path=.bundle
bundle exec ruby main.rb
On GitHub for you to play with it.
Rails usage
As explained in the initialization tutorial, the default Rails template runs on startup:
config/boot.rb
config/application.rb
config/boot.rb contains:
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
which does the require 'bundler/setup' and sets up the require path.
config/application.rb does:
Bundler.require(:default, Rails.env)
which actually requires the gems.
Whenever you specify a Gem in your Gemfile and run bundle install, bundler will go and install specified gem and load code for that Gem in you app by putting require 'whenever' this way bundler will load code for all of your Gems in your Rails app, and you can call any method from any Gem without any pain, like you do most of the time.
but Gems like whenever, faker or capistrano are something which you do not need in your app code you need whenever code in your schedule.rb file to manage crons and capistrano code in deploy.rb file to customize deployment recipe so you need not to load code for these gems in your app code
and wherever you want to call any method from these Gems you can manually require thsese gems by yourself by putting require "whenever" . so you put :require => false in your Gemfile for these Gems, this way bundler will install that Gem but not load code for that Gem itself, you can do it whenever you want by simply putting like require 'whenever' in your case.
Analogy to Explain
## Gemfile
gem "university_degree", require: false
gem "dealing_with_boss"
"dealing_with_boss" - loaded into memory and ready to go.
degree gem - not "needed"....you need to manually require it, in order to use it.
In order to require gems in your Gemfile, you will need to call Bundler.require.
You can prevent bundler from requiring the gem with require: false, but it will still install and maintain the gem. Check this out for a more detailed explanation.

Resources