Reputation: 5761
I'm writting a ruby gem and I want to let user use your own yaml file configuration, but I don't know how to test if the user_config.yml exists into rails config path.
I want to use user_config.yml only if this file exists into config path.
Upvotes: 5
Views: 5496
Reputation: 16355
Rails.root
(or RAILS_ROOT
in earlier versions of Rails) gives you the path of the application root. From there, you can see if the file you're interested in exists.
e.g.
path = File.join(Rails.root, "config", "user_config.yml")
if File.exists?(path)
# do something
end
Upvotes: 13
Reputation: 263
you can access your Rails App's load path with the $LOAD_PATH variable which is of type Array.
Thus
if $LOAD_PATH.include?('/path/where/user/should/put/their/user_config.yml')
# EDIT
config_options = File.new('rails_app/config/user_config.yml', 'r')
else
# EDIT
config_options = File.new('path/to/default/config.yml', 'r')
end
There is also another useful method for the Ruby File class called #exists? which will of course check to see if a file exists. See File class docs for more.
Hope this gets you started. Your question was rather vague, reply with more details if you need more help.
Upvotes: 1