Deleted everything I and started over with a new approach. I'll refactor this later most likly but here was the basic idea.
def walk_until_fork(tree_hash, root_node = nil)
root_node ||= ActiveSupport::OrderedHash.new
tree_hash.each do |node,children|
more_than_one = false
parent_node = root_node[node] = ActiveSupport::OrderedHash.new
children.each do |k,v|
fork_node = more_than_one ? parent_node : root_node
walk_until_fork({k => v},fork_node)
more_than_one = true
end
end
root_node
end
Then I just call with:
walk_until_fork(self.arrange)
The code passes a hash reference around to the current root, changing it to a new hash only there is more then one child.