line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::Model; |
2
|
4
|
|
|
4
|
|
2858
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
4
|
|
|
|
|
9
|
|
|
4
|
|
|
|
|
26
|
|
3
|
|
|
|
|
|
|
|
4
|
4
|
|
|
4
|
|
771
|
use List::Util 'any'; |
|
4
|
|
|
|
|
18
|
|
|
4
|
|
|
|
|
221
|
|
5
|
4
|
|
|
4
|
|
21
|
use Mojo::Loader (); |
|
4
|
|
|
|
|
5
|
|
|
4
|
|
|
|
|
59
|
|
6
|
4
|
|
|
4
|
|
15
|
use Mojo::Util 'camelize'; |
|
4
|
|
|
|
|
17
|
|
|
4
|
|
|
|
|
3106
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.13'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub register { |
11
|
4
|
|
|
4
|
1
|
157
|
my ($plugin, $app, $conf) = @_; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
# check if need camelize moniker |
14
|
4
|
|
|
|
|
29
|
my $moniker = camelize($app->moniker); |
15
|
4
|
100
|
|
|
|
157
|
$moniker = $app->moniker unless -d $app->home . '/lib/' . $moniker; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
$app->helper( |
18
|
|
|
|
|
|
|
model => sub { |
19
|
9
|
|
|
9
|
|
72217
|
my ($self, $name) = @_; |
20
|
9
|
|
66
|
|
|
41
|
$name //= $conf->{default}; |
21
|
|
|
|
|
|
|
|
22
|
9
|
|
|
|
|
12
|
my $model; |
23
|
9
|
100
|
|
|
|
71
|
return $model if $model = $plugin->{models}{$name}; |
24
|
|
|
|
|
|
|
|
25
|
4
|
50
|
|
|
|
41
|
my $class = _load_class_for_name($plugin, $app, $conf, $name, $moniker) |
26
|
|
|
|
|
|
|
or return undef; |
27
|
|
|
|
|
|
|
|
28
|
4
|
|
|
|
|
29
|
my $params = $conf->{params}{$name}; |
29
|
4
|
100
|
|
|
|
29
|
$model = $class->new(ref $params eq 'HASH' ? %$params : (), app => $app); |
30
|
4
|
|
|
|
|
18
|
$plugin->{models}{$name} = $model; |
31
|
4
|
|
|
|
|
16
|
return $model; |
32
|
|
|
|
|
|
|
} |
33
|
4
|
|
|
|
|
401
|
); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
$app->helper( |
36
|
|
|
|
|
|
|
entity => sub { |
37
|
2
|
|
|
2
|
|
151
|
my ($self, $name) = @_; |
38
|
2
|
|
33
|
|
|
6
|
$name //= $conf->{default}; |
39
|
|
|
|
|
|
|
|
40
|
2
|
50
|
|
|
|
5
|
my $class = _load_class_for_name($plugin, $app, $conf, $name, $moniker) |
41
|
|
|
|
|
|
|
or return undef; |
42
|
|
|
|
|
|
|
|
43
|
2
|
|
|
|
|
6
|
my $params = $conf->{params}{$name}; |
44
|
2
|
50
|
|
|
|
8
|
return $class->new(ref $params eq 'HASH' ? %$params : (), app => $app); |
45
|
|
|
|
|
|
|
} |
46
|
4
|
|
|
|
|
624
|
); |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub _load_class { |
51
|
6
|
|
|
6
|
|
14
|
my $class = shift; |
52
|
|
|
|
|
|
|
|
53
|
6
|
50
|
|
|
|
135
|
my $error = Mojo::Loader->can('new') ? Mojo::Loader->new->load($class) : Mojo::Loader::load_class($class); |
54
|
|
|
|
|
|
|
|
55
|
6
|
50
|
|
|
|
498
|
return 1 unless $error; |
56
|
0
|
0
|
|
|
|
0
|
die $error if ref $error; |
57
|
0
|
|
|
|
|
0
|
return; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub _load_class_for_name { |
61
|
6
|
|
|
6
|
|
22
|
my ($plugin, $app, $conf, $name, $moniker) = @_; |
62
|
6
|
50
|
|
|
|
20
|
return $plugin->{classes_loaded}{$name} if $plugin->{classes_loaded}{$name}; |
63
|
|
|
|
|
|
|
|
64
|
6
|
|
100
|
|
|
32
|
my $ns = $conf->{namespaces} // [$moniker . '::Model']; |
65
|
6
|
|
50
|
|
|
30
|
my $base = $conf->{base_classes} // [qw(MojoX::Model)]; |
66
|
|
|
|
|
|
|
|
67
|
6
|
100
|
|
|
|
46
|
$name = camelize($name) if $name =~ /^[a-z]/; |
68
|
|
|
|
|
|
|
|
69
|
6
|
|
|
|
|
108
|
for my $class ( map "${_}::$name", @$ns ) { |
70
|
6
|
50
|
|
|
|
33
|
next unless _load_class($class); |
71
|
|
|
|
|
|
|
|
72
|
6
|
50
|
|
6
|
|
59
|
unless ( any { $class->isa($_) } @$base ) { |
|
6
|
|
|
|
|
80
|
|
73
|
0
|
|
|
|
|
0
|
$app->log->debug(qq[Class "$class" is not a model]); |
74
|
0
|
|
|
|
|
0
|
next; |
75
|
|
|
|
|
|
|
} |
76
|
6
|
|
|
|
|
28
|
$plugin->{classes_loaded}{$name} = $class; |
77
|
6
|
|
|
|
|
27
|
return $class; |
78
|
|
|
|
|
|
|
} |
79
|
0
|
|
|
|
|
|
$app->log->debug(qq[Model "$name" does not exist]); |
80
|
0
|
|
|
|
|
|
return undef; |
81
|
|
|
|
|
|
|
}; |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
1; |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
__END__ |