On Thursday, June 26, 2014 10:46:50 PM UTC-7, Hiroyuki Sato wrote:
>
> I wrote find_from_top method. 
> I want to simplify this. but It seems work fine.
>
> def traverse_to_leaf(tree,node)
>   ret = nil
>   return node if tree.empty?
>   cur_name = tree.shift
>
>   leaf = node.children_dataset.first(:name => cur_name)
>   if( leaf )
>     ret = traverse_to_leaf(tree,leaf)
>   end
>   ret
> end
>
> def find_from_top(search_path)
>
>   root = FileTree.root
>   nodes = search_path.split('/')[1..-1]
>   traverse_to_leaf(nodes,root)
>
> end
>

I would recommend an iterative approach:

def find_from_top(search_path)
  current = FileTree.root
  search_path[1..-1].split('/').each do |name|
    break unless current = current.children_dataset.first(:name=>name)
  end
  current
end

I didn't actually test that, but hopefully it or something similar works.


 

> An approach using joins:
>>
>> Node.join(:nodes___n1, :parent_id=>:id).
>>   join(:nodes___n2, :parent_id=>:id).
>>   first(:nodes__name=>'a', :n1__name=>'b', :n2__name=>'c')
>>
>
> joins approach seems good.
> But I'm not sure how to add multiple path dynamically. 
> I'll check it.
>

It actually looks very similar to the code above:

def find_from_top_join(search_path)
  ds = FileTree.roots_dataset
  search_path[1..-1].split('/').each_with_index do |name, i|
    ds = ds.join(:file_trees.as("n#{i}"), :parent_id=>:id, :name=>name)
  end
  ds.first
end
 
Again, not tested, but hopefully it or something similar works.

Thanks,
Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to