Created
May 4, 2013 21:23
-
-
Save apeiros/5518772 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'yaml' | |
class Configuration | |
def self.load_yaml(file) | |
new(YAML.load_file(file)) | |
end | |
def initialize(config, schema=nil) | |
config.each do |key, value| | |
value = Configuration.new(value) if value.is_a?(Hash) | |
instance_variable_set(:"@#{key}", value) | |
end | |
singleton_class.send :attr_reader, *config.keys | |
end | |
def [](name) | |
ivar = :"@#{name}" | |
raise KeyError, "key not found: #{name.inspect}" unless instance_variable_defined?(ivar) | |
instance_variable_get(ivar) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment