line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::Model; |
2
|
4
|
|
|
4
|
|
3513
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
4
|
|
|
|
|
12
|
|
|
4
|
|
|
|
|
34
|
|
3
|
|
|
|
|
|
|
|
4
|
4
|
|
|
4
|
|
947
|
use List::Util 'any'; |
|
4
|
|
|
|
|
33
|
|
|
4
|
|
|
|
|
286
|
|
5
|
4
|
|
|
4
|
|
27
|
use Mojo::Loader (); |
|
4
|
|
|
|
|
10
|
|
|
4
|
|
|
|
|
71
|
|
6
|
4
|
|
|
4
|
|
19
|
use Mojo::Util 'camelize'; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
3736
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.12'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub register { |
11
|
4
|
|
|
4
|
1
|
189
|
my ($plugin, $app, $conf) = @_; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
# check if need camelize moniker |
14
|
4
|
|
|
|
|
33
|
my $path = $app->home . '/lib/' . $app->moniker; |
15
|
4
|
50
|
|
|
|
504
|
my $moniker = -d $path ? $app->moniker : camelize($app->moniker); |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
$app->helper( |
18
|
|
|
|
|
|
|
model => sub { |
19
|
9
|
|
|
9
|
|
90099
|
my ($self, $name) = @_; |
20
|
9
|
|
66
|
|
|
62
|
$name //= $conf->{default}; |
21
|
|
|
|
|
|
|
|
22
|
9
|
|
|
|
|
22
|
my $model; |
23
|
9
|
100
|
|
|
|
82
|
return $model if $model = $plugin->{models}{$name}; |
24
|
|
|
|
|
|
|
|
25
|
4
|
50
|
|
|
|
30
|
my $class = _load_class_for_name($plugin, $app, $conf, $name, $moniker) |
26
|
|
|
|
|
|
|
or return undef; |
27
|
|
|
|
|
|
|
|
28
|
4
|
|
|
|
|
26
|
my $params = $conf->{params}{$name}; |
29
|
4
|
100
|
|
|
|
48
|
$model = $class->new(ref $params eq 'HASH' ? %$params : (), app => $app); |
30
|
4
|
|
|
|
|
23
|
$plugin->{models}{$name} = $model; |
31
|
4
|
|
|
|
|
24
|
return $model; |
32
|
|
|
|
|
|
|
} |
33
|
4
|
|
|
|
|
182
|
); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
$app->helper( |
36
|
|
|
|
|
|
|
entity => sub { |
37
|
2
|
|
|
2
|
|
184
|
my ($self, $name) = @_; |
38
|
2
|
|
33
|
|
|
7
|
$name //= $conf->{default}; |
39
|
|
|
|
|
|
|
|
40
|
2
|
50
|
|
|
|
6
|
my $class = _load_class_for_name($plugin, $app, $conf, $name, $moniker) |
41
|
|
|
|
|
|
|
or return undef; |
42
|
|
|
|
|
|
|
|
43
|
2
|
|
|
|
|
5
|
my $params = $conf->{params}{$name}; |
44
|
2
|
50
|
|
|
|
11
|
return $class->new(ref $params eq 'HASH' ? %$params : (), app => $app); |
45
|
|
|
|
|
|
|
} |
46
|
4
|
|
|
|
|
635
|
); |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub _load_class { |
51
|
6
|
|
|
6
|
|
16
|
my $class = shift; |
52
|
|
|
|
|
|
|
|
53
|
6
|
50
|
|
|
|
140
|
my $error = Mojo::Loader->can('new') ? Mojo::Loader->new->load($class) : Mojo::Loader::load_class($class); |
54
|
|
|
|
|
|
|
|
55
|
6
|
50
|
|
|
|
721
|
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
|
|
19
|
my ($plugin, $app, $conf, $name, $moniker) = @_; |
62
|
6
|
50
|
|
|
|
21
|
return $plugin->{classes_loaded}{$name} if $plugin->{classes_loaded}{$name}; |
63
|
|
|
|
|
|
|
|
64
|
6
|
|
100
|
|
|
48
|
my $ns = $conf->{namespaces} // [$moniker . '::Model']; |
65
|
6
|
|
50
|
|
|
39
|
my $base = $conf->{base_classes} // [qw(MojoX::Model)]; |
66
|
|
|
|
|
|
|
|
67
|
6
|
100
|
|
|
|
44
|
$name = camelize($name) if $name =~ /^[a-z]/; |
68
|
|
|
|
|
|
|
|
69
|
6
|
|
|
|
|
134
|
for my $class ( map "${_}::$name", @$ns ) { |
70
|
6
|
50
|
|
|
|
32
|
next unless _load_class($class); |
71
|
|
|
|
|
|
|
|
72
|
6
|
50
|
|
6
|
|
71
|
unless ( any { $class->isa($_) } @$base ) { |
|
6
|
|
|
|
|
56
|
|
73
|
0
|
|
|
|
|
0
|
$app->log->debug(qq[Class "$class" is not a model]); |
74
|
0
|
|
|
|
|
0
|
next; |
75
|
|
|
|
|
|
|
} |
76
|
6
|
|
|
|
|
32
|
$plugin->{classes_loaded}{$name} = $class; |
77
|
6
|
|
|
|
|
30
|
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__ |