| 1 | package Mojolicious::Plugin::BModel; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use 5.010; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use Carp qw/ carp croak /; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use File::Find qw/ find /; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use List::Util qw/ first /; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use File::Spec; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use Mojo::Loader; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use Mojo::Base 'Mojolicious::Plugin'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | 3 | our $VERSION = '0.1'; | |||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | 3 | my $DEFAULT_CREATE_DIR = 1; | |||||||||||||||||||||||||||||||
| 17 | 3 | my $DEFAULT_MODEL_DIR = 'Model'; # directory of poject for the Model-modules | |||||||||||||||||||||||||||||||
| 18 | 3 | my %MODULES = (); | |||||||||||||||||||||||||||||||
| 19 | 3 | my $BASE_MODEL = 'Mojolicious::BModel::Base'; | |||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | 0 | ▶ | sub register { | ||||||||||||||||||||||||||||||
|
Subroutine
register: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 22 | 0 | my ( $self, $app, $conf ) = @_; | |||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||
| 24 | 0 | my $app_name = ref $app; # name of calling Mojo app | |||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | # namespace path to Models dir. By default it is the name of application, but it can be redefine. | ||||||||||||||||||||||||||||||||
| 27 | # For example, namespace can be MyPath::ToModel or any other | ||||||||||||||||||||||||||||||||
| 28 | 0 | ▶ | my $namespace = $conf->{namespace} ? $conf->{namespace} : $app_name; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 29 | 0 | ▶ | if ( ! $self->_check_namespace( $namespace ) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 30 | 0 | croak "Wrong format of namespace $namespace. Exit"; | |||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||
| 33 | 0 | my $namespace_path = $self->_convert_namespace_to_path( $namespace ); | |||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||
| 35 | # work with considering operation system | ||||||||||||||||||||||||||||||||
| 36 | 0 | my $path_to_model = File::Spec->catfile( $app->home->child('lib')->to_string, $namespace_path, $DEFAULT_MODEL_DIR ); | |||||||||||||||||||||||||||||||
| 37 | 0 | my $dir_exists = $self->_check_model_dir( $path_to_model ); | |||||||||||||||||||||||||||||||
| 38 | 0 | ▶ | my $to_create_dir = exists $conf->{create_dir} ? $conf->{create_dir} : $DEFAULT_CREATE_DIR; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||
| 40 | 0 | ▶ | if ( ! ( $dir_exists && $to_create_dir ) ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
$dir_exists && $to_create_dir
MC/DC0%0/2
$dir_exists && $to_create_dir
$dir_exists
$to_create_dir
Truth table0%0/3
A && B
A= $dir_existsB= $to_create_dir
| |||||||||||||||||||||||||||||||||
| 41 | 0 | carp "Directory " . File::Spec->catfile( $namespace_path, $DEFAULT_MODEL_DIR ) . " does not exist"; | |||||||||||||||||||||||||||||||
| 42 | 0 | return 1; | |||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||
| 44 | ▶ | elsif ( ! $dir_exists && $to_create_dir ) { | |||||||||||||||||||||||||||||||
|
Condition0%0/3
not $dir_exists and $to_create_dir
MC/DC0%0/2
not $dir_exists and $to_create_dir
not $dir_exists
$to_create_dir
Truth table0%0/3
not(A) and B
A= not $dir_existsB= $to_create_dir
| |||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | mkdir $path_to_model or croak "Could not create directory $path_to_model : $!"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||
| 48 | 0 | $self->load_models( $path_to_model, $namespace, $app ); | |||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||
| 50 | $app->helper( | ||||||||||||||||||||||||||||||||
| 51 | 0 | ▶ | model => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 52 | 0 | my ( $self, $model_name ) = @_; | |||||||||||||||||||||||||||||||
| 53 | 0 | ▶ | croak "Unknown model $model_name" if ! exists $MODULES{ $model_name }; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 54 | 0 | return $MODULES{ $model_name }; | |||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||
| 56 | 0 | ); | |||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||
| 58 | 0 | return 1; | |||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||
| 61 | # check a path to the 'Model' dir | ||||||||||||||||||||||||||||||||
| 62 | 3 | ▶ | sub _check_model_dir { | ||||||||||||||||||||||||||||||
|
Subroutine
_check_model_dir: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 63 | 3 | my ( $self, $path_to_model ) = @_; | |||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||
| 65 | 3 | ▶ | return 1 if -e $path_to_model && -d $path_to_model; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
-e $path_to_model and -d $path_to_model
MC/DC100%2/2
-e $path_to_model and -d $path_to_model
-e $path_to_model
-d $path_to_model
Truth table100%3/3
A and B
A= -e $path_to_modelB= -d $path_to_model
| |||||||||||||||||||||||||||||||||
| 66 | 2 | return; | |||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||
| 69 | # check for validation of namespace | ||||||||||||||||||||||||||||||||
| 70 | 0 | ▶ | sub _check_namespace { | ||||||||||||||||||||||||||||||
|
Subroutine
_check_namespace: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||
| 71 | 0 | my ( $self, $namespace ) = @_; | |||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | 0 | my @splitted_namespace = $self->_separate_namespace_name( $namespace ); | |||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | return if ! scalar @splitted_namespace; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | my $found_wrong_name = first { $_ !~ m/[a-zA-Z0-9]/ } @splitted_namespace; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 77 | 0 | ▶ | return if $found_wrong_name; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||
| 79 | 0 | return 1; | |||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||
| 82 | # convert name like 'Aaaa::Bbbbb::Cccc' to 'Aaaa/Bbbbb/Cccc' for Unix-like OS or 'Aaaa\Bbbbb\Cccc' for Windows | ||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | sub _convert_namespace_to_path { | ||||||||||||||||||||||||||||||
|
Subroutine
_convert_namespace_to_path: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 84 | 0 | my ( $self, $namespace ) = @_; | |||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||
| 86 | 0 | my @splitted_namespace = $self->_separate_namespace_name( $namespace ); | |||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | my $path = scalar @splitted_namespace == 1 ? $splitted_namespace[0] : File::Spec->catfile( @splitted_namespace ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||
| 89 | 0 | return $path; | |||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||
| 92 | 0 | ▶ | sub _separate_namespace_name { | ||||||||||||||||||||||||||||||
|
Subroutine
_separate_namespace_name: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 93 | 0 | my ( $self, $namespace ) = @_; | |||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||
| 95 | 0 | my @splitted_name = split( /\:\:/, $namespace ); | |||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||
| 97 | 0 | return @splitted_name; | |||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||
| 100 | 6 | ▶ | sub _convert_model_dirs_array { | ||||||||||||||||||||||||||||||
|
Subroutine
_convert_model_dirs_array: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||
| 101 | 6 | my ( $self, @dirs ) = @_; | |||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||
| 103 | 6 | my $canonical_name; | |||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||
| 105 | 6 | for my $dir ( @dirs ) { | |||||||||||||||||||||||||||||||
| 106 | 15 | ▶ | next if $dir =~ m/^\s*$/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 107 | 9 | ▶ | $canonical_name = ( defined $canonical_name ) ? $canonical_name . '::' . $dir : $dir; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||
| 110 | 6 | return $canonical_name; | |||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||
| 113 | 1 | ▶ | sub find_models { | ||||||||||||||||||||||||||||||
|
Subroutine
find_models: called CC=1 SCAR=0.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 114 | 1 | my ( $self, $path_to_model, $model_path ) = @_; | |||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||
| 116 | 1 | my @model_dirs = ( $model_path ); | |||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||
| 118 | # find all subdirs in the directory of model | ||||||||||||||||||||||||||||||||
| 119 | find( | ||||||||||||||||||||||||||||||||
| 120 | 7 | ▶ | sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.5
| |||||||||||||||||||||||||||||||||
| 121 | 7 | ▶ | return if ! -d $File::Find::name || $File::Find::name eq $path_to_model; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not -d $File::Find::name or $File::Find::name eq $path_to_model
MC/DC50%1/2
not -d $File::Find::name or $File::Find::name eq $path_to_model
not -d $File::Find::name
$File::Find::name eq $path_to_model
Truth table66%2/3
not(A) or B
A= not -d $File::Find::nameB= $File::Find::name eq $path_to_model
| |||||||||||||||||||||||||||||||||
| 122 | 6 | my $dir_name = $File::Find::name; | |||||||||||||||||||||||||||||||
| 123 | 6 | $dir_name =~ s/$path_to_model//; # remove path to model from directory | |||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||
| 125 | # split dir path name and convert it into name like 'aaa::bbb::ccc' | ||||||||||||||||||||||||||||||||
| 126 | 6 | my @dirs = File::Spec->splitdir( $dir_name ); | |||||||||||||||||||||||||||||||
| 127 | 6 | my $canonical_dir_name = $self->_convert_model_dirs_array( @dirs ); | |||||||||||||||||||||||||||||||
| 128 | 6 | ▶ | if ( ! $canonical_dir_name ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 129 | 0 | carp "Cannot parse dir name $dir_name"; | |||||||||||||||||||||||||||||||
| 130 | 0 | return; | |||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||
| 132 | 6 | push @model_dirs, $model_path . '::' . $canonical_dir_name; | |||||||||||||||||||||||||||||||
| 133 | }, | ||||||||||||||||||||||||||||||||
| 134 | 1 | ( $path_to_model ) | |||||||||||||||||||||||||||||||
| 135 | ); | ||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||
| 137 | 1 | return \@model_dirs; | |||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||
| 140 | # recursive search and download modules with models | ||||||||||||||||||||||||||||||||
| 141 | 0 | ▶ | sub load_models { | ||||||||||||||||||||||||||||||
|
Subroutine
load_models: not called CC=5 SCAR=30.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 142 | 0 | my ( $self, $path_to_model, $namespace, $app ) = @_; | |||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||
| 144 | 0 | my $model_path = $namespace . '::' . $DEFAULT_MODEL_DIR; | |||||||||||||||||||||||||||||||
| 145 | 0 | my @model_dirs = @{ $self->find_models( $path_to_model, $model_path ) }; | |||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||
| 147 | 0 | my $base_load_err = Mojo::Loader::load_class( $BASE_MODEL ); | |||||||||||||||||||||||||||||||
| 148 | 0 | ▶ | croak "Loading base model $BASE_MODEL failed: $base_load_err" if ref $base_load_err; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 149 | { | ||||||||||||||||||||||||||||||||
| 150 | 0 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
| |||||||||||||||||||||||||||||||||
| 151 | 0 | ▶ | *{ "$BASE_MODEL\::app" } = sub { $app }; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||
| 154 | # load modules from every dirs and subdirs of model | ||||||||||||||||||||||||||||||||
| 155 | 0 | for my $dir ( @model_dirs ) { | |||||||||||||||||||||||||||||||
| 156 | 0 | my @model_packages = Mojo::Loader::find_modules( $dir ); | |||||||||||||||||||||||||||||||
| 157 | 0 | for my $pm ( @model_packages ) { | |||||||||||||||||||||||||||||||
| 158 | 0 | my $load_err = Mojo::Loader::load_class( $pm ); | |||||||||||||||||||||||||||||||
| 159 | 0 | ▶ | croak "Loading '$pm' failed: $load_err" if ref $load_err; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 160 | 0 | my ( $basename ) = $pm =~ /$model_path\::(.*)/; | |||||||||||||||||||||||||||||||
| 161 | 0 | $MODULES{ $basename } = $pm->new; | |||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||
| 165 | 0 | return 1; | |||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||
| 168 | 3 | 1; | |||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||
| 170 | __END__ | ||||||||||||||||||||||||||||||||