Class | Hashery::PathHash |
In: |
lib/hashery/path_hash.rb
|
Parent: | Hash |
A PathHash is a hash whose values can be accessed in the normal manner, or with keys that are slash (`/`) separated strings. To get the whole hash as a single flattened level, call `flat`. All keys are converted to strings. All end-of-the-chain values are kept in whatever value they are.
s = PathHash['a' => 'b', 'c' => {'d' => :e}] s['a'] #=> 'b' s['c'] #=> {slashed: 'd'=>:e} s['c']['d'] #=> :e s['c/d'] #=> :e
PathHash is derived from the SlashedHash class in the HashMagic project by Daniel Parker <gems@behindlogic.com>.
Copyright (c) 2006 BehindLogic (hash_magic.rubyforge.org)
Authors: Daniel Parker
TODO: This class is very much a work in progess and will be substantially rewritten for future versions.
Gives a list of all keys in all levels in the multi-level hash, joined by slashes.
{'a'=>{'b'=>'c', 'c'=>'d'}, 'b'=>'c'}.slashed.flat.keys #=> ['a/b', 'a/c', 'b']
Sets the SlashedArray as ordered. The *keys_in_order must be a flat array of slashed keys that specify the order for each level:
s = {'a'=>{'b'=>'c', 'c'=>'d'}, 'b'=>'c'}.slashed s.ordered!('b', 'a/c', 'a/b') s.expand # => {'b'=>'c', 'a'=>{'c'=>'d', 'b'=>'c'}} # Note that the expanded hashes will *still* be ordered!