From fa338dd363f21135dcc77287f23a0aaa4e74159a Mon Sep 17 00:00:00 2001 From: dakkar Date: Sun, 27 Mar 2011 14:48:54 +0100 Subject: bookmarks! and output as a tree --- lib/DeWeave/Collection/Bookmarks.pm | 76 +++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 lib/DeWeave/Collection/Bookmarks.pm (limited to 'lib/DeWeave/Collection/Bookmarks.pm') diff --git a/lib/DeWeave/Collection/Bookmarks.pm b/lib/DeWeave/Collection/Bookmarks.pm new file mode 100644 index 0000000..af1cc3e --- /dev/null +++ b/lib/DeWeave/Collection/Bookmarks.pm @@ -0,0 +1,76 @@ +package DeWeave::Collection::Bookmarks; +use Moose; +use namespace::autoclean; +use DeWeave::BO::Bookmark::Base; +use DeWeave::BO::Bookmark::Bookmark; +use DeWeave::BO::Bookmark::Microsummary; +use DeWeave::BO::Bookmark::Folder; +use DeWeave::BO::Bookmark::Livemark; +use DeWeave::BO::Bookmark::Separator; +use DeWeave::BO::Bookmark::Query; +use MooseX::Types::Moose qw(HashRef); + +extends 'DeWeave::Collection'; + +has _item_cache => ( + isa => HashRef['DeWeave::BO::Bookmark::Base'], + lazy_build => 1, + traits => ['Hash'], + handles => { + item_by_id => 'get', + item_known => 'exists', + }, +); + +sub _build__item_cache { + my ($self) = @_; + + my %ret; + + for my $item (@{$self->items}) { + $ret{$item->id}=$item; + } + + return \%ret; +} + +sub item_class { + my ($class,$input_item) = @_; + + my $item_class = 'Base'; + if (exists $input_item->{type} && defined $input_item->{type}) { + $item_class = ucfirst($input_item->{type}); + } + + return "DeWeave::BO::Bookmark::$item_class"; +} + +sub as_tree { + my ($self,$start_id) = @_; + + $start_id ||= 'menu'; + return $self->_as_tree_rec($start_id,0); +} + +sub _as_tree_rec { + my ($self,$start_id,$depth) = @_; + + if (!$self->item_known($start_id)) { + return ((' ' x $depth)." $start_id is unknown\n"); + } + + my $item = $self->item_by_id($start_id); + + my $padding = ' ' x $depth; + my $out = $item->as_string; + $out =~ s/^/$padding/smg; + + if ($item->can('children')) { + for my $child_id (@{$item->children}) { + $out .= $self->_as_tree_rec($child_id,$depth+1); + } + } + return $out; +} + +1; -- cgit v1.2.3