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