File Coverage

blib/lib/Storage/Abstract/Driver/Superpath.pm
Criterion Covered Total %
statement 46 46 100.0
branch 15 16 93.7
condition 3 3 100.0
subroutine 11 11 100.0
pod 5 6 83.3
total 80 82 97.5


line stmt bran cond sub pod time code
1             package Storage::Abstract::Driver::Superpath;
2             $Storage::Abstract::Driver::Superpath::VERSION = '0.008';
3 1     1   12 use v5.14;
  1         3  
4 1     1   4 use warnings;
  1         2  
  1         56  
5              
6 1     1   4 use Mooish::Base -standard;
  1         2  
  1         8  
7              
8             # need this in BEGIN block because we use constants from this package
9 1     1   9448 BEGIN { extends 'Storage::Abstract::Driver' }
10              
11             has param 'superpath' => (
12             isa => SimpleStr,
13             writer => -hidden,
14             );
15              
16             with 'Storage::Abstract::Role::Driver::Meta';
17              
18             sub BUILD
19             {
20             my ($self) = @_;
21              
22             $self->_set_superpath(
23             $self->SUPER::resolve_path(
24             $self->superpath,
25             allow_directory => !!1,
26             )
27             );
28             }
29              
30             sub source_is_array
31             {
32 2     2 0 21 return !!0;
33             }
34              
35             sub _adjust_path
36             {
37 13     13   19 my ($self, $name) = @_;
38              
39 13         28 my $superpath = quotemeta($self->superpath . Storage::Abstract::Driver::DIRSEP_STR);
40 13 100       58 if ($name =~ s{^$superpath}{}) {
41 6         14 return $name;
42             }
43              
44 7         14 return undef;
45             }
46              
47             sub store_impl
48             {
49 2     2 1 5 my ($self, $path, $handle) = @_;
50 2         3 my $new_path = $self->_adjust_path($path);
51              
52 2 100       17 Storage::Abstract::X::Readonly->raise(
53             "file $path cannot be stored because it's outside of path '" . $self->superpath . "'"
54             ) unless defined $new_path;
55              
56 1         18 return $self->source->store($new_path, $handle);
57             }
58              
59             sub is_stored_impl
60             {
61 7     7 1 12 my ($self, $path, %opts) = @_;
62 7         13 my $adjusted_path = $self->_adjust_path($path);
63              
64 7 100 100     24 if ($opts{directory} && !defined $adjusted_path) {
65 3         7 my @parts_path = $self->split_path($path);
66 3         16 my @parts_superpath = $self->split_path($self->superpath);
67              
68 3         9 for my $i (0 .. $#parts_path) {
69 3 50       5 return !!0 if $i > $#parts_superpath;
70 3 100       15 return !!0 if $parts_path[$i] ne $parts_superpath[$i];
71             }
72              
73             # path prefix matches
74 1         10 return !!1;
75             }
76              
77 4 100       13 return !!0 unless defined $adjusted_path;
78 3         41 return $self->source->is_stored($adjusted_path, %opts);
79             }
80              
81             sub retrieve_impl
82             {
83 2     2 1 3 my ($self, $path, $properties) = @_;
84 2         5 my $new_path = $self->_adjust_path($path);
85              
86 2 100       10 Storage::Abstract::X::NotFound->raise("file $path was not found")
87             unless defined $new_path;
88              
89 1         15 return $self->source->retrieve($new_path, $properties);
90             }
91              
92             sub dispose_impl
93             {
94 2     2 1 3 my ($self, $path) = @_;
95 2         4 my $new_path = $self->_adjust_path($path);
96              
97 2 100       7 Storage::Abstract::X::NotFound->raise("file $path was not found")
98             unless defined $new_path;
99              
100 1         28 return $self->source->dispose($new_path);
101             }
102              
103             sub list_impl
104             {
105 1     1 1 1 my $self = shift;
106 1         19 my $list_aref = $self->source->list(@_);
107 1         4 my $superpath = $self->superpath . Storage::Abstract::Driver::DIRSEP_STR;
108              
109 1         2 @{$list_aref} = map { $superpath . $_ } @{$list_aref};
  1         2  
  4         6  
  1         2  
110 1         9 return $list_aref;
111             }
112              
113             1;
114              
115             __END__