| 1 | package App::AutoCRUD; | ||||||||||||||||||||
| 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::NonMoose; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 1 | extends 'Plack::Component'; | |||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 1 | ▶ | use Plack::Request; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 1 | ▶ | use Plack::Util; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 1 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 1 | ▶ | use Scalar::Does qw/does/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 1 | ▶ | use Clone qw/clone/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 1 | ▶ | use Try::Tiny; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | 1 | ▶ | use YAML::Any qw/Dump/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | 1 | ▶ | use Data::Reach qw/reach/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 1 | ▶ | use namespace::clean -except => 'meta'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | 1 | our $VERSION = '0.15'; | |||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 1 | has 'config' => (is => 'bare', isa => 'HashRef', required => 1); | |||||||||||||||||||
| 26 | 1 | has 'dir' => (is => 'ro', isa => 'Str', | |||||||||||||||||||
| 27 | builder => '_dir', lazy => 1); | ||||||||||||||||||||
| 28 | 1 | has 'name' => (is => 'ro', isa => 'Str', | |||||||||||||||||||
| 29 | builder => '_name', lazy => 1); | ||||||||||||||||||||
| 30 | 1 | has 'title' => (is => 'ro', isa => 'Str', | |||||||||||||||||||
| 31 | builder => '_title', lazy => 1); | ||||||||||||||||||||
| 32 | 1 | has 'datasources' => (is => 'ro', | |||||||||||||||||||
| 33 | isa => 'HashRef', | ||||||||||||||||||||
| 34 | builder => '_datasources', lazy => 1); | ||||||||||||||||||||
| 35 | 1 | has 'share_paths' => (is => 'ro', | |||||||||||||||||||
| 36 | isa => 'ArrayRef', | ||||||||||||||||||||
| 37 | builder => '_share_paths', lazy => 1, auto_deref => 1); | ||||||||||||||||||||
| 38 | 1 | has 'readonly' => (is => 'ro', isa => 'Bool', | |||||||||||||||||||
| 39 | builder => '_readonly', lazy => 1); | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | #====================================================================== | ||||||||||||||||||||
| 44 | # LAZY ATTRIBUTE CONSTRUCTORS | ||||||||||||||||||||
| 45 | #====================================================================== | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | 1 | ▶ | sub _dir { | ||||||||||||||||||
|
Subroutine
_dir: called CC=2 SCAR=7.2
| |||||||||||||||||||||
| 50 | 1 | my $self = shift; | |||||||||||||||||||
| 51 | 1 | ▶ | return $self->config('dir') || '.'; | ||||||||||||||||||
|
Condition50%1/2
$self->config('dir') || '.'
MC/DC0%0/1
$self->config('dir') || '.'
$self->config('dir')
Truth table50%1/2
A
A= $self->config('dir')
| |||||||||||||||||||||
| 52 | } | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | 1 | ▶ | sub _name { | ||||||||||||||||||
|
Subroutine
_name: called CC=2 SCAR=7.2
| |||||||||||||||||||||
| 55 | 1 | my $self = shift; | |||||||||||||||||||
| 56 | 1 | ▶ | return $self->config(qw/app name/) || 'ANONYMOUS_AutoCRUD'; | ||||||||||||||||||
|
Condition50%1/2
$self->config('app', 'name') || 'ANONYMOUS_AutoCRUD'
MC/DC0%0/1
$self->config('app', 'name') || 'ANONYMOUS_AutoCRUD'
$self->config('app', 'name')
Truth table50%1/2
A
A= $self->config('app', 'name')
| |||||||||||||||||||||
| 57 | } | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | 1 | ▶ | sub _title { | ||||||||||||||||||
|
Subroutine
_title: called CC=2 SCAR=7.2
| |||||||||||||||||||||
| 60 | 1 | my $self = shift; | |||||||||||||||||||
| 61 | 1 | ▶ | return $self->config(qw/app title/) || 'Welcome to the wonders of AutoCRUD'; | ||||||||||||||||||
|
Condition50%1/2
$self->config('app', 'title') || 'Welcome to the wonders of AutoCRUD'
MC/DC0%0/1
$self->config('app', 'title') || 'Welcome to the wonders of AutoCRUD'
$self->config('app', 'title')
Truth table50%1/2
A
A= $self->config('app', 'title')
| |||||||||||||||||||||
| 62 | } | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 1 | ▶ | sub _readonly { | ||||||||||||||||||
|
Subroutine
_readonly: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 65 | 1 | my $self = shift; | |||||||||||||||||||
| 66 | 1 | return $self->config(qw/app readonly/); | |||||||||||||||||||
| 67 | } | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 1 | ▶ | sub _datasources { | ||||||||||||||||||
|
Subroutine
_datasources: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 70 | 1 | my $self = shift; | |||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 1 | my $source_class = $self->find_class('DataSource'); | |||||||||||||||||||
| 73 | 1 | my $config_sources = $self->config('datasources'); | |||||||||||||||||||
| 74 | 1 | return {map {($_ => $source_class->new(name => $_, app => $self))} | |||||||||||||||||||
| 75 | sort keys %$config_sources}; | ||||||||||||||||||||
| 76 | } | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 1 | ▶ | sub _share_paths { | ||||||||||||||||||
|
Subroutine
_share_paths: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 79 | 1 | my ($self) = @_; | |||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | # NOTE : we don't use L<File::ShareDir> because of its lack of support for | ||||||||||||||||||||
| 82 | # a development environment. L<File::Share> doesn't help either because | ||||||||||||||||||||
| 83 | # you need to know the distname; here we only know classnames. So in the end, | ||||||||||||||||||||
| 84 | # we put share directories directly under the modules files, which works in | ||||||||||||||||||||
| 85 | # any environment. | ||||||||||||||||||||
| 86 | 1 | my @paths; | |||||||||||||||||||
| 87 | 1 | foreach my $class ($self->meta->linearized_isa) { | |||||||||||||||||||
| 88 | 3 | $class =~ s[::][/]g; | |||||||||||||||||||
| 89 | 3 | my $path = $INC{$class . ".pm"}; | |||||||||||||||||||
| 90 | 3 | $path =~ s[\.pm$][/share]; | |||||||||||||||||||
| 91 | 3 | ▶ | push @paths, $path if -d $path; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | 1 | return \@paths; | |||||||||||||||||||
| 94 | } | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 1 | ▶ | sub BUILD { | ||||||||||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 98 | 1 | my $self = shift; | |||||||||||||||||||
| 99 | 1 | $self->_check_config; | |||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | 1 | ▶ | sub _check_config { | ||||||||||||||||||
|
Subroutine
_check_config: called CC=2 SCAR=7.0
| |||||||||||||||||||||
| 104 | 1 | my $self = shift; | |||||||||||||||||||
| 105 | 1 | my $config_domain_class = $self->find_class("ConfigDomain"); | |||||||||||||||||||
| 106 | 1 | my $domain = $config_domain_class->Config; | |||||||||||||||||||
| 107 | 1 | my $msgs = $domain->inspect($self->{config}); | |||||||||||||||||||
| 108 | 1 | ▶ | die Dump({"ERROR IN CONFIG" => $msgs}) if $msgs; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 109 | } | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | #====================================================================== | ||||||||||||||||||||
| 116 | # METHODS | ||||||||||||||||||||
| 117 | #====================================================================== | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | 17 | ▶ | sub datasource { | ||||||||||||||||||
|
Subroutine
datasource: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 120 | 17 | my ($self, $name) = @_; | |||||||||||||||||||
| 121 | 17 | return $self->datasources->{$name}; | |||||||||||||||||||
| 122 | } | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | 17 | ▶ | sub call { # request dispatcher (see L<Plack::Component>) | ||||||||||||||||||
|
Subroutine
call: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 126 | 17 | my ($self, $env) = @_; | |||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | 17 | ▶ | try { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 129 | 17 | $self->respond($env); | |||||||||||||||||||
| 130 | } | ||||||||||||||||||||
| 131 | 4 | ▶ | catch { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 132 | 4 | return [500, ['Content-type' => 'text/html'], [$self->show_error($_)]]; | |||||||||||||||||||
| 133 | 17 | }; | |||||||||||||||||||
| 134 | } | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | 17 | ▶ | sub respond { # request dispatcher (see L<Plack::Component>) | ||||||||||||||||||
|
Subroutine
respond: called CC=6 SCAR=18.3
Pod
undocumented
| |||||||||||||||||||||
| 140 | 17 | my ($self, $env) = @_; | |||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | 17 | my $controller_name; | |||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | # build context object | ||||||||||||||||||||
| 145 | 17 | ▶ | my $request_class = $self->find_class("Request") || 'Plack::Request'; | ||||||||||||||||||
|
Condition50%1/2
$self->find_class('Request') || 'Plack::Request'
MC/DC0%0/1
$self->find_class('Request') || 'Plack::Request'
$self->find_class('Request')
Truth table50%1/2
A
A= $self->find_class('Request')
| |||||||||||||||||||||
| 146 | 17 | my $req = $request_class->new($env); | |||||||||||||||||||
| 147 | 17 | my $context_class = $self->find_class("Context"); | |||||||||||||||||||
| 148 | 17 | my $context = $context_class->new(app => $self, req => $req); | |||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | # see if a specific view was required in the URL | ||||||||||||||||||||
| 151 | 17 | $context->maybe_set_view_from_path; | |||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | # setup datasource from initial path segment | ||||||||||||||||||||
| 154 | 17 | ▶ | if (my $source_name = $context->extract_path_segments(1)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 155 | 17 | ▶ | if (my $datasource = $self->datasource($source_name)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 156 | # integrate datasource into the context | ||||||||||||||||||||
| 157 | 16 | $datasource->prepare_for_request($req); | |||||||||||||||||||
| 158 | 16 | $context->set_datasource($datasource); | |||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | # setup controller from initial path segment | ||||||||||||||||||||
| 161 | 16 | ▶ | $controller_name = ucfirst($context->extract_path_segments(1)) | ||||||||||||||||||
|
Condition50%1/2
ucfirst $context->extract_path_segments(1) || 'Schema'
MC/DC0%0/1
ucfirst $context->extract_path_segments(1) || 'Schema'
ucfirst $context->extract_path_segments(1)
Truth table50%1/2
A
A= ucfirst $context->extract_path_segments(1)
| |||||||||||||||||||||
| 162 | || 'Schema'; # default | ||||||||||||||||||||
| 163 | } | ||||||||||||||||||||
| 164 | else { | ||||||||||||||||||||
| 165 | 1 | $controller_name = ucfirst($source_name); | |||||||||||||||||||
| 166 | } | ||||||||||||||||||||
| 167 | } | ||||||||||||||||||||
| 168 | else { | ||||||||||||||||||||
| 169 | 0 | $controller_name = 'Home'; | |||||||||||||||||||
| 170 | } | ||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | # call controller | ||||||||||||||||||||
| 173 | 17 | ▶ | my $controller_class = $self->find_class("Controller::$controller_name") | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 174 | or die "no such controller : $controller_name"; | ||||||||||||||||||||
| 175 | 17 | my $controller = $controller_class->new(context => $context); | |||||||||||||||||||
| 176 | 17 | $controller->respond; | |||||||||||||||||||
| 177 | } | ||||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | 8 | ▶ | sub config { | ||||||||||||||||||
|
Subroutine
config: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 181 | 8 | my $self = shift; | |||||||||||||||||||
| 182 | 8 | my $config = $self->{config}; | |||||||||||||||||||
| 183 | 8 | return reach $config, @_; | |||||||||||||||||||
| 184 | } | ||||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | |||||||||||||||||||||
| 187 | 66 | ▶ | sub find_class { | ||||||||||||||||||
|
Subroutine
find_class: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 188 | 66 | my ($self, $name) = @_; | |||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | # try to find $name within namespace of current class, then within parents | ||||||||||||||||||||
| 191 | 66 | foreach my $namespace ($self->meta->linearized_isa) { | |||||||||||||||||||
| 192 | 100 | my $class = $self->try_load_class($name, $namespace); | |||||||||||||||||||
| 193 | 100 | ▶ | return $class if $class; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 194 | } | ||||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | 17 | return; # not found | |||||||||||||||||||
| 197 | } | ||||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | 2 | ▶ | sub default { | ||||||||||||||||||
|
Subroutine
default: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 201 | 2 | my ($self, @path) = @_; | |||||||||||||||||||
| 202 | |||||||||||||||||||||
| 203 | # convenience function, returns default value from config (if any) | ||||||||||||||||||||
| 204 | 2 | return $self->config(default => @path); | |||||||||||||||||||
| 205 | } | ||||||||||||||||||||
| 206 | |||||||||||||||||||||
| 207 | |||||||||||||||||||||
| 208 | 100 | ▶ | sub try_load_class { | ||||||||||||||||||
|
Subroutine
try_load_class: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 209 | 100 | my ($self, $name, $namespace) = @_; | |||||||||||||||||||
| 210 | |||||||||||||||||||||
| 211 | # return classname if loaded successfully; | ||||||||||||||||||||
| 212 | # return undef if not found; | ||||||||||||||||||||
| 213 | # raise exception if found but there is a compilation error | ||||||||||||||||||||
| 214 | 100 | ▶ | my $class = try {Plack::Util::load_class($name, $namespace)} | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 215 | 100 | ▶ | catch {die $_ if $_ !~ /^Can't locate(?! object method)/}; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
| |||||||||||||||||||||
| 216 | 100 | return $class; | |||||||||||||||||||
| 217 | } | ||||||||||||||||||||
| 218 | |||||||||||||||||||||
| 219 | |||||||||||||||||||||
| 220 | 1 | ▶ | sub is_class_loaded { | ||||||||||||||||||
|
Subroutine
is_class_loaded: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||
| 221 | 1 | my ($self, $class) = @_; | |||||||||||||||||||
| 222 | |||||||||||||||||||||
| 223 | # deactivate strict refs because we'll be looking into symbol tables | ||||||||||||||||||||
| 224 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 225 | |||||||||||||||||||||
| 226 | # looking at %{$class."::"} is not enough (it may contain other namespaces); | ||||||||||||||||||||
| 227 | # so we consider a class loaded if it has at least an ISA or a VERSION | ||||||||||||||||||||
| 228 | 1 | ▶ | return @{$class."::ISA"} || ${$class."::VERSION"}; | ||||||||||||||||||
|
Condition33%1/3
@{$class . '::ISA';} || ${$class . '::VERSION';}
MC/DC0%0/2
@{$class . '::ISA';} || ${$class . '::VERSION';}
@{$class . '::ISA';}
${$class . '::VERSION';}
Truth table33%1/3
A || B
A= @{$class . '::ISA';}B= ${$class . '::VERSION';}
| |||||||||||||||||||||
| 229 | |||||||||||||||||||||
| 230 | } | ||||||||||||||||||||
| 231 | |||||||||||||||||||||
| 232 | |||||||||||||||||||||
| 233 | 4 | ▶ | sub show_error { | ||||||||||||||||||
|
Subroutine
show_error: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 234 | 4 | my ($self, $msg) = @_; | |||||||||||||||||||
| 235 | |||||||||||||||||||||
| 236 | 4 | return <<__EOHTML__; | |||||||||||||||||||
| 237 | <!doctype html> | ||||||||||||||||||||
| 238 | <html> | ||||||||||||||||||||
| 239 | <head><title>500 Server Error</title></head> | ||||||||||||||||||||
| 240 | <body><h1>500 Server Error</h1> | ||||||||||||||||||||
| 241 | <pre> | ||||||||||||||||||||
| 242 | $msg | ||||||||||||||||||||
| 243 | </pre> | ||||||||||||||||||||
| 244 | |||||||||||||||||||||
| 245 | <!-- | ||||||||||||||||||||
| 246 | 512 bytes of padding to suppress Internet Explorer's "Friendly error messages" | ||||||||||||||||||||
| 247 | |||||||||||||||||||||
| 248 | From: HOW TO: Turn Off the Internet Explorer 5.x and 6.x | ||||||||||||||||||||
| 249 | "Show Friendly HTTP Error Messages" Feature on the Server Side" | ||||||||||||||||||||
| 250 | http://support.microsoft.com/kb/294807 | ||||||||||||||||||||
| 251 | |||||||||||||||||||||
| 252 | Several frequently-seen status codes have "friendly" error messages | ||||||||||||||||||||
| 253 | that Internet Explorer 5.x displays and that effectively mask the | ||||||||||||||||||||
| 254 | actual text message that the server sends. | ||||||||||||||||||||
| 255 | However, these "friendly" error messages are only displayed if the | ||||||||||||||||||||
| 256 | response that is sent to the client is less than or equal to a | ||||||||||||||||||||
| 257 | specified threshold. | ||||||||||||||||||||
| 258 | For example, to see the exact text of an HTTP 500 response, | ||||||||||||||||||||
| 259 | the content length must be greater than 512 bytes. | ||||||||||||||||||||
| 260 | --> | ||||||||||||||||||||
| 261 | </body> | ||||||||||||||||||||
| 262 | </html> | ||||||||||||||||||||
| 263 | __EOHTML__ | ||||||||||||||||||||
| 264 | } | ||||||||||||||||||||
| 265 | |||||||||||||||||||||
| 266 | |||||||||||||||||||||
| 267 | 1 | 1; # End of App::AutoCRUD | |||||||||||||||||||
| 268 | |||||||||||||||||||||
| 269 | __END__ | ||||||||||||||||||||