File Coverage

blib/lib/DTL/Fast/Tag/With.pm
Criterion Covered Total %
statement 34 34 100.0
branch 4 4 100.0
condition n/a
subroutine 7 7 100.0
pod 0 3 0.0
total 45 48 93.7


line stmt bran cond sub pod time code
1             package DTL::Fast::Tag::With;
2 4     4   1437 use strict; use utf8; use warnings FATAL => 'all';
  4     4   7  
  4     4   113  
  4         18  
  4         7  
  4         79  
  4         111  
  4         8  
  4         169  
3 4     4   17 use parent 'DTL::Fast::Tag';
  4         7  
  4         23  
4              
5             $DTL::Fast::TAG_HANDLERS{'with'} = __PACKAGE__;
6              
7             #@Override
8 35     35 0 72 sub get_close_tag{return 'endwith';}
9              
10             #@Override
11             sub parse_parameters
12             {
13 23     23 0 39 my $self = shift;
14              
15 23         47 $self->{'mappings'} = {};
16 23 100       90 if( $self->{'parameter'} =~ /^\s*(.+?)\s+as\s+(.+)\s*$/s ) # legacy
17             {
18 1         6 $self->{'mappings'}->{$2} = DTL::Fast::Expression->new($1);
19             }
20             else # modern
21             {
22 22         38 my @parts = ();
23 22         100 my $string = $self->backup_strings($self->{'parameter'});
24            
25 22         158 while ( $string =~ s{^
26             \s*
27             ([^\s\=]+)
28             \s*\=\s*
29             ([^\s\=]+)
30             \s*
31             }{}x
32             )
33             {
34 36         94 $self->{'mappings'}->{$1} = $self->get_backup_or_variable($2);
35             }
36            
37 21 100       52 if ( $string ) {
38             die $self->get_parse_error(
39             "there is an error in `with` parameters"
40 2         27 , 'Passed parameters' => $self->{'parameter'}
41             );
42             }
43             }
44            
45 20         33 return $self;
46             }
47              
48             #@Override
49             sub render
50             {
51 14     14 0 16 my $self = shift;
52 14         11 my $context = shift;
53            
54 14         22 my %vars = ();
55 14         13 foreach my $key (keys(%{$self->{'mappings'}}))
  14         37  
56             {
57 24         55 $vars{$key} = $self->{'mappings'}->{$key}->render($context);
58             }
59            
60 14         36 $context->push_scope()->set(%vars);
61            
62 14         49 my $result = $self->SUPER::render($context);
63              
64 10         26 $context->pop_scope();
65            
66 10         29 return $result;
67             }
68              
69             1;