blib/lib/Labyrinth/Paths.pm

filter: Statementstmt 100% 56 / 56 Branchbran 85.7% 12 / 14 Conditioncond 90.9% 10 / 11 MC/DCmcdc 83.3% 5 / 6 Subroutinesub 100% 9 / 9 Podpod 100% 3 / 3 totaltotal 95.9% 95 / 99 CC 15 SCAR 16.6
CC
15
Cov
96%
parse
9.0
load
5.0
new
3.0
CRAP 5.3
↑ Summary
1 package Labyrinth::Paths;
2  
3 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 our $VERSION = '0.03';
7  
8 # -------------------------------------
9 # Library Modules
10  
11 6 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use Labyrinth;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use Labyrinth::Variables;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # -------------------------------------
17 # The Subs
18  
19 =head1 NAME
20  
21 Labyrinth::Paths - CGI path translation for Labyrinth
22  
23 =head1 DESCRIPTION
24  
25 Allow Labyrinth to translate CGI paths into internal settings and parameters.
26  
27 =head1 FUNCTIONS
28  
29 =head2 Constructor
30  
31 =over 4
32  
33 =item new()
34  
35 Create a new path object.
36  
37 =back
38  
39 =cut
40  
41 3 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
42 3 my ($class,$pathfile) = @_;
43  
44 # create an attributes hash
45 my $atts = {
46 3 ▶ pathfile => $pathfile || $settings{pathfile} || './pathfile.json'
Condition100%5/5
$pathfile || $Labyrinth::Paths::settings{'pathfile'} || './pathfile.json'
l!l
21
$pathfile or $Labyrinth::Paths::settings{"pathfile"}
l!l&&r!l&&!r
111
MC/DC100%2/2
$pathfile || $Labyrinth::Paths::settings{'pathfile'} || './pathfile.json'
$pathfile $Labyrinth::Paths::settings{"pathfile"}
Truth table100%3/3
A or B
A= $pathfileB= $Labyrinth::Paths::settings{"pathfile"}
ABresult
1X1
011
000
47 };
48  
49 # create the object
50 3 bless $atts, $class;
51  
52 3 $atts->load;
53  
54 3 return $atts;
55 };
56  
57 =head2 Methods
58  
59 =head3 Handling Actions
60  
61 =over 4
62  
63 =item load( [$file] )
64  
65 Load the given path file, which can be the default, or specified as a parameter.
66  
67 =item parse()
68  
69 Parse the current path and reset cgiparams and settings values as appropriate.
70  
71 =back
72  
73 =cut
74  
75 7 ▶sub load {
Subroutine load: called CC=5 SCAR=16.1
Pod documented
76 7 my $self = shift;
77 7 my $file = shift;
78  
79 7 ▶ if($file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) { } 1 6 7
80 1 ▶ return unless(-f $file);
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 1 1
81 1 $self->{pathfile} = $file;
82 } else {
83 6 ▶ return unless(-f $self->{pathfile});
Branch: 1 branch
BranchTrueFalseTotal
unless -f $self->{"pathfile"} 2 4 6
84 }
85  
86 5 ▶ my $fh = IO::File->new($self->{pathfile},'r') or return;
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new($self->{"pathfile"}, "r") or return 5 0 5
87 5 local $/ = undef;
88 5 my $json = <$fh>;
89 5 $fh->close;
90  
91 5 eval {
92 5 my $data = decode_json($json);
93 5 $self->{data} = $data->{paths};
94 };
95  
96 5 return;
97 }
98  
99 6 ▶sub parse {
Subroutine parse: called CC=9 SCAR=22.0
Pod documented
100 6 my $self = shift;
101  
102 6 ▶ my $path = $ENV{SCRIPT_URL} || $ENV{SCRIPT_NAME};
Condition100%3/3
$ENV{'SCRIPT_URL'} || $ENV{'SCRIPT_NAME'}
l!l&&r!l&&!r
213
MC/DC100%2/2
$ENV{'SCRIPT_URL'} || $ENV{'SCRIPT_NAME'}
$ENV{'SCRIPT_URL'} $ENV{'SCRIPT_NAME'}
Truth table100%3/3
A || B
A= $ENV{'SCRIPT_URL'}B= $ENV{'SCRIPT_NAME'}
ABresult
1X1
011
000
103 6 ▶ return unless($path);
Branch: 1 branch
BranchTrueFalseTotal
unless $path 3 3 6
104  
105 3 for my $data (@{$self->{data}}) {
106 3 ▶ if($path =~ /$data->{path}/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ /$data->{"path"}/) 2 1 3
107 2 my @vars1 = ($1,$2,$3,$4,$5,$6,$7,$8,$9);
108 2 my @vars2 = @{$data->{variables}};
109  
110 2 ▶ while(@vars1 && @vars2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@vars1 and @vars2) 2 2 4
Condition66%2/3
@vars1 and @vars2
!ll&&!rl&&r
022
MC/DC50%1/2
@vars1 and @vars2
@vars1 @vars2
Truth table66%2/3
A and B
A= @vars1B= @vars2
ABresult
0X0
100
111
111 2 my $name = shift @vars2;
112 2 $cgiparams{$name} = shift @vars1;
113 }
114  
115 2 $cgiparams{$_} = $data->{cgiparams}{$_} for(keys %{$data->{cgiparams}});
116 2 $settings{$_} = $data->{settings}{$_} for(keys %{$data->{settings}});
117  
118 2 return;
119 }
120 }
121 }
122  
123 6 1;
124  
125 __END__
↑ Summary