MenuGrinder/Plugin/ActivePath.pm untested

filter: Statementstmt 0.0% 0 / 47 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 9 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 89 CC 9 SCAR 29.6
CC
9
Cov
0%
find_longest_match
42.0
mark_active_path
20.0
plugin_depends
2.0
CRAP 19.3
1 package WWW::MenuGrinder::Plugin::ActivePath;
2 our $VERSION = '0.01_01';
3  
4  
5 # ABSTRACT: WWW::MenuGrinder plugin that finds a path to the currently active page.
6  
7 use Moose;
8 use List::Util;
9  
10 with 'WWW::MenuGrinder::Role::ItemMogrifier';
11 with 'WWW::MenuGrinder::Role::BeforeMogrify';
12  
13 has path => ( is => 'rw' );
14  
15 sub plugin_depends { qw(Visitor) }
16  
17 sub plugin_required_grinder_methods { qw(path) }
18  
19 sub before_mogrify {
20 my ($self) = @_;
21  
22 $self->path( $self->grinder->path );
23 }
24  
25 has 'longest' => (
26 is => 'rw',
27 default => 0
28 );
29  
30 sub item_mogrify_methods {
31 qw(find_longest_match mark_active_path)
32 };
33  
34 sub find_longest_match {
35 my ( $self, $item ) = @_;
36  
37 if (exists $item->{location}) {
38 my @loc = ref($item->{location}) eq 'ARRAY' ?
39 @{ $item->{location} } : $item->{location};
40  
41 for my $location ( @loc ) {
42  
43 my $active;
44 # XML::Simple is stupid
45 if ( $location eq '' or ref($location) eq 'HASH' ) {
46 $active = 0.01; # more than 0, less than 1
47 } elsif ( $self->{path} =~ m#^\Q$location\E(/|$)# ) {
48 $active = length($location);
49 }
50  
51 if (defined $active && $active > $self->longest) {
52 $self->longest( $active );
53 # This one might be the longest, so we might use it later.
54 # If not, we'll delete it.
55 $item->{active} = $active;
56 }
57 }
58 }
59  
60 return $item;
61 }
62  
63 sub mark_active_path {
64 my ( $self, $item ) = @_;
65  
66 # If we were the longest match, set active="yes".
67 # If one of our children is active (of either type) set active="child".
68  
69 my $max = $self->longest;
70  
71 if (defined $item->{active} and $item->{active} == $max) {
72 $item->{active} = "yes";
73 } else {
74 delete $item->{active};
75 if (ref ($item->{item})) {
76 for my $child ( @{ $item->{item} } ) {
77 if (defined $child->{active}) {
78 $item->{active} = "child";
79 last;
80 }
81 }
82 }
83 }
84  
85 return $item;
86 }
87  
88 sub cleanup {
89 my ($self) = @_;
90 $self->longest(0);
91 }
92  
93 no Moose;
94 1;
95  
96  
97 __END__
98 =head1 NAME
99  
100 WWW::MenuGrinder::Plugin::ActivePath - WWW::MenuGrinder plugin that finds a path to the currently active page.
101  
102 =head1 VERSION
103  
104 version 0.01_01
105  
106 =head1 DESCRIPTION
107  
108 C<WWW::MenuGrinder::Plugin::ActivePath> is a plugin for C<WWW::MenuGrinder>. You
109 should not use it directly, but include it in the C<plugins> section of a
110 C<WWW::MenuGrinder> config.
111  
112 When loaded, this plugin will visit each item of the menu, comparing any item
113 with a C<location> attribute to the current URL path. The item that best matches
114 the current path will have its C<active> key set to "yes", and each of its
115 ancestors will have its C<active> key set to "child".
116  
117 =head2 Configuration
118  
119 None.
120  
121 =head2 Required Methods
122  
123 In order to load this plugin your C<WWW::MenuGrinder> subclass must implement
124 the method C<path> returning a path name for the current request.
125  
126 =head2 Dependencies
127  
128 C<WWW::MenuGrinder::Plugin::Visitor>.
129  
130 =head2 Other Considerations
131  
132 It's advisable to load this plugin after any plugins that may remove items from
133 the menu, to ensure that the chain of active items is unbroken.
134  
135 =head1 AUTHOR
136  
137 Andrew Rodland <andrew@hbslabs.com>
138  
139 =head1 COPYRIGHT AND LICENSE
140  
141 This software is copyright (c) 2009 by HBS Labs, LLC..
142  
143 This is free software; you can redistribute it and/or modify it under
144 the same terms as perl itself.
145