no such mongoid configuration file issue - ruby

I have a minitest_helper.rb and mongoid.yml files in a directory. I putted the below code in minitest_helper;
require 'mongoid'
Mongoid.load!("mongoid.yml", :test)
Although these files in a same directory, Mongoid couldn't load yml file and I got 'no such file' as below:
/home/developer/.rvm/gems/ruby-1.9.3-p374/gems/mongoid-3.1.2/lib/mongoid/config
/environment.rb:40:in `initialize': No such file or directory - mongoid.yml
(Errno::ENOENT)
Also I don't use any framework like Rails, Sinatra etc.
What is wrong?

As you can see in the documentation, the #load! needs the full path to the file. Try changing the code into something like this:
Mongoid.load!(File.join('path_to_the_yml','starting_at_root_of_the_project', 'mongoid.yml') , :test)
The way you construct the File.join will depend on your directory structure. If I had an structure such as this:
project_root
--lib
--spec
----fixtures
------test.xml # the path for this file is project_root/spec/fixtures/test.xml
Then the File.join will look like this:
File.join('spec','fixtures', 'test.xml')

Related

Use Ruby Class In A Different Directory

I have written a few ruby classes. However, when trying to access one from another directory I am getting the following error:
uninitialized constant Main::AppVersion
This is what the directory structure looks like:
home --> a --> app_version.rb
home --> b --> c --> lib --> main.rb (and other classes)
Everything within "lib" can see each other. However, when trying to access app_version, it fails. I added the path to app version (home/a) to the $LOAD_PATH. So it should be available from there. I have also tried "requiring" my other class, but when I do that I get the following error:
LoadError: no such file to load -- AppVersion
Any idea on what I could be doing wrong here would be highly appreciated. Thanks!
Can try using require_relative:
require_relative '../../../a/app_version'
You error is:
LoadError: no such file to load -- AppVersion
So require is looking for a file AppVersion.rb. And you said the file name is app_version.rb. Try to load it with:
require 'app_version'
after setting up the $LOAD_PATH.
When you define Main::AppVersion class it's supposed to reside in main folder (i.e. main/app_version.rb)
So it doesn't depend on $LOAD_PATH.
You will have to require the file.
LoadError: no such file to load -- AppVersion
Do you require "app_version" ? Or require "AppVersion"? (first one is correct, and consider to require_relative)
Another option is to run ruby and give the include path like
ruby -Ia -Ib/c/lib b/c/lib/main.rb
.

Ruby including files

I have a Ruby app that runs on a server with no web interface. It is run using the command line(ruby path/to/file.rb).
I have classes in different files that I want to be accessible. The files are located in the "app/classes" directory.
I put this in the application.rb file:
config.autoload_paths += Dir["#{config.root}/classes"]
and I get an uninitialized constant error.
I can put in "require_relitive 'somefile'" but I would rather not have to do this for every class that is used. How do I create an autoload path and where should it be located at?
Use require_all
See https://github.com/jarmo/require_all
It basically allows you to write this:
require 'require_all'
require_all 'app/classes'
And all ruby files in app/classes will be loaded.

RSpec locating resource path

I have a little project (non Rails) and I'm using RSpec for testing. In order to load models I'm using:
require_relative "../lib/checkout"
However I'm encountering the problem with loading config files, for instance, the test no longer locates my "items.csv" when the following:
CSV.foreach("items.csv") do |row|
Note that the problem occurs only when spec is run from the spec directory, i.e:
rspec checkout_spec.rb
Running it from the project root is fine:
rspec spec/checkout_spec.rb
Any help would be appreciated.
CSV.foreach uses the current directory to find the file. You should probably use File.expand_path to get to an absolute path to items.csv to avoid this problem.
Edited to add an example
Assuming that the file is at root/items.csv, and the ruby file with this code is at root/lib/file.rb, you could write
path = File.expand_path File.join(File.dirname(__FILE__), '..', 'items.csv')
CSV.foreach path do |row|
# rest of code...

How do I require a file from inside a directory with Ruby?

I think I'm missing something here. I have a directory like this:
myapp
|-lib
|-package1
|-dostuff.rb
|-package2
|-dostuff.rb
From an irb console I'm trying to test the library before I add it to my real project (a Rails app). However, typing this:
require 'lib/package1/dostuff'
returns an error saying it can't find the file to load. I added the lib directory to the load path but I'm not able to load the file.
What am I forgetting? The two filenames don't have to be the same but that's how they are to begin with (they are auto-generated from some web services I need to call using soap4r; each package represents a different web service API group)
If the directory "lib" is in the load path, the argument to require must be relative to lib. So require 'package1/dostuff' without the lib, otherwise it will look for lib/lib/package1/dostuff.rb.
In Ruby 1.9 there's the new require_relative method, which would let you do require_relative "../package2/dostuff" from within package1/dostuff.rb.

unshift + file.join in ruby

$:.unshift File.join(File.dirname(__FILE__),\
'vendor','addressable-2.1.0','lib','addressable','uri')
Does the code above access a file that has this path:
'vendor/addressable-2.1.0/lib/addressable/uri'
I'm trying to vendor the addressable gem into a Sinatra app to deploy it to my hosting provider but I keep receiving:
"no such file to load -- addressable/uri"
after putting the 'unshift' line in config.ru.
The above code adds the path "vendor/addressable-2.1.0/lib/addressable/uri" to the global variable used for looking up external files. The path will be relative to the directory that houses the file this code is placed in. So were {dir} is the directory config.ru is placed, it will add {dir}/vendor/addressable-2.1.0/lib/addressable/uri to the lookup path for includes.
What the line does is it puts the path 'vendor/addressable-2.1.0/lib/addressable/uri' (relative to the directory your ruby script is in) into the load path, which is the list of directories ruby looks through when looking for files you require.
By itself the line won't try to access any files.

Resources