# Parses config/site.yml and creates an OpenStruct-like Site configuration object.
# The site.yml file can use ERB, just like fixture yml files.
# The file is expected to have a top-level key for the current Rails environment.
# Its values become attribute on the Site object.
# Values that have sub-keys become nested objects.
## For example, a site.yml file containing
## development:
# password: sesame
# flickr:
# api_key: <%= "ali" * 2 %>
# hot_or_not:
# api_key: baba
## creates a Site object that can be used like
## Site.password # => "sesame"
# Site.name = "Test"
# Site.name # => "Test"
# Site::Flickr.api_key # => "aliali"
# Site::HotOrNot.api_key # => "baba"
require'ostruct'moduleOpenModuledefmethod_missing(*args)@ostruct||=OpenStruct.new@ostruct.send(*args)enddefself.build_constant_from_hash(constant_name, hash, parent =Object)
parent.const_set(constant_name.classify,Module.new)
constant = parent.const_get(constant_name.classify)
constant.extend(self)
hash.each do |key,value|if value.is_a?(Hash)
build_constant_from_hash(key, value, constant)else
constant.send("#{key}=", value)endendendend
raw_config =File.read("#{Rails.root}/config/site.yml")
erb_config =ERB.new(raw_config).result
config =YAML.load(erb_config)[Rails.env]OpenModule.build_constant_from_hash('Site', config)