Code/Block.pm

filter: Statementstmt 67.2% 37 / 55 Branchbran 50.0% 5 / 10 Conditioncond 25.0% 2 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 70.0% 7 / 10 Podpod 100% 5 / 5 totaltotal 60.2% 56 / 93 CC 11 SCAR 17.0
CC
11
Cov
60%
output
12.0
add
7.9
new
3.1
CRAP 5.5
« Code/Expression/Op/Not.pm ↑ Summary Code/Element.pm »
1 package JavaScript::Code::Block;
2
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use vars qw[ $VERSION ];
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use base qw[ JavaScript::Code::Element ];
Subroutine BEGIN: called CC=1 SCAR=0.0
6
7 2 ▶use JavaScript::Code::Variable ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use JavaScript::Code::Function ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9
10 2 __PACKAGE__->mk_ro_accessors(qw[ elements ]);
11
12 2 $VERSION = '0.08';
13
14 =head1 NAME
15
16 JavaScript::Code::Block - A JavaScript Block Element
17
18 =head1 DESCRIPTION
19
20 A block element in javascript is a collection of javascript elements enclosed in brakets.
21
22 Example:
23
24 { // a block starts
25 var a = 42;
26 var b = "21 is just half the truth.";
27
28 { // another block starts
29
30 // ...
31
32 } // another block ends
33
34 } // a block ends
35
36
37 =head1 SYNOPSIS
38
39 #!/usr/bin/perl
40
41 use strict;
42 use warnings;
43 use JavaScript::Code::Block;
44 use JavaScript::Code::Variable;
45
46 my $block1 = JavaScript::Code::Block->new();
47 my $var1 = JavaScript::Code::Variable->new( name => 'a', value => "Var 1!" );
48 my $var2 = JavaScript::Code::Variable->new()->name('b')->value("Var 2!");
49
50 my $block2 = JavaScript::Code::Block->new();
51 my $var3 = JavaScript::Code::Variable->new()->name('c')->value("Var 3!");
52
53 $block1->add( $var1 );
54 $block1->add( $var2 )->add( $block2->add( $var3 ) );
55 $block1->add( JavaScript::Code::Variable->new()->name('d')->value(42) );
56
57 print $block1->output;
58
59 =head1 METHODS
60
61 =head2 new
62
63 =cut
64
65 1 ▶sub new {
Subroutine new: called CC=3 SCAR=11.4
Pod documented
66 1 my $this = shift;
67 1 ▶ my $class = ref($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
68
69 1 my $self = $class->SUPER::new(@_);
70
71 # cleanup the elements
72 1 my $array = delete $self->{elements};
73 1 $self->{elements} = [];
74 1 ▶ $self->add($array) if defined $array;
Branch: 1 branch
BranchTrueFalseTotal
if defined $array 0 1 1
75
76 1 return $self;
77 }
78
79 =head2 $self->add( $element | \@elements )
80
81 Adds one or more new element(s) to the block.
82
83 =cut
84
85 2 ▶sub add {
Subroutine add: called CC=7 SCAR=20.6
Pod documented
86 2 my ( $self, $array ) = @_;
87
88 2 ▶ die 'Nothing to add.'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $array 0 2 2
89 unless defined $array;
90
91 2 ▶ $array = [$array] unless ref $array eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $array eq "ARRAY" 2 0 2
92
93 2 my @elements = ();
94 2 foreach my $element ( @{$array} ) {
95
96 2 ▶ die "Not a 'JavaScript::Code::Element'."
Branch: 1 branch
BranchTrueFalseTotal
unless ref $element and $element->isa("JavaScript::Code::Element") 0 2 2
Condition33%1/3
ref $element and $element->isa("JavaScript::Code::Element")
!ll&&!rl&&r
002
MC/DC0%0/2
ref $element and $element->isa("JavaScript::Code::Element")
ref $element $element->isa("JavaScript::Code::Element")
Truth table33%1/3
A and B
A= ref $elementB= $element->isa("JavaScript::Code::Element")
ABresult
0X0
100
111
97 unless ref $element
98 and $element->isa('JavaScript::Code::Element');
99
100 2 ▶ die "Not able to a element of type 'JavaScript::Code'."
Branch: 1 branch
BranchTrueFalseTotal
if $element->isa("JavaScript::Code") 0 2 2
101 if $element->isa('JavaScript::Code');
102
103 2 push @elements, $element->clone->parent($self);
104 }
105
106 2 push @{ $self->{elements} }, @elements;
107
108 2 return $self;
109 }
110
111 =head2 $self->add_variable( %args | \%args )
112
113 Creates a variable using the arguments and adds it to the the block.
114
115 Returns a L<JavaScript::Code::Variable> object.
116
117 =cut
118
119 0 ▶sub add_variable {
Subroutine add_variable: not called CC=1 SCAR=6.9
Pod documented
120 0 my $self = shift;
121
122 0 my $var = JavaScript::Code::Variable->new(@_);
123 0 $self->add($var);
124
125 0 return $var;
126 }
127
128 =head2 $self->add_function( %args | \%args )
129
130 Creates a function using the arguments and adds it to the the block.
131
132 Returns a L<JavaScript::Code::Function> object.
133
134 =cut
135
136 0 ▶sub add_function {
Subroutine add_function: not called CC=1 SCAR=6.9
Pod documented
137 0 my $self = shift;
138
139 0 my $func = JavaScript::Code::Function->new(@_);
140 0 $self->add($func);
141
142 0 return $func;
143 }
144
145 =head2 $self->elements( )
146
147 Returns a ref-array of all added elements.
148
149 =cut
150
151 =head2 $self->output( )
152
153 Returns the javascript code for the block.
154
155 =cut
156
157 0 ▶sub output {
Subroutine output: not called CC=3 SCAR=24.8
Pod documented
158 0 my $self = shift;
159 0 ▶ my $scope = shift || 1;
Condition0%0/2
shift() || 1
l!l
00
MC/DC0%0/1
shift() || 1
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
160
161 0 my $indenting = $self->get_indenting($scope);
162 0 my $output = $indenting . "{\n";
163
164 0 my $elements = $self->elements;
165 0 foreach my $element ( @{$elements} ) {
166 0 $output .= $element->output( $scope + 1 );
167 }
168
169 0 $output .= $indenting . "}\n";
170
171 0 return $output;
172 }
173
174 =head1 SEE ALSO
175
176 L<JavaScript::Code>
177
178 =head1 AUTHOR
179
180 Sascha Kiefer, C<esskar@cpan.org>
181
182 =head1 LICENSE
183
184 This library is free software, you can redistribute it and/or modify it under
185 the same terms as Perl itself.
186
187 =cut
188
189 2 1;
« Code/Expression/Op/Not.pm ↑ Summary Code/Element.pm »