YAML dependency

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 ?

···

--
Posted via http://www.ruby-forum.com/.

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...

···

On Tue, Nov 9, 2010 at 4:02 PM, Guilherme Mello <javaplayer@gmail.com> wrote:

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 ?

--
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.

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.

···

On Tue, Nov 9, 2010 at 12:20 PM, Phillip Gawlowski <cmdjackryan@googlemail.com> wrote:

On Tue, Nov 9, 2010 at 4:02 PM, Guilherme Mello <javaplayer@gmail.com> wrote:

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...

--
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