Code/Type.pm

filter: Statementstmt 76.7% 43 / 56 Branchbran 46.1% 12 / 26 Conditioncond 30.7% 4 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 90.9% 10 / 11 Podpod 100% 2 / 2 totaltotal 61.2% 71 / 116 CC 19 SCAR 29.1
CC
19
Cov
62%
build
27.8
new
21.9
BEGIN
1.0
CRAP 18.3
« Code/Function.pm ↑ Summary Code/Variable.pm »
1 package JavaScript::Code::Type;
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[
Subroutine BEGIN: called CC=1 SCAR=0.0
6 JavaScript::Code::Accessor
7 JavaScript::Code::Value
8 2 ];
9
10 0 ▶use overload '""' => sub { shift->output };
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine __ANON__: not called CC=1 SCAR=0.2
11
12 2 __PACKAGE__->mk_accessors(qw[ type value ]);
13
14 2 ▶use JavaScript::Code::String ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use JavaScript::Code::Number ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use JavaScript::Code::Array ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use JavaScript::Code::Hash ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18
19 2 $VERSION = '0.08';
20
21 =head1 NAME
22
23 JavaScript::Code::Type - A JavaScript Type
24
25 =head1 SYNOPSIS
26
27 #!/usr/bin/perl
28
29 use strict;
30 use warnings;
31 use JavaScript::Code::Type;
32
33 my $type = JavaScript::Code::Type->new({ type => 'String' })->value("Go for it!");
34
35 print $type->output;
36
37
38 =head1 METHODS
39
40 =head2 JavaScript::Code::Type->new( )
41
42 =cut
43
44 2 ▶sub new {
Subroutine new: called CC=6 SCAR=30.9
Pod documented
45 2 my $this = shift;
46 2 ▶ my $class = ref($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
47
48 2 ▶ return $class->SUPER::new(@_)
Branch: 1 branch
BranchTrueFalseTotal
unless $class eq __PACKAGE__ 2 0 2
49 unless $class eq __PACKAGE__;
50
51 0 my $args = shift;
52
53 0 ▶ my $via = lc( delete $args->{type} || '' )
Branch: 1 branch
BranchTrueFalseTotal
my $via = lc(delete $args->{"type"} || "") or die "No type provided." 0 0 0
Condition0%0/2
delete $args->{'type'} || ''
l!l
00
MC/DC0%0/1
delete $args->{'type'} || ''
delete $args->{'type'}
Truth table0%0/2
A
A= delete $args->{'type'}
Aresult
11
00
54 or die "No type provided.";
55
56 0 $via = ucfirst $via;
57 0 $via = "JavaScript::Code::$via";
58
59 0 eval "require $via"; # make sure, it is loaded
60 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
61
62 0 return $via->new( $args, @_ );
63 }
64
65 =head2 $self->type( )
66
67 Returns a string that represents the underlaying type.
68
69 =cut
70
71 =head2 $self->value( $value )
72
73 Gets or sets the associated value.
74
75 =head2 $self->build( )
76
77 =cut
78
79 4 ▶sub build {
Subroutine build: called CC=14 SCAR=33.3
Pod documented
80 4 my $this = shift;
81 4 ▶ my $class = ref($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
040
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
82
83 4 ▶ my $ref = @_ ? Scalar::Util::reftype( $_[0] ) || '' : '';
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 4 0 4
Condition50%1/2
builtin::reftype($_[0]) or ""
l!l
04
MC/DC0%0/1
builtin::reftype($_[0]) or ""
builtin::reftype($_[0])
Truth table50%1/2
A
A= builtin::reftype($_[0])
Aresult
11
00
84
85 0 ▶ my %args = $ref eq 'HASH' ? %{ shift() } : @_;
Branch: 1 branch
BranchTrueFalseTotal
$ref eq 'HASH' ? : 0 4 4
86 4 my $value = $args{value};
87
88 4 my $object;
89 4 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 2 2 4
90
91 2 my $reftype = ref $value;
92
93 2 ▶ if ( $reftype eq 'ARRAY' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($reftype eq 'ARRAY') { } 0 2 2
elsif ($reftype eq 'HASH') { } 0 2 2
elsif ($reftype eq 'SCALAR' or $reftype eq '') { } 2 0 2
elsif ($value->isa('JavaScript::Code::Value')) { } 0 0 0
94 0 $object = JavaScript::Code::Array->new( { value => $value } );
95 }
96 elsif ( $reftype eq 'HASH' ) {
97 0 $object = JavaScript::Code::Hash->new( { value => $value } );
98 }
99 ▶ elsif ( $reftype eq 'SCALAR' or $reftype eq '' ) {
Condition33%1/3
$reftype eq "SCALAR" or $reftype eq ""
l!l&&r!l&&!r
020
MC/DC0%0/2
$reftype eq "SCALAR" or $reftype eq ""
$reftype eq "SCALAR" $reftype eq ""
Truth table33%1/3
A or B
A= $reftype eq "SCALAR"B= $reftype eq ""
ABresult
1X1
011
000
100 0 ▶ $value = $reftype eq 'SCALAR' ? ${$value} : $value;
Branch: 1 branch
BranchTrueFalseTotal
$reftype eq 'SCALAR' ? : 0 2 2
101 2 ▶ $object =
Branch: 1 branch
BranchTrueFalseTotal
&Scalar::Util::looks_like_number($value) ? : 1 1 2
102 Scalar::Util::looks_like_number($value)
103 ? JavaScript::Code::Number->new( { value => $value } )
104 : JavaScript::Code::String->new( { value => $value } );
105 }
106 elsif ( $value->isa('JavaScript::Code::Value') ) {
107 0 $object = $value;
108 }
109
110 2 ▶ die "Unexpected type '$reftype'." unless defined $object;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $object 0 2 2
111 }
112
113 4 return $object;
114 }
115
116 =head2 $self->output( )
117
118 Returns the javascript-code for that type.
119
120 =cut
121
122 =head1 SEE ALSO
123
124 L<JavaScript::Code>
125
126 =head1 AUTHOR
127
128 Sascha Kiefer, C<esskar@cpan.org>
129
130 =head1 LICENSE
131
132 This library is free software, you can redistribute it and/or modify it under
133 the same terms as Perl itself.
134
135 =cut
136
137 2 1;
138
« Code/Function.pm ↑ Summary Code/Variable.pm »