Created
June 28, 2016 23:08
-
-
Save dasibre/0f1de48590064e4126440b7c4933cd10 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def construct_concept(concept) | |
h = {} | |
c = Hash[*concept.to_a.first] | |
c['paths'] = [[Hash[*concept.to_a.first]]] | |
h[concept['id']] = c | |
h | |
end | |
def parent_child_concepts(concepts) | |
pc = {} | |
pc[:parent] = Hash[*concepts.first] | |
pc[:children] = concepts.values_at('children').flatten.map {|child| parent_child_concepts(child)} || [] | |
pc | |
end | |
def add_parent_child_paths(parent_hash,children_array) | |
h = {} | |
parent_hash.each do |parent_key,parent_value| | |
h[parent_key] = parent_value | |
children_array.each do |child| | |
child.each do |k,v| | |
h[k] = v | |
h[k]['paths'].map {|path| path.unshift({'id' => parent_key})} | |
end | |
end | |
end | |
h | |
end | |
def build_concept_data(concepts) | |
#{"id"=>"apple", "children"=>[{"id"=>"ipad"}]} | |
parsed_concepts = parent_child_concepts(concepts) | |
parent = construct_concept(parsed_concepts[:parent]) | |
children = parsed_concepts[:children].each_with_object([]) {|child,accu| accu << construct_concept(child)} | |
concept_paths_data = add_parent_child_paths(parent,children) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment