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.

Methods

==   []   []=   delete   empty?   expand   flat   index   inspect   keys   new   ordered   ordered!   to_string_array  

Public Class methods

Initialize PathHash.

hsh - Priming Hash.

Public Instance methods

Behaves like the usual Hash#[] method, but you can access nested hash values by composing a single key of the traversing keys joined by ’/’:

  hash['c']['d'] # is the same as:
  hash['c/d']

Same as above, except sets value rather than retrieving it.

Delete entry from Hash. Slashed keys can be used here, too.

key - The key to delete. block - Produces the return value if key not found.

Returns delete value.

Expands the whole hash to Hash objects … not useful very often, it seems.

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']

This gives you the slashed key of the value, no matter where the value is in the tree.

This gives you only the top-level keys, no slashes. To get the list of slashed keys, do hash.flat.keys

Same as ordered! but returns a new SlashedHash object instead of modifying the same.

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!

[Validate]