Instance Method Details

This method will interpret each key of your Hash as a
TreeNode. Nested hashes are expected and child nodes will be added
accordingly. If a hash key is a single value that value will be used as the
name for the node. If a hash key is an Array, both node name and content
will be populated.

A leaf element of the tree should be represented as a hash key with
corresponding value nil or {}.

Examples:

TreeNode.from_hash({:A=>{:B=>{},:C=>{:D=>{},:E=>{}}}})# would be parsed into the following tree structure:
# A
# / \
# B C
# / \
# D E
# The same tree would result from this nil-terminated Hash
{:A=>{:B=>nil,:C=>{:D=>nil,:E=>nil}}}# A tree with equivalent structure but with content present for
# nodes A and D could be built from a hash like this:
{[:A,"A content"]=>{:B=>{},:C=>{[:D,"D content"]=>{},:E=>{}}}}

# File 'lib/tree/utils/hash_converter.rb', line 96deffrom_hash(hash)raiseArgumentError,'Argument must be a type of hash'\
unlesshash.is_a?(Hash)raiseArgumentError,'Hash must have one top-level element'\
ifhash.size!=1root,children=hash.firstunless[Hash,NilClass].include?(children.class)raiseArgumentError,'Invalid child. Must be nil or hash.'endnode=self.new(*root)node.add_from_hash(children)unlesschildren.nil?nodeend