| 1 | package App::AutoCRUD::Context; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.010; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use MooseX::SemiAffordanceAccessor; # writer methods as "set_*" | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Scalar::Does qw/does/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Encode (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use namespace::clean -except => 'meta'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 1 | has 'app' => (is => 'ro', isa => 'App::AutoCRUD', required => 1, | |||||||||||||||||||||||||||
| 17 | handles => [qw/config dir/]); | ||||||||||||||||||||||||||||
| 18 | 1 | has 'req' => (is => 'ro', isa => 'Plack::Request', required => 1, | |||||||||||||||||||||||||||
| 19 | handles => [qw/logger/]); | ||||||||||||||||||||||||||||
| 20 | 1 | has 'req_data' => (is => 'ro', isa => 'HashRef', | |||||||||||||||||||||||||||
| 21 | builder => '_req_data', lazy => 1, init_arg => undef); | ||||||||||||||||||||||||||||
| 22 | 1 | has 'base' => (is => 'ro', isa => 'Str', | |||||||||||||||||||||||||||
| 23 | builder => '_base', lazy => 1, init_arg => undef); | ||||||||||||||||||||||||||||
| 24 | 1 | has 'path' => (is => 'rw', isa => 'Str', | |||||||||||||||||||||||||||
| 25 | builder => '_path', lazy => 1); | ||||||||||||||||||||||||||||
| 26 | 1 | has 'template' => (is => 'rw', isa => 'Str'); | |||||||||||||||||||||||||||
| 27 | 1 | has 'view' => (is => 'rw', isa => 'App::AutoCRUD::View', | |||||||||||||||||||||||||||
| 28 | builder => '_view', lazy => 1); | ||||||||||||||||||||||||||||
| 29 | 1 | has 'process_time' => (is => 'rw', isa => 'Num'); | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 1 | has 'datasource' => (is => 'rw', isa => 'App::AutoCRUD::DataSource', | |||||||||||||||||||||||||||
| 32 | handles => [qw/dbh schema/]); | ||||||||||||||||||||||||||||
| 33 | 1 | has 'title' => (is => 'rw', isa => 'Str', | |||||||||||||||||||||||||||
| 34 | builder => '_title', lazy => 1); | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 7 | ▶ | sub _view { | ||||||||||||||||||||||||||
|
Subroutine
_view: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 7 | my $self = shift; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | # default view, if no specific view was required from the URL | ||||||||||||||||||||||||||||
| 41 | 7 | return $self->app->find_class("View::TT")->new; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 10 | ▶ | sub _req_data { | ||||||||||||||||||||||||||
|
Subroutine
_req_data: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | 10 | my $self = shift; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 10 | require CGI::Expand; | |||||||||||||||||||||||||||
| 49 | 10 | my $req_data = CGI::Expand->expand_cgi($self->req); | |||||||||||||||||||||||||||
| 50 | 10 | _decode_utf8($req_data); | |||||||||||||||||||||||||||
| 51 | 10 | return $req_data; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 7 | ▶ | sub _base { | ||||||||||||||||||||||||||
|
Subroutine
_base: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 55 | 7 | my $self = shift; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 7 | my $base = $self->req->base->as_string; | |||||||||||||||||||||||||||
| 58 | 7 | ▶ | $base .= "/" unless $base =~ m[/$]; # force trailing slash | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 7 | return $base | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 17 | ▶ | sub _path { | ||||||||||||||||||||||||||
|
Subroutine
_path: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 63 | 17 | my $self = shift; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 17 | return $self->req->path; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 17 | ▶ | sub _title { | ||||||||||||||||||||||||||
|
Subroutine
_title: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 69 | 17 | my $self = shift; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 17 | my $title = $self->app->name; | |||||||||||||||||||||||||||
| 72 | 17 | my $datasource = $self->datasource; | |||||||||||||||||||||||||||
| 73 | 17 | ▶ | $title .= "-" . $datasource->name if $datasource; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 17 | return $title; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 54 | ▶ | sub extract_path_segments { | ||||||||||||||||||||||||||
|
Subroutine
extract_path_segments: called CC=7 SCAR=19.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 80 | 54 | my ($self, $n_segments) = @_; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | # check argument | ||||||||||||||||||||||||||||
| 83 | 54 | ▶ | $n_segments >= 0 or croak "illegal n_segments: $n_segments"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 54 | ▶ | $n_segments < 2 or wantarray or croak "n_segments too big for scalar context"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$n_segments < 2 or wantarray
MC/DC0%0/2
$n_segments < 2 or wantarray
$n_segments < 2
wantarray
Truth table66%2/3
A or B
A= $n_segments < 2B= wantarray
| |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | # extract segments | ||||||||||||||||||||||||||||
| 87 | 54 | my $path = $self->path; | |||||||||||||||||||||||||||
| 88 | 54 | my @segments; | |||||||||||||||||||||||||||
| 89 | 54 | ▶ | while ($n_segments-- && $path =~ s[^/([^/]*)][]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$n_segments-- and $path =~ s[^/([^/]*)][]
MC/DC50%1/2
$n_segments-- and $path =~ s[^/([^/]*)][]
$n_segments--
$path =~ s[^/([^/]*)][]
Truth table66%2/3
A and B
A= $n_segments--B= $path =~ s[^/([^/]*)][]
| |||||||||||||||||||||||||||||
| 90 | 69 | push @segments, $1; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | # inject remaining path (without segments) back into context | ||||||||||||||||||||||||||||
| 94 | 54 | $self->set_path($path); | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | # contextual return | ||||||||||||||||||||||||||||
| 97 | 54 | ▶ | return wantarray ? @segments : $segments[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 17 | ▶ | sub maybe_set_view_from_path { | ||||||||||||||||||||||||||
|
Subroutine
maybe_set_view_from_path: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 103 | 17 | my $self = shift; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 17 | my $path = $self->path; | |||||||||||||||||||||||||||
| 106 | 17 | ▶ | if ($path =~ s/\.(\w+)$//) { # e.g. /TABLE/foo/list.yaml?... | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 4 | my $requested_view = $1; | |||||||||||||||||||||||||||
| 108 | 4 | my $view_class = $self->app->find_class("View::".ucfirst $requested_view); | |||||||||||||||||||||||||||
| 109 | 4 | ▶ | if ($view_class) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 4 | $self->set_view($view_class->new); | |||||||||||||||||||||||||||
| 111 | 4 | $self->set_path($path); | |||||||||||||||||||||||||||
| 112 | }; | ||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | #====================================================================== | ||||||||||||||||||||||||||||
| 117 | # UTILITY FUNCTIONS | ||||||||||||||||||||||||||||
| 118 | #====================================================================== | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 23 | ▶ | sub _decode_utf8 { | ||||||||||||||||||||||||||
|
Subroutine
_decode_utf8: called CC=5 SCAR=17.4
| |||||||||||||||||||||||||||||
| 122 | 23 | ▶ | if (does($_[0], 'ARRAY')) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 123 | 0 | _decode_utf8($_) foreach @{$_[0]}; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | elsif (does($_[0], 'HASH')) { | ||||||||||||||||||||||||||||
| 126 | 16 | _decode_utf8($_) foreach values %{$_[0]}; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | else { | ||||||||||||||||||||||||||||
| 129 | 7 | $_[0] = Encode::decode_utf8($_[0], Encode::FB_CROAK); | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 1 | 1; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | __END__ | ||||||||||||||||||||||||||||