Resolver.pm

filter: Statementstmt 83.8% 52 / 62 Branchbran 59.0% 13 / 22 Conditioncond 50.0% 7 / 14 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 100% 11 / 11 Podpod 100% 2 / 2 totaltotal 72.2% 86 / 119 CC 19 SCAR 23.2
CC
19
Cov
73%
_entity_to_properties
20.5
resolve
5.7
_get_property
3.5
CRAP 10.2
↑ Summary Transformer.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.05';
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 5 ▶ return bless( {}, shift )->_init(@_);
Subroutine new: called CC=1 SCAR=0.0
Pod documented
18 }
19  
20 sub _entity_to_properties {
21 31 ▶ my ( $entity, $properties, $prefix ) = @_;
Subroutine _entity_to_properties: called CC=12 SCAR=30.2
22  
23 31 ▶ $properties = {} unless $properties;
Branch: 1 branch
BranchTrueFalseTotal
unless $properties 5 26 31
24  
25 31 my $ref = ref($entity);
26 31 ▶ if ( ( $ref && $ref eq 'HASH' ) || blessed($entity) ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($ref and $ref eq 'HASH' or builtin::blessed($entity)) { } 22 9 31
elsif ($ref and $ref eq 'ARRAY') { } 0 9 9
elsif ($ref) { } 0 9 9
Condition66%4/6
$ref and $ref eq "HASH" or builtin::blessed($entity)
l!l&&r!l&&!r
2209
$ref and $ref eq "HASH"
!ll&&!rl&&r
9022
MC/DC33%1/3
$ref and $ref eq "HASH" or builtin::blessed($entity)
$ref $ref eq "HASH" builtin::blessed($entity)
Truth table40%2/5
A and B or C
A= $refB= $ref eq "HASH"C= builtin::blessed($entity)
ABCresult
11X1
0X11
1011
0X00
1000
27 22 foreach my $key ( keys( %{$entity} ) ) {
28 26 ▶ _entity_to_properties( $entity->{$key}, $properties,
Branch: 1 branch
BranchTrueFalseTotal
$prefix ? : 17 9 26
29 ( $prefix ? "$prefix.$key" : $key ) );
30 }
31 }
32 ▶ elsif ( $ref && $ref eq 'ARRAY' ) {
Condition33%1/3
$ref and $ref eq "ARRAY"
!ll&&!rl&&r
900
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
33 0 my $index = 0;
34 0 foreach my $array_entity ( @{$entity} ) {
35 0 ▶ _entity_to_properties( $array_entity, $properties,
Branch: 1 branch
BranchTrueFalseTotal
$prefix ? : 0 0 0
36 ( $prefix ? "$prefix\[$index\]" : "[$index]" ) );
37 0 $index++;
38 }
39 }
40 elsif ($ref) {
41 0 croak("unsupported ref type '$ref'");
42 }
43 else {
44 9 $properties->{$prefix} = $entity;
45 }
46  
47 31 return $properties;
48 }
49  
50 sub _get_property {
51 7 ▶ my ( $self, $value, $transform ) = @_;
Subroutine _get_property: called CC=3 SCAR=12.5
52 7 my $transformed = $self->{transformer}->transform( $value, $transform );
53 7 ▶ croak( "undefined value $value" . ( $transform ? ", using transform $transform" : '' ) )
Branch: 2 branches
BranchTrueFalseTotal
unless defined $transformed 0 7 7
$transform ? : 0 0 0
54 unless ( defined($transformed) );
55 7 return $transformed;
56 }
57  
58 sub _init {
59 5 ▶ my ( $self, $entity, %options ) = @_;
Subroutine _init: called CC=2 SCAR=7.1
60  
61 5 ▶ my $os = $options{os} || $^O;
Condition33%1/3
$options{'os'} || $^O
l!l&&r!l&&!r
050
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 5 $logger->debug('creating new Resolver');
64  
65 5 $self->{entity} = $entity;
66 5 $self->{transformer} = Template::Transformer->new( $os, _entity_to_properties($entity) );
67  
68 5 return $self;
69 }
70  
71 sub resolve {
72 7 ▶ my ( $self, %options ) = @_;
Subroutine resolve: called CC=5 SCAR=17.4
Pod documented
73  
74 7 ▶ my $key = $options{key} || 'TEMPLATE';
Condition50%1/2
$options{'key'} || 'TEMPLATE'
l!l
70
MC/DC0%0/1
$options{'key'} || 'TEMPLATE'
$options{'key'}
Truth table50%1/2
A
A= $options{'key'}
Aresult
11
00
75  
76 7 my $content;
77 7 ▶ if ( $options{content} ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($options{'content'}) { } 1 6 7
elsif ($options{'handle'}) { } 0 6 6
elsif ($options{'filename'}) { } 6 0 6
78 1 $content = $options{content};
79 }
80 elsif ( $options{handle} ) {
81 0 $content = do { local ($/) = undef; <$options{handle}> };
82 }
83 elsif ( $options{filename} ) {
84 6 $content = do { local ( @ARGV, $/ ) = $options{filename}; <> };
85 }
86 else {
87 0 croak('Must provide one of [content, handle, filename]');
88 }
89  
90 7 $content =~ s/\$\{$key(?:_(.*?))?\{(.*?)\}\}/$self->_get_property($2,$1)/egs;
91 7 return $content;
92 }
93  
94 2 1;
95  
96 __END__
↑ Summary Transformer.pm »