blib/lib/ASP4x/Linker.pm

filter: Statementstmt 96.0% 97 / 101 Branchbran 71.4% 10 / 14 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 94.7% 18 / 19 Podpod 100% 9 / 9 totaltotal 91.5% 140 / 153 CC 12 SCAR 10.5
CC
12
Cov
92%
vars
7.0
new
3.2
add_widget
2.0
CRAP 2.9
↑ Summary blib/lib/ASP4x/Linker/Widget.pm »
1  
2 package ASP4x::Linker;
3  
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use warnings 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use ASP4x::Linker::Widget;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use ASP4::ConfigLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 7 our $VERSION = '1.003';
11  
12  
13 sub new
14 19 ▶{
Subroutine new: called CC=3 SCAR=11.8
Pod documented
15 19 my ($class, %args) = @_;
16
17 19 ▶ $args{base_href} ||= $ENV{REQUEST_URI};
Condition33%1/3
$args{"base_href"} ||= $ENV{"REQUEST_URI"}
l!l&&r!l&&!r
0190
MC/DC0%0/2
$args{"base_href"} ||= $ENV{"REQUEST_URI"}
$args{"base_href"} $ENV{"REQUEST_URI"}
Truth table33%1/3
A ||= B
A= $args{"base_href"}B= $ENV{"REQUEST_URI"}
ABresult
1X1
011
000
18 confess "No 'base_href' argument provided and can't discover it from \$ENV{REQUEST_URI}!"
19 19 ▶ unless $args{base_href};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"base_href"} 0 19 19
20
21 19 $args{widgets} = [ ];
22
23 19 return bless \%args, $class;
24 }# end new()
25  
26  
27 # Public read-only properties:
28 89 ▶sub base_href { shift->{base_href} }
Subroutine base_href: called CC=1 SCAR=0.0
Pod documented
29 31 ▶sub _router { eval { ASP4::ConfigLoader->load->web->router } }
Subroutine _router: called CC=1 SCAR=0.0
30 147 ▶sub widgets { @{ shift->{widgets} } }
Subroutine widgets: called CC=1 SCAR=0.0
Pod documented
31  
32  
33 sub add_widget
34 68 ▶{
Subroutine add_widget: called CC=2 SCAR=7.1
Pod documented
35 68 my ($s, %args) = @_;
36
37 68 my $widget = ASP4x::Linker::Widget->new( %args );
38 68 $widget->linker( $s );
39
40 0 confess "Another widget with the name '@{[ $widget->name ]}' already exists."
41 68 ▶ if grep { $_->name eq $widget->name } $s->widgets;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_->name eq $widget->name;} $s->widgets 0 68 68
42
43 68 push @{ $s->{widgets} }, $widget;
44 68 $widget;
45 }# end add_widget()
46  
47  
48 sub widget
49 5 ▶{
Subroutine widget: called CC=2 SCAR=7.0
Pod documented
50 5 my ($s, $name) = @_;
51
52 5 ▶ my ($widget) = grep { $_->name eq $name } $s->widgets
Branch: 1 branch
BranchTrueFalseTotal
my($widget) = grep({$_->name eq $name;} $s->widgets) or return 5 0 5
53 or return;
54
55 5 return $widget;
56 }# end widget()
57  
58  
59 sub reset
60 32 ▶{
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
61 32 map { $_->reset } shift->widgets;
62 }# end reset()
63  
64  
65 sub uri
66 23 ▶{
Subroutine uri: called CC=2 SCAR=6.9
Pod documented
67 23 my ($s, $args) = @_;
68
69 23 my $vars = $s->vars( $args );
70
71 7 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
72 23 my ($uri) = split /\?/, $s->base_href;
73 23 my $context = ASP4::HTTPContext->current;
74 23 my $server = $context->server;
75 97 my $final_querystring = join '&', map { $server->URLEncode($_) . '=' . $server->URLEncode($vars->{$_}) }
76 23 grep { defined($vars->{$_}) }
77 sort keys %$vars;
78
79 23 ▶ return $final_querystring ? join '?', ( $uri, $final_querystring ) : $uri;
Branch: 1 branch
BranchTrueFalseTotal
$final_querystring ? : 22 1 23
80 }# end uri()
81  
82  
83 sub hidden_fields
84 4 ▶{
Subroutine hidden_fields: called CC=1 SCAR=0.0
Pod documented
85 4 my ($s, $args) = @_;
86
87 4 my $vars = $s->vars( $args, 1 );
88
89 7 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
90 4 my ($uri) = split /\?/, $s->base_href;
91 4 my $context = ASP4::HTTPContext->current;
92 4 my $server = $context->server;
93  
94 4 my @inputs = map {qq(<input type="hidden" name="@{[ $server->URLEncode( $_ ) ]}" value="@{[ $server->URLEncode( $vars->{$_} ) ]}" />)}
95 keys %$vars;
96 4 return join "\n", @inputs;
97 }# end hidden_fields()
98  
99  
100 sub vars
101 31 ▶{
Subroutine vars: called CC=7 SCAR=19.5
Pod documented
102 31 my ($s, $args) = @_;
103
104 31 my @parts = ( );
105 7 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
106 31 my ($uri) = split /\?/, $s->base_href;
107
108 31 my $context = ASP4::HTTPContext->current;
109 31 my $server = $context->server;
110 31 my %vars = %{ $context->request->Form };
111 map {
112 0 delete($vars{$_})
113 31 } grep { $_ !~ m/\./ } keys %vars;
114
115 31 ▶ if( $context->config->web->can('router') )
Branch: 1 branch
BranchTrueFalseTotal
if ($context->config->web->can("router")) 31 0 31
116 {
117 31 ▶ if( my $route = eval { $s->_router->route_for( $s->base_href, $ENV{REQUEST_METHOD} ) } )
Branch: 1 branch
BranchTrueFalseTotal
if (my $route = eval { do { $s->_router->route_for($s->base_href, $ENV{"REQUEST_METHOD"}) } }) 11 20 31
118 {
119 map {
120 11 delete($vars{$_});
121 11 } @{$route->{captures}};
122 }# end if()
123 }# end if()
124
125 31 foreach my $w ( $s->widgets )
126 {
127 107 foreach( $w->attrs )
128 {
129 358 my $key = $server->URLEncode( $w->name . '.' . $_ );
130 358 my $val;
131 358 ▶ if( exists( $args->{ $w->name } ) && exists( $args->{ $w->name }->{ $_ } ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$args{$w->name} and exists $$args{$w->name}{$_}) { } 44 314 358
Condition100%3/3
exists $$args{$w->name} and exists $$args{$w->name}{$_}
!ll&&!rl&&r
2645044
MC/DC100%2/2
exists $$args{$w->name} and exists $$args{$w->name}{$_}
exists $$args{$w->name} exists $$args{$w->name}{$_}
Truth table100%3/3
A and B
A= exists $$args{$w->name}B= exists $$args{$w->name}{$_}
ABresult
0X0
100
111
132 {
133 44 $vars{ $key } = $args->{ $w->name }->{ $_ };
134 }
135 else
136 {
137 314 $vars{ $key } = $w->get( $_ );
138 }# end if()
139 }# end foreach()
140 }# end foreach()
141
142 # Also add any non-ref values that were passed in as $args:
143 1 map { $vars{$_} = $args->{$_} }
144 31 grep { ! ref($args->{$_}) }
145 sort keys %$args;
146
147 31 my $res = \%vars;
148 31 $s->reset();
149 31 return $res;
150 }# end _prepare_vars()
151  
152  
153 0 ▶sub DESTROY { my $s = shift; undef(%$s); }
Subroutine DESTROY: not called CC=1 SCAR=6.9
154  
155 7 1;
156  
157 =pod
158  
159 =head1 NAME
160  
161 ASP4x::Linker - In-page persistence of widget-specific variables.
162  
163 =head1 DEPRECATED
164  
165 L<ASP4> was marked deprecated, so this module is now also deprecated.
166  
167 =head1 SYNOPSIS
168  
169 =head2 Setup
170  
171 (Within /some-page.asp)
172  
173 use ASP4x::Linker;
174
175 # Create our linker:
176 my $linker = ASP4x::Linker->new();
177 -or be more specific-
178 my $linker = ASP4x::Linker->new( base_href => "/whatever.html" );
179
180 # Add a widget:
181 $linker->add_widget(
182 name => "widgetA",
183 attrs => [qw( page_size page_number sort_col sort_dir )]
184 );
185
186 # If the page size is changed, go back to page 1:
187 $linker->widget("widgetA")->on_change( page_size => sub {
188 my ($s) = @_;
189 $s->set( page_number => 1 );
190 });
191
192 # Add another widget:
193 $linker->add_widget(
194 name => "widgetB",
195 attrs => [qw( keywords tag start_date stop_date )]
196 );
197
198 # Chained accessor goodness:
199 # New as of v1.001
200 my $fancy_uri = $linker->widget('widgetA')->set( %args )->uri;
201  
202 =head2 Setting Variables
203  
204 Since the C<$linker> starts out with only the variables found in C<$ENV{REQUEST_URI}>
205 or in the C<base_href> argument, calling C<< $linker->uri() >> on a new linker object
206 will return the same uri that you are currently on (eg: C</some-page.asp>
207  
208 To generate links that contain specific attributes, set the attributes for those
209 widgets and then call C<< $linker->uri() >> to get the resultant uri to match.
210  
211 $linker->widget("widgetA")->set( page_number => 2 );
212 $uri = $linker->uri;
213 # $uri is now "/some-page.asp?widgetA.page_number=2"
214  
215 =head1 DESCRIPTION
216  
217 C<ASP4x::Linker> aims to solve the age-old problem of:
218  
219 B<How do I change one widget on the page without losing my settings for all the other widgets on the page?>
220  
221 OK - say you have one data grid on your web page that allows paging and sorting. You can move forward and backward between
222 pages, change the sorting - life's great. B<THEN> your boss says:
223  
224 We need to have two of those on the same page. One for Albums and one for Genres.
225  
226 Now you have 2 options.
227  
228 =over 4
229  
230 B<Option 1>: If a user pages "Albums" to page 4, then pages "Genres" to page 2, you forget that "Albums" was on page 4.
231  
232 B<Option 2>: Use ASP4x::Linker. Register 2 "widgets" (albums and genres) and let the linker know that they both have C<page_number>, C<page_size>, C<sort_col> and C<sort_dir> attributes.
233 When the user makes paging or sorting changes in Albums, the stuff for Genres will persist between requests without any extra effort.
234  
235 =back
236  
237 =head1 CONSTRUCTOR
238  
239 =head2 new( [ base_href => $ENV{REQUEST_URI} ] )
240  
241 Returns a new C<ASP4x::Linker> object using the supplied C<base_href> value as the "starting point"
242 for all links that will be generated.
243  
244 If no C<base_href> is provided, the value of C<$ENV{REQUEST_URI}> will be used instead.
245  
246 =head1 PUBLIC READ-ONLY PROPERTIES
247  
248 =head2 base_href
249  
250 Returns the C<base_href> value in use for the linker object.
251  
252 =head2 widgets
253  
254 Returns an array of L<ASP4x::Linker::Widget> objects assigned to the linker.
255  
256 =head1 PUBLIC METHODS
257  
258 =head2 add_widget( name => $str, attrs => \@attrNames )
259  
260 Adds a "widget" to the widgets collection.
261  
262 =head2 widget( $name )
263  
264 Returns an individual L<ASP4x::Linker::Widget> object by that name.
265  
266 Returns undef if no widget by that name is found.
267  
268 =head2 uri( [$properties] )
269  
270 Returns the uri for all widgets based on the intersect of:
271  
272 =over 4
273  
274 =item * The incoming form data from the original request
275  
276 =item * Individually-set values for each widget in the collection.
277  
278 =item * Any properties provided as an argument to C<uri()>.
279  
280 =back
281  
282 =head2 hidden_fields( [$properties] )
283  
284 Returns a string of XHTML hidden input fields (<input type="hidden" name="$name" value="$value" />).
285  
286 Useful if your persistence logic involves repeated form submissions rather than hyperlinks.
287  
288 The C<$properties> argument is the same as in the C<uri()> method.
289  
290 =head2 vars( [$properties] )
291  
292 Returns a hashref representing the intersect of all widgets' names and attributes.
293  
294 Supposing you setup your linker like this:
295  
296 my $linker = ASP4x::Linker->new();
297
298 $linker->add_widget(
299 name => 'albums',
300 attrs => [qw( page sort )]
301 );
302
303 $linker->add_widget(
304 name => 'artists',
305 attrs => [qw( page sort )]
306 );
307  
308 After calling C<vars()> you'd get:
309  
310 $VAR1 = {
311 'albums.page' => undef,
312 'albums.sort' => undef,
313 'artists.page' => undef,
314 'artists.sort' => undef,
315 };
316  
317 If you did this:
318  
319 $linker->vars({
320 albums => {page => 2},
321 artists => {sort => 'desc'}
322 });
323  
324 Then you'd get this instead:
325  
326 $VAR1 = {
327 'albums.page' => 2,
328 'albums.sort' => undef,
329 'artists.page' => undef,
330 'artists.sort' => 'desc',
331 };
332  
333 You could also do this:
334  
335 $linker->widget('albums')->page( 10 );
336 $linker->widget('artists')->sort( 'desc' );
337 $linker->vars();
338  
339 And you would get the same thing:
340  
341 $VAR1 = {
342 'albums.page' => 2,
343 'albums.sort' => undef,
344 'artists.page' => undef,
345 'artists.sort' => 'desc',
346 };
347  
348 =head2 reset( )
349  
350 Resets all widgets to their original values from the original request (as specified in the C<base_href> value used by C<new()>).
351  
352 =head1 SEE ALSO
353  
354 L<ASP4>, L<ASP4x::Router>, L<Router::Generic>
355  
356 =head1 AUTHOR
357  
358 John Drago <jdrago_999@yahoo.com>
359  
360 =head1 LICENSE
361  
362 This software is B<Free> software and may be used and redistributed under the same
363 terms as Perl itself.
364  
365 =cut
366  
↑ Summary blib/lib/ASP4x/Linker/Widget.pm »