File Coverage

blib/lib/Connector/Role/LocalPath.pm
Criterion Covered Total %
statement 22 28 78.5
branch 6 12 50.0
condition 2 4 50.0
subroutine 4 4 100.0
pod n/a
total 34 48 70.8


line stmt bran cond sub pod time code
1             package Connector::Role::LocalPath;
2 4     4   4061 use Moose::Role;
  4         15910  
  4         20  
3 4     4   22629 use Template;
  4         56  
  4         103  
4 4     4   2179 use utf8;
  4         50  
  4         28  
5              
6             requires 'DELIMITER';
7              
8             has file => (
9             is => 'rw',
10             isa => 'Str',
11             );
12              
13             has path => (
14             is => 'rw',
15             isa => 'Str',
16             );
17              
18              
19             sub _render_local_path {
20              
21 21     21   40 my $self = shift;
22 21   50     75 my $args = shift || [];
23 21         37 my $data = shift;
24              
25 21         37 my $file;
26 21 50 50     696 if ($self->path()) {
    100          
    50          
27 0         0 my $pattern = $self->path();
28 0         0 $self->log()->debug('Process template ' . $pattern);
29 0 0       0 Template->new({})->process( \$pattern, { ARGS => $args, DATA => $data }, \$file) || die "Error processing file template.";
30             # yes we allow paths here
31 0         0 $file =~ s/[^\s\w\.\-\/\\]//g;
32             } elsif ($self->file()) {
33 19         498 my $pattern = $self->file();
34 19         431 $self->log()->debug('Process template ' . $pattern);
35 19 50       274 Template->new({})->process( \$pattern, { ARGS => $args, DATA => $data }, \$file) || die "Error processing argument template.";
36 19 50       152990 if ($file =~ m{[^\s\w\.\-]}) {
37 0         0 $self->log()->error('Target file name contains disallowed characters! Consider using path instead.');
38 0         0 die "Target file name contains disallowed seperator! Consider using path instead.";
39             }
40 2         6 } elsif (ref $args && scalar @{$args}) {
41 2         45 $file = join $self->DELIMITER(), @{$args};
  2         8  
42             }
43              
44 21         832 return $file;
45             }
46              
47             1;
48              
49             __END__;
50              
51              
52             =head1 Connector::Role::LocalPath
53              
54             This role is used to generate file or pathnames from a template string
55             and the path arguments given. It also accepts additional data as a hash.
56              
57             =head2 Parameters
58              
59             =over
60              
61             =item file
62              
63             A template toolkit string to generate the filename to write to. The
64             arguments given as connector location are available in I<ARGS>, the
65             payload data in I<DATA>. The class will die if the result contains
66             any other characters than word, whitespace, dash or dot.
67              
68             =item path
69              
70             Similar to file with slash and backslash also allowed. Disallowed
71             characters will be removed and the sanitized string is returned.
72              
73             =back