Skip to content

Commit bf3d73f

Browse files
committed
Moved Permission querying to MetaCPAN::Query::Permission
1 parent 0543535 commit bf3d73f

File tree

2 files changed

+85
-60
lines changed

2 files changed

+85
-60
lines changed

lib/MetaCPAN/Document/Permission/Set.pm

Lines changed: 14 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,25 @@
11
package MetaCPAN::Document::Permission::Set;
22

3-
use strict;
4-
use warnings;
5-
63
use Moose;
7-
use Ref::Util qw( is_arrayref );
84

9-
use MetaCPAN::Util qw( single_valued_arrayref_to_scalar );
5+
use MetaCPAN::Query::Permission;
106

117
extends 'ElasticSearchX::Model::Document::Set';
128

13-
sub by_author {
14-
my ( $self, $pauseid ) = @_;
15-
16-
my $body = {
17-
query => {
18-
bool => {
19-
should => [
20-
{ term => { owner => $pauseid } },
21-
{ term => { co_maintainers => $pauseid } },
22-
],
23-
},
24-
},
25-
size => 5_000,
26-
};
27-
28-
my $ret = $self->es->search(
29-
index => $self->index->name,
30-
type => 'permission',
31-
body => $body,
9+
has query_permission => (
10+
is => 'ro',
11+
isa => 'MetaCPAN::Query::Permission',
12+
lazy => 1,
13+
builder => '_build_query_permission',
14+
handles => [qw< by_author by_modules >],
15+
);
16+
17+
sub _build_query_permission {
18+
my $self = shift;
19+
return MetaCPAN::Query::Permission->new(
20+
es => $self->es,
21+
index_name => $self->index->name,
3222
);
33-
return unless $ret->{hits}{total};
34-
35-
my $data = [
36-
sort { $a->{module_name} cmp $b->{module_name} }
37-
map { $_->{_source} } @{ $ret->{hits}{hits} }
38-
];
39-
40-
return { permissions => $data };
41-
}
42-
43-
sub by_modules {
44-
my ( $self, $modules ) = @_;
45-
$modules = [$modules] unless is_arrayref($modules);
46-
47-
my @modules = map +{ term => { module_name => $_ } }, @{$modules};
48-
49-
my $body = {
50-
query => {
51-
bool => { should => \@modules }
52-
},
53-
size => 1_000,
54-
};
55-
56-
my $ret = $self->es->search(
57-
index => $self->index->name,
58-
type => 'permission',
59-
body => $body,
60-
);
61-
return unless $ret->{hits}{total};
62-
63-
my $data = [
64-
sort { $a->{module_name} cmp $b->{module_name} }
65-
map { $_->{_source} } @{ $ret->{hits}{hits} }
66-
];
67-
68-
return { permissions => $data };
6923
}
7024

7125
__PACKAGE__->meta->make_immutable;

lib/MetaCPAN/Query/Permission.pm

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package MetaCPAN::Query::Permission;
2+
3+
use Moose;
4+
5+
use Ref::Util qw( is_arrayref );
6+
7+
use MetaCPAN::Util qw( single_valued_arrayref_to_scalar );
8+
9+
with 'MetaCPAN::Query::Role::Common';
10+
11+
sub by_author {
12+
my ( $self, $pauseid ) = @_;
13+
14+
my $body = {
15+
query => {
16+
bool => {
17+
should => [
18+
{ term => { owner => $pauseid } },
19+
{ term => { co_maintainers => $pauseid } },
20+
],
21+
},
22+
},
23+
size => 5_000,
24+
};
25+
26+
my $ret = $self->es->search(
27+
index => $self->index_name,
28+
type => 'permission',
29+
body => $body,
30+
);
31+
return unless $ret->{hits}{total};
32+
33+
my $data = [
34+
sort { $a->{module_name} cmp $b->{module_name} }
35+
map { $_->{_source} } @{ $ret->{hits}{hits} }
36+
];
37+
38+
return { permissions => $data };
39+
}
40+
41+
sub by_modules {
42+
my ( $self, $modules ) = @_;
43+
$modules = [$modules] unless is_arrayref($modules);
44+
45+
my @modules = map +{ term => { module_name => $_ } }, @{$modules};
46+
47+
my $body = {
48+
query => {
49+
bool => { should => \@modules }
50+
},
51+
size => 1_000,
52+
};
53+
54+
my $ret = $self->es->search(
55+
index => $self->index_name,
56+
type => 'permission',
57+
body => $body,
58+
);
59+
return unless $ret->{hits}{total};
60+
61+
my $data = [
62+
sort { $a->{module_name} cmp $b->{module_name} }
63+
map { $_->{_source} } @{ $ret->{hits}{hits} }
64+
];
65+
66+
return { permissions => $data };
67+
}
68+
69+
no Moose;
70+
__PACKAGE__->meta->make_immutable;
71+
1;

0 commit comments

Comments
 (0)