Skip to content

Instantly share code, notes, and snippets.

@apeiros
Created May 4, 2013 21:23
Show Gist options
  • Save apeiros/5518772 to your computer and use it in GitHub Desktop.
Save apeiros/5518772 to your computer and use it in GitHub Desktop.
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