Factory.pm

filter: Statementstmt 87.6% 64 / 73 Branchbran 71.4% 20 / 28 Conditioncond 52.1% 12 / 23 MC/DCmcdc 20.0% 3 / 15 Subroutinesub 81.2% 13 / 16 Podpod 80.0% 4 / 5 totaltotal 72.5% 116 / 160 SCAR 35.7
CC
27
Cov
77%
new
15.6
_default_instance
6.0
provided_widgets
6.0
CRAP 35.4
↑ Summary Plugin/Radio.pm »
1 15 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 package HTML::Widget::Factory;
5 # ABSTRACT: churn out HTML widgets
6 15 $HTML::Widget::Factory::VERSION = '0.203';
7 15 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use Module::Load ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use MRO::Compat;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 #pod =head1 SYNOPSIS
12 #pod
13 #pod my $factory = HTML::Widget::Factory->new();
14 #pod
15 #pod my $html = $factory->select({
16 #pod name => 'flavor',
17 #pod options => [
18 #pod [ minty => 'Peppermint', ],
19 #pod [ perky => 'Fresh and Warm', ],
20 #pod [ super => 'Red and Blue', ],
21 #pod ],
22 #pod value => 'minty',
23 #pod });
24 #pod
25 #pod =head1 DESCRIPTION
26 #pod
27 #pod HTML::Widget::Factory provides a simple, pluggable system for constructing HTML
28 #pod form controls.
29 #pod
30 #pod =cut
31  
32 #pod =head1 METHODS
33 #pod
34 #pod Most of the useful methods in an HTML::Widget::Factory object will be provided
35 #pod by its plugins. Consult the documentation for the HTML::Widget::Plugin
36 #pod modules.
37 #pod
38 #pod =head2 new
39 #pod
40 #pod my $factory = HTML::Widget::Factory->new(\%arg);
41 #pod
42 #pod This constructor returns a new widget factory.
43 #pod
44 #pod The only valid arguments are C<plugins> and C<extra_plugins>, which provide
45 #pod arrayrefs of plugins to be used. If C<plugins> is not given, the default
46 #pod plugin list is used, which is those plugins that ship with
47 #pod HTML::Widget::Factory. The plugins in C<extra_plugins> are loaded in addition
48 #pod to these.
49 #pod
50 #pod Plugins may be provided as class names or as objects.
51 #pod
52 #pod =cut
53  
54 15 my %default_instance;
55 sub _default_instance {
56 0 ▶ $default_instance{ $_[0] } ||= $_[0]->new;
Subroutine _default_instance: not called CC=2 SCAR=17.9
Condition0%0/3
$default_instance{$_[0]} ||= $_[0]->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$default_instance{$_[0]} ||= $_[0]->new
$default_instance{$_[0]} $_[0]->new
Truth table0%0/3
A ||= B
A= $default_instance{$_[0]}B= $_[0]->new
ABresult
1X1
011
000
57 }
58  
59 my $LOADED_DEFAULTS;
60 15 my @DEFAULT_PLUGINS = qw(
61 HTML::Widget::Plugin::Attrs
62 HTML::Widget::Plugin::Button
63 HTML::Widget::Plugin::Checkbox
64 HTML::Widget::Plugin::Image
65 HTML::Widget::Plugin::Input
66 HTML::Widget::Plugin::Link
67 HTML::Widget::Plugin::Multiselect
68 HTML::Widget::Plugin::Password
69 HTML::Widget::Plugin::Radio
70 HTML::Widget::Plugin::Select
71 HTML::Widget::Plugin::Submit
72 HTML::Widget::Plugin::Textarea
73 );
74  
75 sub _default_plugins {
76 16 ▶ $LOADED_DEFAULTS ||= do {
Subroutine _default_plugins: called CC=3 SCAR=11.1
Condition66%2/3
$LOADED_DEFAULTS ||= do { &Module::Load::load("$_") foreach (@DEFAULT_PLUGINS); 1 }
l!l&&r!l&&!r
1150
MC/DC0%0/2
$LOADED_DEFAULTS ||= do { &Module::Load::load("$_") foreach (@DEFAULT_PLUGINS); 1 }
$LOADED_DEFAULTS do { &Module::Load::load("$_") foreach (@DEFAULT_PLUGINS); 1 }
Truth table66%2/3
A ||= B
A= $LOADED_DEFAULTSB= do { &Module::Load::load("$_") foreach (@DEFAULT_PLUGINS); 1 }
ABresult
1X1
011
000
77 15 Module::Load::load("$_") for @DEFAULT_PLUGINS;
78 15 1;
79 };
80 16 return @DEFAULT_PLUGINS;
81 }
82  
83 sub new {
84 21 ▶ my ($self, $arg) = @_;
Subroutine new: called CC=15 SCAR=27.5
Pod documented
85 21 ▶ $arg ||= {};
Condition100%2/2
$arg ||= {}
l!l
516
MC/DC100%1/1
$arg ||= {}
$arg
Truth table100%2/2
A
A= $arg
Aresult
11
00
86  
87 21 ▶ my $class = ref $self || $self;
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
1200
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table66%2/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
88  
89 # XXX: I think we need to use default plugins when new is invoked on the
90 # class, but get the parent object's plugins when it's called on an existing
91 # factory. -- rjbs, 2014-02-21
92 my @plugins = $arg->{plugins}
93 21 ▶ ? @{ $arg->{plugins} }
Branch: 1 branch
BranchTrueFalseTotal
$arg->{'plugins'} ? : 5 16 21
94 : $class->_default_plugins;
95  
96 21 ▶ unshift @plugins, @{ $self->{plugins} } if ref $self;
Branch: 1 branch
BranchTrueFalseTotal
if ref $self 1 20 21
97  
98 21 ▶ if ($arg->{plugins} or $arg->{extra_plugins}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg->{"plugins"} or $arg->{"extra_plugins"}) 5 16 21
Condition66%2/3
$arg->{"plugins"} or $arg->{"extra_plugins"}
l!l&&r!l&&!r
5016
MC/DC50%1/2
$arg->{"plugins"} or $arg->{"extra_plugins"}
$arg->{"plugins"} $arg->{"extra_plugins"}
Truth table66%2/3
A or B
A= $arg->{"plugins"}B= $arg->{"extra_plugins"}
ABresult
1X1
011
000
99 5 ▶ push @plugins, @{ $arg->{extra_plugins} } if $arg->{extra_plugins};
Branch: 1 branch
BranchTrueFalseTotal
if $arg->{"extra_plugins"} 0 5 5
100 }
101  
102 # make sure plugins given as classes become objects
103 21 ▶ ref $_ or $_ = $_->new for @plugins;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ or $_ = $_->new 197 4 201
104  
105 21 my %widget;
106 21 for my $plugin (@plugins) {
107 201 for my $widget ($plugin->provided_widgets) {
108 220 ▶ my ($method, $name) = ref $widget ? @$widget : ($widget) x 2;
Branch: 1 branch
BranchTrueFalseTotal
ref $widget ? : 17 203 220
109  
110 Carp::croak "$plugin tried to provide $name, already provided by $widget{$name}{plugin}"
111 220 ▶ if $widget{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $widget{$name} 0 220 220
112  
113 220 ▶ Carp::croak
Branch: 1 branch
BranchTrueFalseTotal
unless $plugin->can($method) 0 220 220
114 "$plugin claims to provide widget via ->$method but has no such method"
115 unless $plugin->can($method);
116  
117 220 $widget{$name} = { plugin => $plugin, method => $method };
118 }
119 }
120  
121 # for some reason PPI/Perl::Critic think this is multiple statements:
122 bless { ## no critic
123 21 plugins => \@plugins,
124 widgets => \%widget,
125 } => $class;
126 }
127  
128 #pod =head2 provides_widget
129 #pod
130 #pod if ($factory->provides_widget($name)) { ... }
131 #pod
132 #pod This method returns true if the given name is a widget provided by the factory.
133 #pod This, and not C<can> should be used to determine whether a factory can provide
134 #pod a given widget.
135 #pod
136 #pod =cut
137  
138 sub provides_widget {
139 10 ▶ my ($self, $name) = @_;
Subroutine provides_widget: called CC=3 SCAR=11.0
Pod documented
140 10 ▶ $self = $self->_default_instance unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 10 10
141  
142 10 ▶ return 1 if $self->{widgets}{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"widgets"}{$name} 9 1 10
143  
144 1 return;
145 }
146  
147 #pod =head2 provided_widgets
148 #pod
149 #pod for my $name ($fac->provided_widgets) { ... }
150 #pod
151 #pod This method returns an unordered list of the names of the widgets provided by
152 #pod this factory.
153 #pod
154 #pod =cut
155  
156 sub provided_widgets {
157 0 ▶ my ($self) = @_;
Subroutine provided_widgets: not called CC=2 SCAR=17.9
Pod documented
158 0 ▶ $self = $self->_default_instance unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
159  
160 0 return keys %{ $self->{widgets} };
161 }
162  
163 15 my $ErrorMsg = qq{Can\'t locate object method "%s" via package "%s" }.
164 qq{at %s line %d.\n};
165  
166 sub AUTOLOAD {
167 66 ▶ my $widget_name = our $AUTOLOAD;
Subroutine AUTOLOAD: called CC=5 SCAR=17.4
168 66 $widget_name =~ s/.*:://;
169  
170 66 ▶ return if $widget_name eq 'DESTROY' or $widget_name eq 'CLONE';
Branch: 1 branch
BranchTrueFalseTotal
if $widget_name eq "DESTROY" or $widget_name eq "CLONE" 8 58 66
Condition66%2/3
$widget_name eq "DESTROY" or $widget_name eq "CLONE"
l!l&&r!l&&!r
8058
MC/DC50%1/2
$widget_name eq "DESTROY" or $widget_name eq "CLONE"
$widget_name eq "DESTROY" $widget_name eq "CLONE"
Truth table66%2/3
A or B
A= $widget_name eq "DESTROY"B= $widget_name eq "CLONE"
ABresult
1X1
011
000
171  
172 58 my ($self, $given_arg) = @_;
173 58 ▶ my $class = ref $self || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
5800
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
174 58 my $howto = $self->{widgets}{$widget_name};
175  
176 58 ▶ unless ($howto) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($howto) 0 58 58
177 0 my ($callpack, $callfile, $callline) = caller;
178 0 die sprintf $ErrorMsg, $widget_name, $class, $callfile, $callline;
179 }
180  
181 58 return $self->_build_widget(@$howto{qw(plugin method)}, $given_arg);
182 }
183  
184 sub _build_widget {
185 58 ▶ my ($self, $plugin, $method, $given_arg) = @_;
Subroutine _build_widget: called CC=1 SCAR=0.0
186  
187 58 my $arg = $plugin->rewrite_arg($given_arg, $method);
188  
189 58 return $plugin->$method($self, $arg);
190 }
191  
192 sub can {
193 22 ▶ my ($self, $method) = @_;
Subroutine can: called CC=3 SCAR=13.3
Pod undocumented
194  
195 0 ▶ return sub { $self->$method(@_) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
196 22 ▶ if ref $self and $self->{widgets}{$method};
Branch: 1 branch
BranchTrueFalseTotal
if ref $self and $self->{"widgets"}{$method} 0 22 22
Condition33%1/3
ref $self and $self->{"widgets"}{$method}
!ll&&!rl&&r
0220
MC/DC0%0/2
ref $self and $self->{"widgets"}{$method}
ref $self $self->{"widgets"}{$method}
Truth table33%1/3
A and B
A= ref $selfB= $self->{"widgets"}{$method}
ABresult
0X0
100
111
197  
198 22 return $self->SUPER::can($method);
199 }
200  
201 #pod =head2 plugins
202 #pod
203 #pod This returns a list of the plugins loaded by the factory.
204 #pod
205 #pod =cut
206  
207 1 ▶sub plugins { @{ $_[0]->{plugins} } }
Subroutine plugins: called CC=1 SCAR=0.0
Pod documented
208  
209 #pod =head1 TODO
210 #pod
211 #pod =over
212 #pod
213 #pod =item * fixed_args for args that are fixed, like (type => 'checkbox')
214 #pod
215 #pod =item * a simple way to say "only include this output if you haven't before"
216 #pod
217 #pod This will make it easy to do JavaScript inclusions: if you've already made a
218 #pod calendar (or whatever) widget, don't bother including this hunk of JS, for
219 #pod example.
220 #pod
221 #pod =item * giving the constructor a data store
222 #pod
223 #pod Create a factory that has a CGI.pm object and let it default values to the
224 #pod param that matches the passed name.
225 #pod
226 #pod =item * include id attribute where needed
227 #pod
228 #pod =item * optional labels (before or after control, or possibly return a list)
229 #pod
230 #pod =back
231 #pod
232 #pod =head1 SEE ALSO
233 #pod
234 #pod =over
235 #pod
236 #pod =item L<HTML::Widget::Plugin>
237 #pod
238 #pod =item L<HTML::Widget::Plugin::Input>
239 #pod
240 #pod =item L<HTML::Widget::Plugin::Submit>
241 #pod
242 #pod =item L<HTML::Widget::Plugin::Link>
243 #pod
244 #pod =item L<HTML::Widget::Plugin::Image>
245 #pod
246 #pod =item L<HTML::Widget::Plugin::Password>
247 #pod
248 #pod =item L<HTML::Widget::Plugin::Select>
249 #pod
250 #pod =item L<HTML::Widget::Plugin::Multiselect>
251 #pod
252 #pod =item L<HTML::Widget::Plugin::Checkbox>
253 #pod
254 #pod =item L<HTML::Widget::Plugin::Radio>
255 #pod
256 #pod =item L<HTML::Widget::Plugin::Button>
257 #pod
258 #pod =item L<HTML::Widget::Plugin::Textarea>
259 #pod
260 #pod =item L<HTML::Element>
261 #pod
262 #pod =back
263 #pod
264 #pod =cut
265  
266 15 1;
267  
268 __END__
↑ Summary Plugin/Radio.pm »