blib/lib/CatalystX/Menu/Tree.pm

filter: Statementstmt 84.7% 89 / 105 Branchbran 61.5% 16 / 26 Conditioncond 60.0% 3 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 8 / 8 Podpod 100% 1 / 1 totaltotal 79.7% 118 / 148 CC 25 SCAR 24.0
CC
25
Cov
79%
_build_tree
15.3
_get_actions
12.2
new
3.3
CRAP 11.0
↑ Summary
1 package CatalystX::Menu::Tree;
2  
3 2 ▶use 5.008000;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use MRO::Compat;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 $VERSION = '0.02';
11  
12 =head1 NAME
13  
14 CatalystX::Menu::Tree - Generate Catalyst application menus
15  
16 =head1 SYNOPSIS
17  
18 my $tree = CatalystX::Menu::Tree->new(
19 context => $c,
20 namespaces => [ $c->namespace ],
21 filter => sub {
22 my ($c, %action) = @_;
23 # return the list of k/v pairs that meet criteria
24 },
25 menupath_attr => 'MenuPath',
26 menutitle_attr => 'MenuTitle',
27 add_nodes => [
28 {
29 menupath => '/Bargains',
30 menutitle => 'Cheap stuff',
31 uri => '/products/cheap',
32 },
33 {
34 menupath => '/Returns',
35 menutitle => 'Return a product',
36 uri => '/products/returns',
37 },
38 ],
39 );
40  
41 =head1 DESCRIPTION
42  
43 Builds the tree used by CatalystX::Menu::Suckerfish to construct an HTML UL
44 element for use as a degradable, CSS-styled menu or horizontal navbar.
45  
46 Catalyst actions with the Private attribute are excluded from the tree.
47  
48 =head2 Menu Attributes
49  
50 =over
51  
52 =item menupath_attr
53  
54 Names the action attribute that contains the menu path:
55  
56 menupath_attr => 'MenuPath'
57  
58 # and in your controller:
59  
60 sub foobar :Local
61 :MenuPath(/Foo/Bar)
62 :MenuTitle('Foobar and stuff')
63 { ... }
64  
65 Only actions with the menupath_attr attribute are processed. This attribute's
66 value determines where the action's menu item is placed in the menu structure
67 (HTML UL).
68  
69 Depending on the attribute values collected from the processed actions, there
70 may be menu items containing only text. If you want a link to a landing page,
71 for example, instead of text, include an action for the landing page with the
72 appropriate MenuPath attribute in your controller, or add an entry manually
73 with the add_nodes parameter.
74  
75 =item menutitle_attr
76  
77 The menutitle_attr attribute will be used to add the HTML title attribute to
78 each list item. This should result in a balloon text with the title when the
79 pointing device hovers over each list item.
80  
81 =back
82  
83 Suckerfish menus: http://www.alistapart.com/articles/dropdowns
84  
85 Superfish jQuery menu plugin: http://users.tpg.com.au/j_birch/plugins/superfish/
86  
87 =head1 METHODS
88  
89 =cut
90  
91 =head2 C<new( %params )>
92  
93 Return an instance of this class.
94  
95 Params
96  
97 =over
98  
99 =item context
100  
101 The Catalyst application context (usually $c or $ctx in your controller).
102  
103 =item menupath_attr
104  
105 The action attribute that contains the menu tree path to the menu item
106 to be inserted for each action.
107  
108 =item menutitle_attr
109  
110 The action attribute that contains text describing each action. This
111 text is applied as a "title" attribute to the menu item's HTML container
112 so that a tooltip will be displayed when the pointer hovers over the
113 menu item.
114  
115 This text can be used anywhere in your application where a description
116 of an action is useful.
117  
118 =item namespaces
119  
120 A reference to an array of action namespaces from which actions with the
121 menupath_attr attribute should be collected.
122  
123 =item filter
124  
125 A reference to a subroutine that takes the Catalyst context and a hash of
126 action name/action object and returns the name/action pairs that meet certain
127 criteria.
128  
129 =item add_nodes
130  
131 A list of hash references containing data defining arbitrary menu items to
132 be merged into the menu tree.
133  
134 =back
135  
136 =cut
137  
138 4 ▶sub new {
Subroutine new: called CC=3 SCAR=12.0
Pod documented
139 4 my $class = shift;
140 4 ▶ if (@_ && @_ % 2 != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and @_ % 2 != 0) 0 4 4
Condition33%1/3
@_ and @_ % 2 != 0
!ll&&!rl&&r
040
MC/DC0%0/2
@_ and @_ % 2 != 0
@_ @_ % 2 != 0
Truth table33%1/3
A and B
A= @_B= @_ % 2 != 0
ABresult
0X0
100
111
141 0 die 'expected list of key/value pairs';
142 }
143  
144 4 my $self = { @_ };
145  
146 4 bless $self, $class;
147  
148 4 $self->_build_tree;
149  
150 4 return $self;
151 }
152  
153 =head1 INTERNAL METHODS
154  
155 =head2 C<_build_tree()>
156  
157 Create and store a reference to a hash containing a tree of menu data.
158  
159 =cut
160  
161 4 ▶sub _build_tree {
Subroutine _build_tree: called CC=14 SCAR=27.3
162 4 my ($self) = @_;
163  
164 4 my $c = $self->{context};
165 4 $self->_get_actions;
166 4 my $actions = $self->{action_hash};
167  
168 4 my @data;
169  
170 4 my $menpattr = $self->{menupath_attr};
171 4 my $mentattr = $self->{menutitle_attr};
172  
173 4 for my $namespace (keys %$actions) {
174 4 for my $name (keys %{$actions->{$namespace}}) {
175 8 my $action = $actions->{$namespace}{$name};
176 my %data = (
177 8 menupath => $action->attributes->{$menpattr}[0],
178 uri => $c->uri_for($action),
179 );
180 8 ▶ if ($mentattr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mentattr) 4 4 8
181 4 $data{menutitle} = $action->attributes->{$mentattr}[0];
182 }
183 8 push @data, { %data };
184 }
185 }
186  
187 # mix in any nodes the user wants to add
188 4 ▶ if ($self->{add_nodes}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"add_nodes"}) 4 0 4
189 4 for my $node (@{$self->{add_nodes}}) {
190 4 push @data, $node;
191 }
192 }
193  
194 my @sorted =
195 12 sort { $b->[0] cmp $a->[0] }
196 12 map { $_->[0] =~ s!^/!!; $_ }
197 4 map { [ $_->{menupath}, $_ ] }
198 @data;
199  
200 4 my %tree;
201  
202 4 for my $obj (@sorted) {
203 12 my $mpath = $obj->[1]{menupath};
204 12 ▶ my $mtitle = $obj->[1]{menutitle} || '';
Condition100%2/2
$obj->[1]{'menutitle'} || ''
l!l
66
MC/DC100%1/1
$obj->[1]{'menutitle'} || ''
$obj->[1]{'menutitle'}
Truth table100%2/2
A
A= $obj->[1]{'menutitle'}
Aresult
11
00
205 12 $mpath =~ s!^/!!;
206 12 my $uri = $obj->[1]{uri};
207 12 my @path = split m!/!, $mpath;
208 12 my $str = join ', ' => @path;
209 12 my $node = pop @path;
210 12 my $ref = \%tree;
211 12 ▶ while (@path) {
Branch: 1 branch
BranchTrueFalseTotal
if (@path) 8 12 20
212 8 my $key = shift @path;
213 8 ▶ if (exists $ref->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ref->{$key}) { } 0 8 8
214 0 ▶ unless (exists $ref->{$key}{children}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $ref->{$key}{"children"}) 0 0 0
215 0 $ref->{$key}{children} = {};
216 }
217 0 $ref = $ref->{$key}{children};
218 }
219 else {
220 8 $ref->{$key} = {
221 children => {},
222 };
223 8 $ref = $ref->{$key}{children};
224 }
225 }
226  
227 # this addresses the case where a top level node is added
228 # with add_node to attach a URI or title to a label
229 12 ▶ if (exists $ref->{$node}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ref->{$node}) { } 0 12 12
230 0 $ref->{$node}{uri} = $uri;
231 0 ▶ if ($mtitle) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mtitle) 0 0 0
232 0 $ref->{$node}{menutitle} = $mtitle;
233 }
234 }
235 else {
236 12 my %data = (
237 children => {},
238 uri => $uri,
239 );
240 12 ▶ if ($mtitle) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mtitle) 6 6 12
241 6 $data{menutitle} = $mtitle;
242 }
243 12 $ref->{$node} = { %data };
244 }
245 }
246  
247 4 $self->{tree} = {%tree};
248 }
249  
250 =head2 C<_get_actions()>
251  
252 Build a hash of Catalyst::Action objects.
253  
254 =cut
255  
256 4 ▶sub _get_actions {
Subroutine _get_actions: called CC=10 SCAR=25.1
257 4 my ($self) = @_;
258  
259 4 my $c = $self->{context};
260 4 my $d = $c->dispatcher;
261  
262 4 my @namespace;
263 my @container;
264 4 my %actionhash;
265  
266 my @controller =
267 4 map {$c->controller($_)}
268 $c->controllers;
269 4 push @namespace, $_->action_namespace($c) for @controller;
270 4 ▶ if ($self->{namespaces}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"namespaces"}) 0 4 4
271 0 my %wanted = map {$_,1} @{$self->{namespaces}};
272 0 @namespace = grep {$wanted{$_}} @namespace;
273 }
274 4 push @container, $d->get_containers($_) for @namespace;
275 4 my $menpattr = $self->{menupath_attr};
276  
277 4 for my $ctr (@container) {
278 4 my $acthash = $ctr->actions;
279 4 for my $name (keys %{$acthash}) {
280 44 ▶ next if exists $acthash->{$name}->attributes->{Private};
Branch: 1 branch
BranchTrueFalseTotal
if exists $acthash->{$name}->attributes->{"Private"} 24 20 44
281 20 ▶ next unless $acthash->{$name}->attributes->{$menpattr};
Branch: 1 branch
BranchTrueFalseTotal
unless $acthash->{$name}->attributes->{$menpattr} 12 8 20
282 8 my $actname = $acthash->{$name}->name;
283 8 my $namespace = $acthash->{$name}->namespace;
284 8 $actionhash{$namespace}{$actname} = $acthash->{$name};
285 }
286 }
287 4 ▶ if ($self->{filter}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"filter"}) 0 4 4
288 0 for my $namespace (keys %actionhash) {
289 0 my %hash = $self->{filter}->($c, %{$actionhash{$namespace}});
290 0 $actionhash{$namespace} = {%hash};
291 }
292 }
293  
294 4 $self->{action_hash} = {%actionhash};
295 }
296  
297 2 1;
298  
299 =head1 AUTHOR
300  
301 David P.C. Wollmann E<lt>converter42@gmail.comE<gt>
302  
303 =head1 BUGS
304  
305 This is brand new code, so use at your own risk.
306  
307 =head1 COPYRIGHT & LICENSE
308  
309 Copyright 2009 by David P.C. Wollmann
310  
311 This program is free software; you can redistribute it and/or modify it under
312 the same terms as Perl itself.
313  
↑ Summary