Skip to content

Commit 2bc8c81

Browse files
committed
move the model loading to a plugin
Note, since the models are stateless I'm keeping them in attributes on the plugin itself. This is not the most common pattern, but all it really does is keep instances late-built but cached
1 parent 61f50f2 commit 2bc8c81

File tree

4 files changed

+38
-20
lines changed

4 files changed

+38
-20
lines changed

lib/MetaCPAN/API.pm

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ use Mojo::Base 'Mojolicious';
2323
use Config::ZOMG ();
2424
use File::Temp ();
2525
use List::Util qw( any );
26-
use MetaCPAN::Model::Search ();
27-
use MetaCPAN::Model::User ();
2826
use MetaCPAN::Script::Runner ();
2927
use Search::Elasticsearch ();
3028
use Try::Tiny qw( catch try );
@@ -36,19 +34,6 @@ has es => sub {
3634
);
3735
};
3836

39-
has model_search => sub {
40-
my $self = shift;
41-
return MetaCPAN::Model::Search->new(
42-
es => $self->es,
43-
index => 'cpan',
44-
);
45-
};
46-
47-
has model_user => sub {
48-
my $self = shift;
49-
return MetaCPAN::Model::User->new( es => $self->es, );
50-
};
51-
5237
sub startup {
5338
my $self = shift;
5439

@@ -84,6 +69,7 @@ sub startup {
8469
$self->minion->add_task(
8570
index_favorite => $self->_gen_index_task_sub('favorite') );
8671

72+
$self->plugin('MetaCPAN::API::Plugin::Model');
8773
$self->_set_up_routes;
8874
}
8975

lib/MetaCPAN/API/Controller/Admin.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ sub identity_search_form { }
66

77
sub search_identities {
88
my $self = shift;
9-
my $data = $self->app->model_user->lookup( $self->param('name'),
9+
my $data = $self->model->user->lookup( $self->param('name'),
1010
$self->param('key') );
1111
$self->stash( user_data => $data );
1212
$self->render('admin/search_identities');

lib/MetaCPAN/API/Controller/Search.pm

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@ package MetaCPAN::API::Controller::Search;
22

33
use Mojo::Base 'Mojolicious::Controller';
44

5-
has model => sub { shift->app->model_search };
6-
75
sub first {
86
my $c = shift;
97
return unless $c->openapi->valid_input;
108
my $args = $c->validation->output;
119

12-
my $results = $c->model->search_for_first_result( $args->{q} );
10+
my $results = $c->model->search->search_for_first_result( $args->{q} );
1311
return $c->render( openapi => $results ) if $results;
1412
$c->rendered(404);
1513
}
@@ -21,7 +19,7 @@ sub web {
2119

2220
my @search = ( @{$args}{qw/q from size/} );
2321
push @search, $args->{collapsed} if exists $args->{collapsed};
24-
my $results = $c->model->search_web(@search);
22+
my $results = $c->model->search->search_web(@search);
2523

2624
return $c->render( json => $results );
2725
}

lib/MetaCPAN/API/Plugin/Model.pm

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package MetaCPAN::API::Plugin::Model;
2+
3+
use Mojo::Base 'Mojolicious::Plugin';
4+
5+
use Carp ();
6+
use MetaCPAN::Model::Search ();
7+
use MetaCPAN::Model::User ();
8+
9+
has app => sub { Carp::croak 'app is required' }, weak => 1;
10+
11+
has search => sub {
12+
my $self = shift;
13+
return MetaCPAN::Model::Search->new(
14+
es => $self->app->es,
15+
index => 'cpan',
16+
);
17+
};
18+
19+
has user => sub {
20+
my $self = shift;
21+
return MetaCPAN::Model::User->new( es => $self->app->es );
22+
};
23+
24+
sub register {
25+
my ( $plugin, $app, $conf ) = @_;
26+
$plugin->app($app);
27+
28+
# cached models
29+
$app->helper( 'model.search' => sub { $plugin->search } );
30+
$app->helper( 'model.user' => sub { $plugin->user } );
31+
}
32+
33+
1;
34+

0 commit comments

Comments
 (0)