blib/lib/ASP4x/Linker/Widget.pm

filter: Statementstmt 96.3% 52 / 54 Branchbran 71.4% 10 / 14 Conditioncond 50.0% 3 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 92.8% 13 / 14 Podpod 70.0% 7 / 10 totaltotal 84.1% 85 / 101 CC 12 SCAR 10.1
CC
12
Cov
88%
new
5.1
set
4.0
on_change
3.1
CRAP 2.8
« blib/lib/ASP4x/Linker.pm ↑ Summary
1  
2 package ASP4x::Linker::Widget;
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  
8  
9 sub new
10 68 ▶{
Subroutine new: called CC=5 SCAR=16.3
Pod undocumented
11 68 my ($class, %args) = @_;
12
13 68 foreach(qw( name ))
14 {
15 confess "Required param '$_' was not provided"
16 68 ▶ unless $args{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{$_} 0 68 68
17 }# end foreach()
18
19 68 my $context = ASP4::HTTPContext->current;
20 68 my $form = $context->request->Form;
21 68 ▶ $args{attrs} ||= [ ];
Condition50%1/2
$args{"attrs"} ||= []
l!l
680
MC/DC0%0/1
$args{"attrs"} ||= []
$args{"attrs"}
Truth table50%1/2
A
A= $args{"attrs"}
Aresult
11
00
22 68 ▶ $args{triggers} ||= { };
Condition50%1/2
$args{"triggers"} ||= {}
l!l
068
MC/DC0%0/1
$args{"triggers"} ||= {}
$args{"triggers"}
Truth table50%1/2
A
A= $args{"triggers"}
Aresult
11
00
23
24 $args{vars} = {
25 232 map { $_ => $form->{"$args{name}.$_"} }
26 68 @{$args{attrs}}
27 };
28 $args{original_vars} = {
29 232 map { $_ => $form->{"$args{name}.$_"} }
30 68 @{$args{attrs}}
31 };
32
33 68 return bless \%args, $class;
34 }# end new()
35  
36  
37 151 ▶sub attrs { sort @{ shift->{attrs} } }
Subroutine attrs: called CC=1 SCAR=0.0
Pod documented
38  
39 1110 ▶sub name { shift->{name} }
Subroutine name: called CC=1 SCAR=0.0
Pod undocumented
40  
41 sub set
42 7 ▶{
Subroutine set: called CC=4 SCAR=13.9
Pod documented
43 7 my ($s, %args) = @_;
44 7 ▶ while( my ($attr, $val) = each %args )
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %args) 8 7 15
45 {
46 confess "widget '$s->{name}' does not have any attribute named '$attr'"
47 8 ▶ unless exists($s->{vars}->{$attr});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $s->{"vars"}{$attr} 0 8 8
48 8 $s->{vars}->{$attr} = $val;
49 8 ▶ if( my $triggers = $s->{triggers}->{$attr} )
Branch: 1 branch
BranchTrueFalseTotal
if (my $triggers = $s->{"triggers"}{$attr}) 1 7 8
50 {
51 1 map { $_->( $s ) } @$triggers
52 }# end if()
53 }# end while()
54
55 # $val;
56 7 $s;
57 }# end set()
58  
59  
60 sub get
61 468 ▶{
Subroutine get: called CC=2 SCAR=7.1
Pod documented
62 468 my ($s, $key) = @_;
63
64 468 ▶ exists( $s->{vars}->{ $key } ) or return;
Branch: 1 branch
BranchTrueFalseTotal
exists $s->{"vars"}{$key} or return 468 0 468
65 468 $s->{vars}->{$key};
66 }# end get()
67  
68  
69 sub vars
70 1 ▶{
Subroutine vars: called CC=1 SCAR=0.0
Pod documented
71 1 my $s = shift;
72
73 1 return $s->{vars};
74 }# end filters()
75  
76  
77 sub reset
78 111 ▶{
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
79 111 my $s = shift;
80
81 111 %{ $s->{vars} } = %{ $s->{original_vars} };
82 }# end reset()
83  
84  
85 sub linker
86 69 ▶{
Subroutine linker: called CC=2 SCAR=6.9
Pod undocumented
87 69 my $s = shift;
88 69 ▶ @_ ? $s->{linker} = shift : $s->{linker};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 68 1 69
89 }# end linker()
90  
91  
92 1 ▶sub uri { shift->linker->uri }
Subroutine uri: called CC=1 SCAR=0.0
Pod documented
93  
94  
95 sub on_change
96 1 ▶{
Subroutine on_change: called CC=3 SCAR=11.3
Pod documented
97 1 my ($s, $attr, $code) = @_;
98
99 1 ▶ return unless exists( $s->{vars}->{$attr} );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $s->{"vars"}{$attr} 0 1 1
100 1 ▶ $s->{triggers}->{ $attr } ||= [ ];
Condition50%1/2
$s->{"triggers"}{$attr} ||= []
l!l
01
MC/DC0%0/1
$s->{"triggers"}{$attr} ||= []
$s->{"triggers"}{$attr}
Truth table50%1/2
A
A= $s->{"triggers"}{$attr}
Aresult
11
00
101 1 push @{ $s->{triggers}->{ $attr } }, $code;
102 }# end on_change()
103  
104 0 ▶sub DESTROY { my $s = shift; undef(%$s); }
Subroutine DESTROY: not called CC=1 SCAR=6.9
105  
106 7 1;# return true:
107  
108 =pod
109  
110 =head1 NAME
111  
112 ASP4x::Linker::Widget - A single item that should be persisted via links.
113  
114 =head1 SYNOPSIS
115  
116 use ASP4x::Linker;
117
118 my $linker = ASP4x::Linker->new();
119
120 # Add a widget:
121 $linker->add_widget(
122 name => 'albums',
123 attrs => [qw( page_number page_size sort_col sort_dir )]
124 );
125
126 # Get the widget:
127 my $widget = $linker->widget('albums');
128
129 # Change some attributes:
130 $widget->set( page_size => 10 );
131 $widget->set( page_number => 4 );
132
133 # Get the value of some attributes:
134 $widget->get( 'page_size' ); # 10
135 $widget->get( 'page_number' ); # 4
136  
137 # Make page_number reset to 1 if the page_size is changed:
138 $widget->on_change( page_size => sub {
139 my $s = shift;
140 $s->set( page_number => 1 );
141 });
142
143 $widget->set( page_size => 20 );
144 print $widget->get( 'page_number' ); # 1
145
146 # Set multiple values at once:
147 $widget->set( %args );
148
149 # Set multiple values at once and get the uri:
150 warn $widget->set( %args )->uri();
151
152 # Set multiple values by chaining and get the uri:
153 warn $widget->set( foo => 'bar' )->set( baz => 'bux' )->uri();
154  
155 =head1 DESCRIPTION
156  
157 C<ASP4x::Linker::Widget> provides a simple, simple interface to a "thing" on your
158 web page (which we'll call a "widget").
159  
160 A "widget" can be anything. My experience generally means that a widget is a data grid
161 that supports paging and sorting through multiple records. However a widget could
162 represent anything you want it to, as long as you can describe it with a B<name> and
163 and arrayref of B<attrs> (attributes). The B<attrs> arrayref for a data grid might
164 look like what you see in the example above:
165  
166 attrs => [qw( page_number page_size sort_col sort_dir )]
167  
168 In English, a "widget" can be anything. The word "widget" could be replaced with "thing" -
169 so in this case a "widget" is just a "thing" - anything (with a name and attributes).
170  
171 =head1 PUBLIC PROPERTIES
172  
173 =head2 vars
174  
175 Returns a hashref of all name/value pairs of attributes and their current values.
176  
177 =head2 attrs
178  
179 Returns an B<array> of the names of the widget's attributes.
180  
181 =head1 PUBLIC METHODS
182  
183 =head2 reset( )
184  
185 Restores the widget's C<vars> to its original state - as it was when the widget
186 was first instantiated.
187  
188 =head2 set( $attr => $value )
189  
190 Changes the value of an attribute to a new value.
191  
192 B<NOTE:> As of version , attempts to apply a value to a non-existant attribute will result in a runtime exception.
193  
194 =head2 get( $attr )
195  
196 Returns the current value of the attribute.
197  
198 =head2 on_change( $attr => sub { ... } )
199  
200 Adds a trigger to the widget that will be called when the given attribute's value is changed via C<set()>.
201  
202 =head2 uri()
203  
204 Just a wrapper around the widget's parent C<ASP4x::Linker> object.
205  
206 =head1 SEE ALSO
207  
208 L<ASP4x::Linker>
209  
210 =head1 AUTHOR
211  
212 John Drago <jdrago_999@yahoo.com>
213  
214 =head1 LICENSE
215  
216 This software is B<Free> software and may be used and redistributed under the same
217 terms as Perl itself.
218  
219 =cut
220  
« blib/lib/ASP4x/Linker.pm ↑ Summary