Description
Simple wrapper for YAML config files.
Simple example
# File config/app_config.yml
# development:
# app_host:
# name: google.com
# port: 80
# End of config/app_config.yml
require 'yaml_config'
config = YamlConfig.new(File.join(Rails.root, "config/app_config.yml"), :root => RAILS_ENV)
config.get(:app_host)[:name] # -> "google.com"
config.get(:app_host)[:port] # -> 80
config.get(:app_host)[:some] # -> NullProperty
Different initializations
# using filename
filename = File.join(Rails.root, "config/app_config.yml")
config = YamlConfig.new(filename, :root => RAILS_ENV)
# using open stream
File.open(filename, "r") do |file|
config = YamlConfig.new(file, :root => RAILS_ENV)
end
# using data string
config = YamlConfig.new(File.read(filename), :root => RAILS_ENV)
Singleton version
In many cases (rails application is the best example) developer needs only one file for whole project. To accomplish this it possible to use singleton version of YamlConfig.
# environment.rb example:
require "yaml_config"
...
filename = File.join(Rails.root, "config/app_config.yml")
config = AppYamlConfig.instance.init!(filename, :root => RAILS_ENV)
...
# Usage
config.get(:username)