Plugin/Button.pm

filter: Statementstmt 100% 37 / 37 Branchbran 100% 12 / 12 Conditioncond 100% 5 / 5 MC/DCmcdc 100% 3 / 3 Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 100% 70 / 70 CC 10 SCAR 17.5
CC
10
Cov
100%
build
10.0
BEGIN
1.0
BEGIN
1.0
CRAP 5.7
« Factory.pm ↑ Summary Plugin/Link.pm »
1 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package HTML::Widget::Plugin::Button;
4 # ABSTRACT: a button for clicking
5 15 $HTML::Widget::Plugin::Button::VERSION = '0.204';
6 15 ▶use parent 'HTML::Widget::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 #pod =head1 SYNOPSIS
9 #pod
10 #pod $widget_factory->button({
11 #pod text => "submit & continue",
12 #pod type => 'submit',
13 #pod });
14 #pod
15 #pod ...or...
16 #pod
17 #pod $widget_factory->button({
18 #pod html => "reset <em>all</em> content",
19 #pod type => 'reset',
20 #pod });
21 #pod
22 #pod =head1 DESCRIPTION
23 #pod
24 #pod This plugin provides a basic button widget.
25 #pod
26 #pod =cut
27  
28 15 ▶use HTML::Element;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 #pod =head1 METHODS
31 #pod
32 #pod =head2 C< provided_widgets >
33 #pod
34 #pod This plugin provides the following widgets: input
35 #pod
36 #pod =cut
37  
38 16 ▶sub provided_widgets { qw(button) }
Subroutine provided_widgets: called CC=1 SCAR=0.0
Pod documented
39  
40 #pod =head2 C< button >
41 #pod
42 #pod This method returns simple button element.
43 #pod
44 #pod In addition to the generic L<HTML::Widget::Plugin> attributes, the following
45 #pod are valid arguments:
46 #pod
47 #pod =over
48 #pod
49 #pod =item text
50 #pod
51 #pod =item html
52 #pod
53 #pod One of these options may be provided. If text is provided, it is used as the
54 #pod content of the button, after being entity encoded. If html is provided, it is
55 #pod used as the content of the button with no encoding performed.
56 #pod
57 #pod =item type
58 #pod
59 #pod This is the type of input button to be created. Valid types are button,
60 #pod submit, and reset. The default is button.
61 #pod
62 #pod =item value
63 #pod
64 #pod This is the widget's initial value.
65 #pod
66 #pod =back
67 #pod
68 #pod =cut
69  
70 16 ▶sub _attribute_args { qw(type value) }
Subroutine _attribute_args: called CC=1 SCAR=0.0
71 32 ▶sub _boolean_args { qw(disabled) }
Subroutine _boolean_args: called CC=1 SCAR=0.0
72  
73 6 ▶sub button {
Subroutine button: called CC=1 SCAR=0.0
Pod documented
74 6 my ($self, $factory, $arg) = @_;
75  
76 6 $self->build($factory, $arg);
77 }
78  
79 #pod =head2 C< build >
80 #pod
81 #pod my $widget = $class->build($factory, $arg);
82 #pod
83 #pod This method does the actual construction of the input based on the args
84 #pod collected by the widget-constructing method. It is primarily here for
85 #pod subclasses to exploit.
86 #pod
87 #pod =cut
88  
89 15 my %TYPES = map { $_ => 1 } qw(button reset submit);
90 6 ▶sub __is_valid_type {
Subroutine __is_valid_type: called CC=1 SCAR=0.0
91 6 my ($self, $type) = @_;
92  
93 6 return exists $TYPES{ $type };
94 }
95  
96 6 ▶sub build {
Subroutine build: called CC=10 SCAR=23.0
Pod documented
97 6 my ($self, $factory, $arg) = @_;
98  
99 6 ▶ $arg->{attr}{name} = $arg->{attr}{id} if not defined $arg->{attr}{name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg->{"attr"}{"name"} 2 4 6
100 6 ▶ $arg->{attr}{type} ||= 'button';
Condition100%2/2
$arg->{"attr"}{"type"} ||= "button"
l!l
33
MC/DC100%1/1
$arg->{"attr"}{"type"} ||= "button"
$arg->{"attr"}{"type"}
Truth table100%2/2
A
A= $arg->{"attr"}{"type"}
Aresult
11
00
101  
102 Carp::croak "invalid button type: $arg->{attr}{type}"
103 6 ▶ unless $self->__is_valid_type($arg->{attr}{type});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->__is_valid_type($arg->{"attr"}{"type"}) 1 5 6
104  
105 Carp::croak "text and html arguments for link widget are mutually exclusive"
106 5 ▶ if $arg->{text} and $arg->{html};
Branch: 1 branch
BranchTrueFalseTotal
if $arg->{"text"} and $arg->{"html"} 1 4 5
Condition100%3/3
$arg->{"text"} and $arg->{"html"}
!ll&&!rl&&r
311
MC/DC100%2/2
$arg->{"text"} and $arg->{"html"}
$arg->{"text"} $arg->{"html"}
Truth table100%3/3
A and B
A= $arg->{"text"}B= $arg->{"html"}
ABresult
0X0
100
111
107  
108 4 my $widget = HTML::Element->new('button');
109 4 $widget->attr($_ => $arg->{attr}{$_}) for keys %{ $arg->{attr} };
110  
111 4 my $content;
112 4 ▶ if ($arg->{html}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg->{'html'}) { } 2 2 4
113 $content = ref $arg->{html}
114 ? $arg->{html}
115 2 ▶ : HTML::Element->new('~literal' => text => $arg->{html});
Branch: 1 branch
BranchTrueFalseTotal
ref $arg->{'html'} ? : 1 1 2
116 } else {
117 $content = defined $arg->{text}
118 ? $arg->{text}
119 2 ▶ : ucfirst lc $arg->{attr}{type};
Branch: 1 branch
BranchTrueFalseTotal
defined $arg->{'text'} ? : 1 1 2
120 }
121  
122 4 $widget->push_content($content);
123  
124 4 return $widget->as_XML;
125 }
126  
127 15 1;
128  
129 __END__
« Factory.pm ↑ Summary Plugin/Link.pm »