Skip to content

Instantly share code, notes, and snippets.

@ajlai
Created April 2, 2012 22:27
Show Gist options
  • Save ajlai/2287633 to your computer and use it in GitHub Desktop.
Save ajlai/2287633 to your computer and use it in GitHub Desktop.
YamlNav
require 'psych'
require 'pathname'
# Hash-like access to objects stored in yaml files in directory structures.
#
# Examples:
# CONFIGS = YamlNav.new(Rails.root + "config")
#
# CONFIGS["exceptional.yml"]["api-key"]
# # => the api-key stored in config/exceptional.yml
#
# CONFIGS["foo"]["bar"]["nonexistent.yml"]["baz"]
# # raises (Errno::ENOENT: No such file or directory config/foo/bar/nonexistent.yml)
class YamlNav
def initialize(path)
@pathname = Pathname.new(path)
end
def [](base)
pathname_base = @pathname + base
pathname_base.extname == ".yml" ? Psych.load_file(pathname_base) : self.class.new(pathname_base)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment