1
def self.directory_hash(path, name=nil)
    data = {:parent => (name || path)}
    data[:children] = children = []
    Dir.foreach(path) do |entry|
      next if (entry == '..' || entry == '.')
      full_path = File.join(path, entry)
      if File.directory?(full_path)
        children << directory_hash(full_path, entry)
      else
      children << entry
      end
    end
    return data
  end

I used above method that returns the json given below:-

{:parent=>"public", :children=>["422.html", {:parent=>"applications", :children=>[{:parent=>"1", :children=>[{:parent=>"1", :children=>[{:parent=>"1", :children=>["configurations.xml"]}, {:parent=>"2", :children=>["configurations.xml"]}, "Projects.rar"]}, {:parent=>"2", :children=>[{:parent=>"9", :children=>["configurations.xml"]}, "rest.rar", {:parent=>"5", :children=>["configurations.xml"]}, {:parent=>"6", :children=>["configurations.xml"]}, {:parent=>"3", :children=>["configurations.xml"]}, {:parent=>"4", :children=>["configurations.xml"]}]}]}, {:parent=>"2", :children=>[{:parent=>"3", :children=>["Projects.rar"]}, {:parent=>"4", :children=>["rest.rar"]}]}]}, "500.html", "robots.txt", "favicon.ico", {:parent=>"data", :children=>[]}, "_index.html", "404.html"]}

Now in my view i want to show structure from above output so the view should look like

public
 422.html
 applications
  1
   1
    1
    2
  2
.....
.....
.....

An So On

I have a variable that keeps this json

@structure = ApplicationVersion.directory_hash("public")

So what code i will need to write to read the above json so that tree could be constructed?

3
  • its not json, its a hash. Commented Mar 1, 2013 at 10:39
  • Oh !!, Can you suggest how to create tree structure from this hash Commented Mar 1, 2013 at 10:40
  • 1
    it is already a tree structure, if you want to know how to display it, I would advice doing it in the browser with some javascript library, like jstree.com Commented Mar 1, 2013 at 11:42

1 Answer 1

1

Try:

your_hash = {:parent=>"public", :children=>["422.html", {:parent=>"applications", :children=>[{:parent=>"1", :children=>[{:parent=>"1", :children=>[{:parent=>"1", :children=>["configurations.xml"]}, {:parent=>"2", :children=>["configurations.xml"]}, "Projects.rar"]}, {:parent=>"2", :children=>[{:parent=>"9", :children=>["configurations.xml"]}, "rest.rar", {:parent=>"5", :children=>["configurations.xml"]}, {:parent=>"6", :children=>["configurations.xml"]}, {:parent=>"3", :children=>["configurations.xml"]}, {:parent=>"4", :children=>["configurations.xml"]}]}]}, {:parent=>"2", :children=>[{:parent=>"3", :children=>["Projects.rar"]}, {:parent=>"4", :children=>["rest.rar"]}]}]}, "500.html", "robots.txt", "favicon.ico", {:parent=>"data", :children=>[]}, "_index.html", "404.html"]}  

new_hash= your_hash.to_s.gsub(/(\[\"\S*\"\])/,'').gsub(/(\[|\]|\{|\})/,'').gsub('=>',',').gsub(',,',',')

new_hash.split(',').each {|i| puts i if i.strip[0] != ":"}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your response. It has worked for me. Just another thing i want to know if this is possible to create <ul> and <li> tags for that output produced by above code so that i could use JsTree plugin?
I want to create the above output in the following format:- <div id="demo1" class="demo" style="height:100px;"> <ul> <li id="phtml_1"><a href="#">Root node 1</a> <ul> <li id="phtml_11"><a href="#">Child node 1</a> <ul> <li id="phtml_111"><a href="#">Child node 11</a></li> <li id="phtml_222"><a href="#">Child node 22</a></li> </ul> </li> <li id="phtml_21"><a href="#">Child node 2</a></li> </ul> </li> </ul> </div>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.