YAML dependency

  • Thread starter Guilherme Mello
  • Start date
G

Guilherme Mello

Hi,

I'm creating a Gem and the code uses data from a YAML file:

CONFIG = YAML.load_file("config/file.yml")["development"]

This code is fixed in the class and located at Rails.root/config. The
Gem looks to that path and loads the YAML.

The question is: How allow the user to set what environment will be used
outside of Gem ?
 
P

Phillip Gawlowski

Hi,

I'm creating a Gem and the code uses data from a YAML file:

CONFIG = YAML.load_file("config/file.yml")["development"]

This code is fixed in the class and located at Rails.root/config. The
Gem looks to that path and loads the YAML.

The question is: How allow the user to set what environment will be used
outside of Gem ?

Don't hard code the pathname, instead provide a default filename if

myinstance = YourClass.new # or whatever makes sense for your gem

doesn't pass a valid filename. Or check if you are running in Rails,
and then supply a default filename, or...

--
Phillip Gawlowski

Though the folk I have met,
(Ah, how soon!) they forget
When I've moved on to some other place,
There may be one or two,
When I've played and passed through,
Who'll remember my song or my face.
 
R

Rick DeNatale

Hi,

I'm creating a Gem and the code uses data from a YAML file:

CONFIG = YAML.load_file("config/file.yml")["development"]

This code is fixed in the class and located at Rails.root/config. The
Gem looks to that path and loads the YAML.

The question is: How allow the user to set what environment will be used
outside of Gem ?

Don't hard code the pathname, instead provide a default filename if

myinstance = YourClass.new # or whatever makes sense for your gem

doesn't pass a valid filename. Or check if you are running in Rails,
and then supply a default filename, or...

Well from the OP I take it he is running in Rails due to the reference
to Rails.root/config, and I think he's trying to emulate the kind of
thing used to differentiate on the RAILS_ENV environment value in
files like config/database.yml where the load produces a hash keyed on
the environment. So, assuming the gem always is used in a Rails
environment something like:


CONFIG = YAML.load_file(Rails.root + "config/file.yml")[Rails.env]

Rails.root returns a Pathname object, and Rails.env returns the value
of ENV['RAILS_ENV'] which was the old school way before The Rails
module got the env singleton method.

--
Rick DeNatale

Help fund my talk at Ruby Conf 2010:http://pledgie.com/campaigns/13677
Blog: http://talklikeaduck.denhaven2.com/
Github: http://github.com/rubyredrick
Twitter: @RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top