One of Rails’ selling points is convention over configuration, but most apps do need some configuration. database.yml is a great example.
Here’s some code that loads data into a ruby class from a YAML config file. Just like database.yml the configuration is specific to the rails environment you’re running in.
class MyClass
def self.config_file
File.join(RAILS_ROOT, 'config', 'my_class.yml')
end
def self.config
YAML.load(File.read(config_file)).with_indifferent_access[RAILS_ENV]
end
def config
self.class.config
end
end
Now you can do things like this in your instance and class methods:
def where_am_i
config[:host]
end
The YAML file would look like this:
development:
host: localhost
test:
host: test.host
production:
host: your_production_host.com