blib/lib/Magpie/Plugin/URITemplate.pm

filter: Statementstmt 98.0% 51 / 52 Branchbran 78.5% 11 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 2 totaltotal 92.1% 70 / 76 CC 9 SCAR 12.7
CC
9
Cov
94%
uri_template_params
5.2
process_template
5.0
BEGIN
1.0
CRAP 3.6
« blib/lib/Magpie/Dispatcher/RequestParam.pm ↑ Summary blib/lib/Magpie/Types.pm »
1 package Magpie::Plugin::URITemplate;
2 45 $Magpie::Plugin::URITemplate::VERSION = '1.163200';
3 45 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 #
5 # ABSTRACT: A Role to add URI Template-like path variable capture;
6 #
7 45 ▶use Magpie::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 has uri_template_param_names => (
10 is => 'rw',
11 isa => 'ArrayRef',
12 12 ▶ default => sub {[]},
Subroutine __ANON__: called CC=1 SCAR=0.0
13 45 clearer => 'clear_params',
14 );
15  
16 has uri_template => (
17 is => 'rw',
18 isa => 'Str',
19 required => 1,
20 27 ▶ trigger => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
21 27 my $self = shift;
22 27 $self->clear_regex;
23 27 $self->clear_params;
24 },
25 45 );
26  
27 45 has uri_template_regex => (
28 is => 'ro',
29 isa => 'RegexpRef',
30 lazy => 1,
31 builder => '_build_regexp',
32 clearer => 'clear_regex',
33 );
34  
35 15 ▶sub _build_regexp {
Subroutine _build_regexp: called CC=1 SCAR=0.0
36 15 my $self = shift;
37 15 my ($re, $names) = process_template($self->uri_template);
38 15 $self->uri_template_param_names($names);
39 15 return $re;
40 }
41  
42 15 ▶sub uri_template_params {
Subroutine uri_template_params: called CC=5 SCAR=16.4
Pod undocumented
43 15 my $self = shift;
44 15 my $extractor = $self->uri_template_regex;
45 15 my $names = $self->uri_template_param_names;
46 15 my $path = $self->request->path_info;
47 #my @vals = ( $path =~ $extractor );
48 15 my %params = ();
49 15 ▶ if ($path =~ $extractor) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ /$extractor/) 15 0 15
50 15 ▶ for (my $i = 0; $i < @{$names}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$names;}) 22 15 37
51 22 $params{$names->[$i]} = $+{$names->[$i]};
52 }
53 }
54 15 ▶ unless (scalar keys %params == scalar @{$names}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar keys %params == scalar @{$names;}) 0 15 15
55 0 warn "URI template param extraction mismatch\n";
56 }
57  
58  
59  
60 # for (my $i = 0; $i < @{$names}; $i++) {
61 # $params{$names->[$i]} = $vals[$i];
62 # }
63  
64 15 ▶ return wantarray ? %params : \%params;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 15 0 15
65 }
66  
67 24 ▶sub process_template {
Subroutine process_template: called CC=5 SCAR=16.1
Pod undocumented
68 24 my $template = shift;
69 24 my @names = ();
70 24 my $intoken = 0;
71 24 my $token = undef;
72 24 my $transformed = '';
73 #my @chars = split '', $string;
74  
75 24 for (split '', $template) {
76 699 ▶ if ($_ eq '{') {
Branch: 2 branches
BranchTrueFalseTotal
if ($_ eq '{') { } 36 663 699
elsif ($_ eq '}') { } 36 627 663
77 36 $intoken = 1;
78 }
79 elsif ($_ eq '}') {
80 36 push @names, $token;
81 36 $transformed .= '(?<' . $token . '>[^/]*)';
82 36 $token = undef;
83 36 $intoken = 0;
84  
85 }
86 else {
87 627 ▶ if ($intoken) {
Branch: 1 branch
BranchTrueFalseTotal
if ($intoken) { } 296 331 627
88 296 $token .= $_;
89 }
90 else {
91 331 $transformed .= $_;
92 }
93 }
94 }
95  
96 24 my $re = qr|$transformed|;
97 24 return ($re, \@names);
98  
99 }
100  
101 45 ▶no Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
102  
103 45 1;
104  
105 __END__
« blib/lib/Magpie/Dispatcher/RequestParam.pm ↑ Summary blib/lib/Magpie/Types.pm »