blib/lib/DTL/Fast/Tag/Url.pm

filter: Statementstmt 95.0% 77 / 81 Branchbran 75.0% 15 / 20 Conditioncond 60.0% 3 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 13 / 13 Podpod 0.0% 0 / 6 totaltotal 85.1% 109 / 128 SCAR 28.5
CC
17
Cov
90%
render_arguments
5.0
render
4.5
parse_parameters
4.0
CRAP 17.3
« blib/lib/DTL/Fast/Entity.pm ↑ Summary blib/lib/DTL/Fast/Expression/Operator/Binary/Mul.pm »
1 package DTL::Fast::Tag::Url;
2 2 ▶use strict; use utf8; use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use parent 'DTL::Fast::Tag::Simple';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 $DTL::Fast::TAG_HANDLERS{'url'} = __PACKAGE__;
6  
7 2 ▶use DTL::Fast::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 #@Override
10 sub parse_parameters
11 {
12 4 ▶ my( $self ) = @_;
Subroutine parse_parameters: called CC=4 SCAR=13.9
Pod undocumented
13  
14 4 ▶ if( $self->{'parameter'} =~ /^\s*(.+?)(?:\s+as\s+([^\s]+))?\s*$/s )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'parameter'} =~ /^\s*(.+?)(?:\s+as\s+([^\s]+))?\s*$/s) { } 4 0 4
15 {
16 4 $self->{'target_name'} = $2;
17 4 my @params = split /\s+/, $self->backup_strings($1);
18
19 4 $self->{'model_path'} = $self->get_backup_or_variable(shift @params);
20 4 ▶ if( scalar @params )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @params) 3 1 4
21 {
22 3 ▶ if( $params[0] =~ /\=/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($params[0] =~ /\=/) { } 2 1 3
23 {
24 2 $self->parse_named_parameters(\@params);
25 }
26 else
27 {
28 1 $self->parse_positional_parameters(\@params);
29 }
30 }
31 }
32 else
33 {
34 0 die $self->get_parse_error("unable to parse url parameters: $self->{'parameter'}");
35 }
36
37 4 return $self;
38 }
39  
40 #@Override
41 sub render
42 {
43 4 ▶ my( $self, $context ) = @_;
Subroutine render: called CC=4 SCAR=15.0
Pod undocumented
44
45 4 my $result = '';
46
47 4 my $url_source = $context->{'ns'}->[-1]->{'_dtl_url_source'};
48
49 4 ▶ if(
Branch: 1 branch
BranchTrueFalseTotal
if (defined $url_source and ref $url_source eq 'CODE') { } 4 0 4
Condition33%1/3
defined $url_source and ref $url_source eq "CODE"
!ll&&!rl&&r
004
MC/DC0%0/2
defined $url_source and ref $url_source eq "CODE"
defined $url_source ref $url_source eq "CODE"
Truth table33%1/3
A and B
A= defined $url_sourceB= ref $url_source eq "CODE"
ABresult
0X0
100
111
50 defined $url_source
51 and ref $url_source eq 'CODE'
52 )
53 {
54 4 my $model_path = $self->{'model_path'}->render($context);
55 4 my $arguments = $self->render_arguments($context);
56 4 my $url_template = $url_source->($model_path, $arguments);
57
58 4 ▶ if( $url_template )
Branch: 1 branch
BranchTrueFalseTotal
if ($url_template) { } 4 0 4
59 {
60 4 $result = $self->restore_url($url_template, $arguments);
61 }
62 else
63 {
64 0 die $self->get_render_error("url source returned false value by model path: $model_path");
65 }
66 }
67 else
68 {
69 0 die $self->get_render_error("in order to render url's you must provide `url_source` argument to the template constructor");
70 }
71
72 4 return $result;
73 }
74  
75 sub restore_url
76 {
77 4 ▶ my( $self, $template, $arguments ) = @_;
Subroutine restore_url: called CC=2 SCAR=6.9
Pod undocumented
78
79 4 ▶ if( ref $arguments eq 'ARRAY' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arguments eq 'ARRAY') { } 2 2 4
80 {
81 my $replacer = sub{
82 4 ▶ return DTL::Fast::Utils::escape((shift @$arguments) // '');
Subroutine __ANON__: called CC=2 SCAR=6.9
Condition100%2/2
shift @$arguments // ""
l!l
22
MC/DC100%1/1
shift @$arguments // ""
shift @$arguments
Truth table100%2/2
A
A= shift @$arguments
Aresult
11
00
83 2 };
84 2 $template =~ s/
85 \(
86 [^)(]+
87 \)
88 \??
89 4 /$replacer->()/xge; # @todo: this one is dumb, need improve
90 }
91 else # MUST be a hash
92 {
93 my $replacer = sub{
94 4 ▶ my( $key ) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
95 4 return DTL::Fast::Utils::escape($arguments->{$key});
96 2 };
97 2 $template =~ s/
98 \(\?<(.+?)>
99 [^)(]+
100 \)
101 \??
102 4 /$replacer->($1)/xge; # @todo: this one is dumb, need improve
103 }
104
105 # removing regexp remains
106 4 $template =~ s/(
107 ^\^
108 |\$$
109 |\(\?\:
110 |\(
111 |\)
112 )//xgs;
113
114 4 return '/'.$template;
115 }
116  
117  
118 sub render_arguments
119 {
120 4 ▶ my( $self, $context ) = @_;
Subroutine render_arguments: called CC=5 SCAR=16.1
Pod undocumented
121
122 4 my $result = [];
123  
124 4 ▶ if( $self->{'arguments'} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"arguments"}) 3 1 4
125 {
126 3 ▶ if( ref $self->{'arguments'} eq 'ARRAY' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'arguments'} eq 'ARRAY') { } 1 2 3
127 {
128 1 $result = [];
129
130 1 foreach my $argument (@{$self->{'arguments'}})
131 {
132 2 push @$result, $argument->render($context);
133 }
134 }
135 else # MUST be a HASH
136 {
137 2 $result = {};
138 2 foreach my $key (keys( %{$self->{'arguments'}}))
139 {
140 4 $result->{$key} = $self->{'arguments'}->{$key}->render($context);
141 }
142 }
143 }
144
145 4 return $result;
146 }
147  
148 sub parse_named_parameters
149 {
150 2 ▶ my( $self, $params ) = @_;
Subroutine parse_named_parameters: called CC=3 SCAR=11.2
Pod undocumented
151
152 2 my $result = {};
153 2 foreach my $param (@$params)
154 {
155 4 ▶ if( $param =~ /^(.+)\=(.+)$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($param =~ /^(.+)\=(.+)$/) { } 4 0 4
156 {
157 4 $result->{$1} = $self->get_backup_or_variable($2);
158 }
159 else
160 {
161 0 die $self->get_parse_error("you can't mix positional and named arguments in url tag: $self->{'parameter'}");
162 }
163 }
164 2 $self->{'arguments'} = $result;
165 2 return $self;
166 }
167  
168 sub parse_positional_parameters
169 {
170 1 ▶ my( $self, $params ) = @_;
Subroutine parse_positional_parameters: called CC=3 SCAR=11.0
Pod undocumented
171
172 1 my $result = [];
173 1 foreach my $param (@$params)
174 {
175 2 ▶ die $self->get_parse_error("you can't mix positional and named arguments in url tag: $self->{'parameter'}")
Branch: 1 branch
BranchTrueFalseTotal
if $param =~ /\=/ 0 2 2
176 if $param =~ /\=/;
177
178 2 push @$result, $self->get_backup_or_variable($param);
179 }
180 1 $self->{'arguments'} = $result;
181 1 return $self;
182 }
183  
184 2 1;
« blib/lib/DTL/Fast/Entity.pm ↑ Summary blib/lib/DTL/Fast/Expression/Operator/Binary/Mul.pm »