Neaf/Util/Container.pm

filter: Statementstmt 100% 91 / 91 Branchbran 87.5% 28 / 32 Conditioncond 78.5% 22 / 28 MC/DCmcdc 56.2% 9 / 16 Subroutinesub 100% 12 / 12 Podpod 100% 7 / 7 totaltotal 90.8% 169 / 186 CC 35 SCAR 23.2
CC
35
Cov
93%
store
17.5
store_check_conflict
9.0
fetch_raw
8.0
CRAP 10.1
« Neaf/Route.pm ↑ Summary Neaf/X/Files.pm »
1 package MVC::Neaf::Util::Container;
2  
3 82 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 82 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 82 our $VERSION = '0.2901';
6  
7 =head1 NAME
8  
9 MVC::Neaf::Util::Container - path & method based container for Not Even A Framework
10  
11 =head1 DESCRIPTION
12  
13 This is utility class.
14 Nothing to see here unless one intends to work on L<MVC::Neaf> itself.
15  
16 This class can hold multiple entities addressed by paths and methods
17 and extract them in the needed order.
18  
19 =head1 SYNOPSIS
20  
21 my $c = MVC::Neaf::Util::Container->new;
22  
23 $c->store( "foo", path => '/foo', method => 'GET' );
24 $c->store( "bar", path => '/foo/bar', exclude => '/foo/bar/baz' );
25  
26 $c->fetch( path => "/foo", method => 'GET' ); # foo
27 $c->fetch( path => "/foo/bar", method => 'GET' ); # foo bar
28 $c->fetch( path => "/foo/bar", method => 'POST' );
29 # qw(bar) - 'foo' limited to GET only
30 $c->fetch( path => "/foo/bar/baz", method => 'GET' );
31 # qw(foo) - 'bar' excluded
32  
33 =cut
34  
35 82 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 82 ▶use parent qw(MVC::Neaf::Util::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
38 82 ▶use MVC::Neaf::Util qw( maybe_list canonize_path path_prefixes supported_methods check_path );
Subroutine BEGIN: called CC=1 SCAR=0.0
39 82 our @CARP_NOT = qw(MVC::Neaf::Route);
40  
41 =head1 ATTRIBUTES
42  
43 =head2 exclusive
44  
45 Only store one item per (path,method) pair, and fail loudly in case of conflicts.
46  
47 =head1 METHODS
48  
49 =head2 store
50  
51 store( $data, %spec )
52  
53 Store $data in container. Spec may include:
54  
55 =over
56  
57 =item path - single path or list of paths, '/' assumed if none.
58  
59 =item method - name of method or array of methods.
60 By default, all methods supported by Neaf.
61  
62 =item exclude - single path or list of paths. None by default.
63  
64 =item prepend - if true, prepend to the list instead of appending.
65  
66 =item tentative (exclusive container only) - if true, don't override existing
67 declarations, and don't complain when overridden.
68  
69 =item override (exclusive container only) - if true, override
70 any preexisting content.
71  
72 =back
73  
74 =cut
75  
76 165 ▶sub store {
Subroutine store: called CC=17 SCAR=28.6
Pod documented
77 165 my ($self, $data, %opt) = @_;
78  
79 $self->my_croak( "'tentative' and 'override' are useless for non-exclusive container" )
80 165 ▶ if !$self->{exclusive} and ( $opt{tentative} or $opt{override} );
Branch: 1 branch
BranchTrueFalseTotal
if not $self->{"exclusive"} and $opt{"tentative"} || $opt{"override"} 0 165 165
Condition50%3/6
not $self->{"exclusive"} and $opt{"tentative"} || $opt{"override"}
!ll&&!rl&&r
191460
$opt{"tentative"} or $opt{"override"}
l!l&&r!l&&!r
00146
MC/DC0%0/3
not $self->{"exclusive"} and $opt{"tentative"} || $opt{"override"}
not $self->{"exclusive"} $opt{"tentative"} $opt{"override"}
Truth table50%2/4
not(A) and B or C
A= not $self->{"exclusive"}B= $opt{"tentative"}C= $opt{"override"}
ABCresult
0XX0
1000
11X1
1011
81  
82 $self->my_croak( "'tentative' and 'override' are mutually exclusive" )
83 165 ▶ if $opt{tentative} and $opt{override};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"tentative"} and $opt{"override"} 0 165 165
Condition66%2/3
$opt{"tentative"} and $opt{"override"}
!ll&&!rl&&r
16410
MC/DC0%0/2
$opt{"tentative"} and $opt{"override"}
$opt{"tentative"} $opt{"override"}
Truth table66%2/3
A and B
A= $opt{"tentative"}B= $opt{"override"}
ABresult
0X0
100
111
84  
85 165 $opt{data} = $data;
86  
87 165 my @methods = map { uc $_ } maybe_list( $opt{method}, supported_methods() );
88  
89 165 my @todo = check_path map { canonize_path( $_ ) } maybe_list( $opt{path}, '' );
90 165 ▶ if ($opt{exclude}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"exclude"}) 3 162 165
91 3 my $rex = join '|', map { quotemeta(canonize_path($_)) }
92 3 check_path maybe_list( $opt{exclude} );
93 3 $opt{exclude} = qr(^(?:$rex)(?:[/?]|$));
94 3 @todo = grep { $_ !~ $opt{exclude} } @todo
95 };
96  
97 165 ▶ if ($self->{exclusive}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"exclusive"}) 19 146 165
98 19 my @list = $self->store_check_conflict( %opt, method => \@methods, path => \@todo );
99 19 ▶ $self->my_croak( "Conflicting path spec: ".join ", ", @list )
Branch: 1 branch
BranchTrueFalseTotal
if @list 1 18 19
100 if @list;
101 };
102  
103 164 foreach my $method ( @methods ) {
104 965 foreach my $path ( @todo ) {
105 979 ▶ my $array = $self->{data}{$method}{$path} ||= [];
Condition100%2/2
$self->{"data"}{$method}{$path} ||= []
l!l
76903
MC/DC100%1/1
$self->{"data"}{$method}{$path} ||= []
$self->{"data"}{$method}{$path}
Truth table100%2/2
A
A= $self->{"data"}{$method}{$path}
Aresult
11
00
106 979 ▶ if ( $self->{exclusive} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'exclusive'}) { } 113 866 979
elsif ($opt{'prepend'}) { } 12 854 866
107 @$array = (\%opt)
108 113 ▶ unless $array->[0] and $opt{tentative} and !$array->[0]{tentative};
Branch: 1 branch
BranchTrueFalseTotal
unless $array->[0] and $opt{"tentative"} and not $array->[0]{"tentative"} 111 2 113
Condition83%5/6
$array->[0] and $opt{"tentative"} and not $array->[0]{"tentative"}
!ll&&!rl&&r
11102
$array->[0] and $opt{"tentative"}
!ll&&!rl&&r
10472
MC/DC66%2/3
$array->[0] and $opt{"tentative"} and not $array->[0]{"tentative"}
$array->[0] $opt{"tentative"} not $array->[0]{"tentative"}
Truth table75%3/4
A and B and not(C)
A= $array->[0]B= $opt{"tentative"}C= not $array->[0]{"tentative"}
ABCresult
0XX0
10X0
1100
1111
109 } elsif ( $opt{prepend} ) {
110 12 unshift @$array, \%opt;
111 } else {
112 854 push @$array, \%opt;
113 };
114 };
115 };
116  
117 164 $self;
118 };
119  
120 =head2 store_check_conflict
121  
122 store_check_conflict( path => ..., method => ... )
123  
124 Check that no previous declarations conflict with the new one.
125  
126 This is only if exclusive was specified.
127  
128 =cut
129  
130 19 ▶sub store_check_conflict {
Subroutine store_check_conflict: called CC=9 SCAR=22.0
Pod documented
131 19 my ($self, %opt) = @_;
132  
133 $self->my_croak( "useless call for non-exclusive container" )
134 19 ▶ unless $self->{exclusive};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"exclusive"} 0 19 19
135  
136 19 ▶ if (!$opt{tentative} and !$opt{override}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt{"tentative"} or $opt{"override"}) 12 7 19
Condition100%3/3
$opt{"tentative"} or $opt{"override"}
l!l&&r!l&&!r
1612
MC/DC100%2/2
$opt{"tentative"} or $opt{"override"}
$opt{"tentative"} $opt{"override"}
Truth table100%3/3
A or B
A= $opt{"tentative"}B= $opt{"override"}
ABresult
1X1
011
000
137 # Check for conflicts before changing anything
138 12 my %conflict;
139 12 foreach my $method ( @{ $opt{method} } ) {
140 64 foreach my $path ( @{ $opt{path} } ) {
141 68 my $existing = $self->{data}{$method}{$path};
142 68 ▶ next unless $existing && $existing->[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $existing and $existing->[0] 61 7 68
Condition66%2/3
$existing and $existing->[0]
!ll&&!rl&&r
6107
MC/DC50%1/2
$existing and $existing->[0]
$existing $existing->[0]
Truth table66%2/3
A and B
A= $existingB= $existing->[0]
ABresult
0X0
100
111
143 7 ▶ next if $existing->[0]->{tentative};
Branch: 1 branch
BranchTrueFalseTotal
if $existing->[0]{"tentative"} 6 1 7
144 1 push @{ $conflict{$path} }, $method;
145 };
146 };
147  
148 my @list =
149 12 map { $_."[".(join ",", sort @{ $conflict{$_} })."]" }
150 sort keys %conflict;
151 12 return @list;
152 };
153  
154 7 return ();
155 };
156  
157 =head2 list_methods
158  
159 Returns methods currently in the storage.
160  
161 =cut
162  
163 3 ▶sub list_methods {
Subroutine list_methods: called CC=1 SCAR=0.0
Pod documented
164 3 my $self = shift;
165  
166 3 return keys %{ $self->{data} };
167 };
168  
169 =head2 list_paths
170  
171 Returns paths for given method, or all if no method given.
172  
173 =cut
174  
175 2 ▶sub list_paths {
Subroutine list_paths: called CC=4 SCAR=13.9
Pod documented
176 2 my ($self, @methods) = @_;
177  
178 2 ▶ @methods = $self->list_methods
Branch: 1 branch
BranchTrueFalseTotal
unless @methods 1 1 2
179 unless @methods;
180  
181 2 my %uniq;
182 2 foreach my $method (@methods) {
183 8 $uniq{$_}++ for keys %{ $self->{data}{$method} };
184 };
185 2 return keys %uniq;
186 };
187  
188 =head2 fetch
189  
190 fetch( %spec )
191  
192 Return all matching previously stored objects,
193 from shorter to longer paths, in order of addition.
194  
195 Spec may include:
196  
197 =over
198  
199 =item path - a single path to match against
200  
201 =item method - method to match against
202  
203 =back
204  
205 =cut
206  
207 292 ▶sub fetch {
Subroutine fetch: called CC=1 SCAR=0.0
Pod documented
208 292 my $self = shift;
209 292 return map { $_->{data} } $self->fetch_raw(@_);
210 };
211  
212 =head2 fetch_last
213  
214 Same as fetch(), but only return the last (last added & longest path) element.
215  
216 =cut
217  
218 10 ▶sub fetch_last {
Subroutine fetch_last: called CC=1 SCAR=0.0
Pod documented
219 10 my $self = shift;
220 10 my ($bucket) = reverse $self->fetch_raw(@_);
221 10 return $bucket->{data};
222 };
223  
224 =head2 fetch_raw
225  
226 Same as fetch(), but return additional info instead of just stored item:
227  
228 {
229 data => $your_item_here,
230 path => $all_the_paths,
231 method => $list_of_methods,
232 ...
233 }
234  
235 =cut
236  
237 302 ▶sub fetch_raw {
Subroutine fetch_raw: called CC=8 SCAR=20.8
Pod documented
238 302 my ($self, %opt) = @_;
239  
240 302 my @missing = grep { !defined $opt{$_} } qw(path method);
241 302 ▶ croak __PACKAGE__."->fetch: required fields missing: @missing"
Branch: 1 branch
BranchTrueFalseTotal
if @missing 0 302 302
242 if @missing;
243  
244 302 my $path = canonize_path( $opt{path} );
245  
246 302 my @ret;
247 302 my $tree = $self->{data}{ $opt{method} };
248  
249 302 ▶ foreach my $prefix ( path_prefixes( $opt{path} || '' ) ) {
Condition100%2/2
$opt{'path'} || ''
l!l
28220
MC/DC100%1/1
$opt{'path'} || ''
$opt{'path'}
Truth table100%2/2
A
A= $opt{'path'}
Aresult
11
00
250 560 my $list = $tree->{$prefix};
251 560 ▶ next unless $list;
Branch: 1 branch
BranchTrueFalseTotal
unless $list 240 320 560
252 320 foreach my $node( @$list ) {
253 353 ▶ next if $node->{exclude} and $opt{path} =~ $node->{exclude};
Branch: 1 branch
BranchTrueFalseTotal
if $node->{"exclude"} and $opt{"path"} =~ /$node->{"exclude"}/ 5 348 353
Condition100%3/3
$node->{"exclude"} and $opt{"path"} =~ /$node->{"exclude"}/
!ll&&!rl&&r
34265
MC/DC100%2/2
$node->{"exclude"} and $opt{"path"} =~ /$node->{"exclude"}/
$node->{"exclude"} $opt{"path"} =~ /$node->{"exclude"}/
Truth table100%3/3
A and B
A= $node->{"exclude"}B= $opt{"path"} =~ /$node->{"exclude"}/
ABresult
0X0
100
111
254 348 push @ret, $node;
255 };
256 };
257  
258 302 return @ret;
259 };
260  
261 =head1 LICENSE AND COPYRIGHT
262  
263 This module is part of L<MVC::Neaf> suite.
264  
265 Copyright 2016-2023 Konstantin S. Uvarin C<khedin@cpan.org>.
266  
267 This program is free software; you can redistribute it and/or modify it
268 under the terms of either: the GNU General Public License as published
269 by the Free Software Foundation; or the Artistic License.
270  
271 See L<http://dev.perl.org/licenses/> for more information.
272  
273 =cut
274  
275 82 1;
« Neaf/Route.pm ↑ Summary Neaf/X/Files.pm »