Resolver.pm

filter: Statementstmt 92.7% 103 / 111 Branchbran 68.1% 30 / 44 Conditioncond 60.8% 14 / 23 MC/DCmcdc 35.7% 5 / 14 Subroutinesub 100% 17 / 17 Podpod 100% 2 / 2 totaltotal 81.0% 171 / 211 SCAR 37.6
CC
36
Cov
83%
_entity_to_properties
12.4
_resolve_loop
7.6
_get_value
7.5
CRAP 42.8
↑ Summary Overlay.pm »
1 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Template::Resolver;
5 2 $Template::Resolver::VERSION = '1.14';
6 # ABSTRACT: A powerful, and simple, library for resolving placeholders in templated files
7 # PODNAME: Template::Resolver
8  
9 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Log::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Template::Transformer;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 my $logger = Log::Any->get_logger();
15  
16 sub new {
17 13 ▶ return bless( {}, shift )->_init(@_);
Subroutine new: called CC=1 SCAR=0.0
Pod documented
18 }
19  
20 sub _entity_to_properties {
21 107 ▶ my ( $entity, $properties, $prefix ) = @_;
Subroutine _entity_to_properties: called CC=12 SCAR=25.2
22  
23 107 ▶ $properties = {} unless $properties;
Branch: 1 branch
BranchTrueFalseTotal
unless $properties 13 94 107
24  
25 107 my $ref = ref($entity);
26 107 ▶ if ( ( $ref && $ref eq 'HASH' ) || blessed($entity) ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($ref and $ref eq 'HASH' or builtin::blessed($entity)) { } 67 40 107
elsif ($ref and $ref eq 'ARRAY') { } 3 37 40
elsif ($ref) { } 0 37 37
Condition83%5/6
$ref and $ref eq "HASH" or builtin::blessed($entity)
l!l&&r!l&&!r
67040
$ref and $ref eq "HASH"
!ll&&!rl&&r
37367
MC/DC66%2/3
$ref and $ref eq "HASH" or builtin::blessed($entity)
$ref $ref eq "HASH" builtin::blessed($entity)
Truth table60%3/5
A and B or C
A= $refB= $ref eq "HASH"C= builtin::blessed($entity)
ABCresult
11X1
0X11
1011
0X00
1000
27 67 foreach my $key ( keys( %{$entity} ) ) {
28 88 ▶ _entity_to_properties( $entity->{$key}, $properties,
Branch: 1 branch
BranchTrueFalseTotal
$prefix ? : 69 19 88
29 ( $prefix ? "$prefix.$key" : $key ) );
30 }
31 }
32 ▶ elsif ( $ref && $ref eq 'ARRAY' ) {
Condition66%2/3
$ref and $ref eq "ARRAY"
!ll&&!rl&&r
3703
MC/DC50%1/2
$ref and $ref eq "ARRAY"
$ref $ref eq "ARRAY"
Truth table66%2/3
A and B
A= $refB= $ref eq "ARRAY"
ABresult
0X0
100
111
33 3 my $index = 0;
34 3 foreach my $array_entity ( @{$entity} ) {
35 6 ▶ _entity_to_properties( $array_entity, $properties,
Branch: 1 branch
BranchTrueFalseTotal
$prefix ? : 6 0 6
36 ( $prefix ? "$prefix\[$index\]" : "[$index]" ) );
37 6 $index++;
38 }
39 }
40 elsif ($ref) {
41 0 croak("unsupported ref type '$ref'");
42 }
43 else {
44 37 $properties->{$prefix} = $entity;
45 }
46  
47 107 return $properties;
48 }
49  
50 sub _get_property {
51 34 ▶ my ( $self, $value, $transform ) = @_;
Subroutine _get_property: called CC=3 SCAR=12.5
52 34 my $transformed = $self->{transformer}->transform( $value, $transform );
53 34 ▶ croak( "undefined value $value" . ( $transform ? ", using transform $transform" : '' ) )
Branch: 2 branches
BranchTrueFalseTotal
unless defined $transformed 0 34 34
$transform ? : 0 0 0
54 unless ( defined($transformed) );
55 34 return $transformed;
56 }
57  
58 sub _init {
59 13 ▶ my ( $self, $entity, %options ) = @_;
Subroutine _init: called CC=3 SCAR=11.2
60  
61 13 ▶ my $os = $options{os} || $^O;
Condition33%1/3
$options{'os'} || $^O
l!l&&r!l&&!r
0130
MC/DC0%0/2
$options{'os'} || $^O
$options{'os'} $^O
Truth table33%1/3
A || B
A= $options{'os'}B= $^O
ABresult
1X1
011
000
62  
63 13 $logger->debug('creating new Resolver');
64  
65 13 $self->{entity} = $entity;
66 $self->{transformer} = Template::Transformer->new(
67 $os,
68 _entity_to_properties($entity),
69 ( $options{additional_transforms}
70 ? ( additional_transforms => $options{additional_transforms} )
71 13 ▶ : ()
Branch: 1 branch
BranchTrueFalseTotal
$options{'additional_transforms'} ? : 1 12 13
72 )
73 );
74  
75 13 return $self;
76 }
77  
78 sub _resolve_loop {
79 9 ▶ my ( $self, $template_key, $loop_name, $property_name, $content ) = @_;
Subroutine _resolve_loop: called CC=7 SCAR=20.2
80 9 my $property_value = $self->_get_value($property_name);
81 9 my $result = '';
82 9 my $ref = ref($property_value);
83 9 my ( $replacer, $key_match, @keys );
84  
85 9 ▶ if ( $ref && $ref eq 'HASH' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($ref and $ref eq 'HASH') { } 6 3 9
elsif ($ref and $ref eq 'ARRAY') { } 3 0 3
elsif ($ref) { } 0 0 0
Condition66%2/3
$ref and $ref eq "HASH"
!ll&&!rl&&r
036
MC/DC50%1/2
$ref and $ref eq "HASH"
$ref $ref eq "HASH"
Truth table66%2/3
A and B
A= $refB= $ref eq "HASH"
ABresult
0X0
100
111
86 6 ▶ $replacer = sub { return $_[1] ? $_[0] : "${property_name}.${_[0]}" };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 2 12 14
87 6 $key_match = "key";
88 6 @keys = sort( keys(%$property_value) );
89 }
90 ▶ elsif ( $ref && $ref eq 'ARRAY' ) {
Condition33%1/3
$ref and $ref eq "ARRAY"
!ll&&!rl&&r
003
MC/DC0%0/2
$ref and $ref eq "ARRAY"
$ref $ref eq "ARRAY"
Truth table33%1/3
A and B
A= $refB= $ref eq "ARRAY"
ABresult
0X0
100
111
91 3 ▶ $replacer = sub { return $_[1] ? $_[0] : "${property_name}[${_[0]}]" };
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 0 14 14
92 3 $key_match = "ix";
93 3 @keys = keys(@$property_value);
94 }
95 elsif ($ref) {
96 0 croak("'$property_name': cannot loop on unsupported ref type '$ref'");
97 }
98 else {
99 0 croak("'$property_name': does not exist");
100 }
101  
102 my $resolve_template = sub {
103 40 ▶ my ( $text, $key ) = @_;
Subroutine __ANON__: called CC=2 SCAR=7.2
104 40 ▶ if ( $text eq "\$\{\Q$template_key\E<\Q$loop_name\E\.\Q$key_match\E\}\}" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text eq "\${\Q$template_key\E<\Q$loop_name\E.\Q$key_match\E}}") { } 0 40 40
105 0 $text = $key;
106 }
107 else {
108 40 $text =~ s/<\Q$loop_name\E(\.\Q$key_match\E)?>/$replacer->($key,$1)/egs;
109 }
110 40 return $text;
111 9 };
112  
113 9 foreach my $key (@keys) {
114 18 my $line = $content;
115 18 $line =~ s/\$\{$template_key<\Q$loop_name\E\.\Q$key_match\E>\}/$key/egs;
116 18 $line =~ s/(\$\{$template_key.*?\}\})/$resolve_template->($1, $key)/egs;
117 18 $result = $result . $line;
118 }
119  
120 9 return $result;
121 }
122  
123 sub _resolve_loops {
124 14 ▶ my ( $self, $key, $content ) = @_;
Subroutine _resolve_loops: called CC=2 SCAR=6.9
125 14 my $done = 0;
126 14 ▶ while ( !$done ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($done) 20 14 34
127 20 my $converted = $content
128 9 =~ s/\$\{$key<(\S+)>:\{(.*?)\}\}(.*?)\$\{$key<\1>:end\}/$self->_resolve_loop($key,$1,$2,$3)/egs;
129 20 $done = ( $converted == 0 );
130 }
131 14 return $content;
132 }
133  
134 sub _get_value {
135 9 ▶ my ( $self, $key ) = @_;
Subroutine _get_value: called CC=7 SCAR=20.1
136 9 my $val = $self->{entity};
137 9 for my $token ( split( /\./, $key ) ) {
138 19 my ( $name, $indices ) = $token =~ /^(\w+)?((?:\[\d+\])*)$/;
139 19 ▶ croak("Invalid entity: '$key'") if ( !$name && !$indices );
Branch: 1 branch
BranchTrueFalseTotal
unless $name or $indices 0 19 19
Condition33%1/3
$name or $indices
l!l&&r!l&&!r
1900
MC/DC0%0/2
$name or $indices
$name $indices
Truth table33%1/3
A or B
A= $nameB= $indices
ABresult
1X1
011
000
140 19 ▶ $val = $val->{$name} if ($name);
Branch: 1 branch
BranchTrueFalseTotal
if $name 19 0 19
141 19 ▶ if ($indices) {
Branch: 1 branch
BranchTrueFalseTotal
if ($indices) 2 17 19
142 2 for my $index ( split( /\]\[/, substr( $indices, 1, length($indices) - 2 ) ) ) {
143 2 $val = $val->[$index];
144 }
145 }
146 }
147 9 return $val;
148 }
149  
150 sub resolve {
151 14 ▶ my ( $self, %options ) = @_;
Subroutine resolve: called CC=5 SCAR=16.8
Pod documented
152  
153 14 ▶ my $key = $options{key} || 'TEMPLATE';
Condition100%2/2
$options{'key'} || 'TEMPLATE'
l!l
131
MC/DC100%1/1
$options{'key'} || 'TEMPLATE'
$options{'key'}
Truth table100%2/2
A
A= $options{'key'}
Aresult
11
00
154  
155 14 my $content;
156 14 ▶ if ( $options{content} ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($options{'content'}) { } 5 9 14
elsif ($options{'handle'}) { } 0 9 9
elsif ($options{'filename'}) { } 9 0 9
157 5 $content = $options{content};
158 }
159 elsif ( $options{handle} ) {
160 0 $content = do { local ($/) = undef; <$options{handle}> };
161 }
162 elsif ( $options{filename} ) {
163 9 $content = do { local ( @ARGV, $/ ) = $options{filename}; <> };
164 }
165 else {
166 0 croak('Must provide one of [content, handle, filename]');
167 }
168 14 $content = $self->_resolve_loops( $key, $content );
169 14 $content =~ s/\$\{$key(?:_(.*?))?\{(.*?)\}\}/$self->_get_property($2,$1)/egs;
170 14 return $content;
171 }
172  
173 2 1;
174  
175 __END__
↑ Summary Overlay.pm »