File Coverage

blib/lib/Pinto/Action/List.pm
Criterion Covered Total %
statement 51 59 86.4
branch 14 26 53.8
condition 4 6 66.6
subroutine 9 9 100.0
pod 0 1 0.0
total 78 101 77.2


line stmt bran cond sub pod time code
1             # ABSTRACT: List the contents of a stack
2              
3             package Pinto::Action::List;
4              
5 5     5   12031 use Moose;
  5         42  
  5         176  
6 5     5   55663 use MooseX::StrictConstructor;
  5         36  
  5         146  
7 5     5   23379 use MooseX::MarkAsMethods ( autoclean => 1 );
  5         37  
  5         129  
8 5     5   24041 use MooseX::Types::Moose qw(Str Bool);
  5         38  
  5         187  
9              
10 5     5   32217 use Pinto::Constants qw(:color);
  5         50  
  5         1021  
11 5     5   1102 use Pinto::Types qw(StackName StackDefault StackObject);
  5         47  
  5         79  
12              
13             #------------------------------------------------------------------------------
14              
15             our $VERSION = '0.13'; # VERSION
16              
17             #------------------------------------------------------------------------------
18              
19             extends qw( Pinto::Action );
20              
21             #------------------------------------------------------------------------------
22              
23             has stack => (
24             is => 'ro',
25             isa => StackName | StackDefault | StackObject,
26             default => undef,
27             );
28              
29             has pinned => (
30             is => 'ro',
31             isa => Bool,
32             );
33              
34             has authors => (
35             is => 'ro',
36             isa => Str,
37             );
38              
39             has packages => (
40             is => 'ro',
41             isa => Str,
42             );
43              
44             has distributions => (
45             is => 'ro',
46             isa => Str,
47             );
48              
49             has all => (
50             is => 'ro',
51             isa => Bool,
52             default => 0,
53             );
54              
55             has format => (
56             is => 'ro',
57             isa => Str,
58             default => '[%F] %-40p %12v %a/%f',
59             lazy => 1,
60             );
61              
62             #------------------------------------------------------------------------------
63              
64             sub _where {
65 9     9   27 my ($self) = @_;
66              
67 9         30 my $where = {};
68 9 50       271 if ($self->all) {
69              
70 0 0       0 if ( my $pkg_name = $self->packages ) {
71 0         0 $where->{'me.name'} = {regexp => qr/$pkg_name/ };
72             }
73              
74 0 0       0 if ( my $dist_name = $self->distributions ) {
75 0         0 $where->{'distribution.archive'} = {regexp => qr/$dist_name/};
76             }
77              
78 0 0       0 if ( my $authors = $self->authors ) {
79 0         0 $where->{'distribution.author'} = {regexp => qr/$authors/i};
80             }
81             }
82             else {
83              
84 9         303 my $stack = $self->repo->get_stack( $self->stack );
85 9         262 $where = {revision => $stack->head->id};
86              
87 9 100       71864 if ( my $pkg_name = $self->packages ) {
88 1         53 $where->{'package.name'} = {regexp => qr/$pkg_name/};
89             }
90              
91 9 100       308 if ( my $dist_name = $self->distributions ) {
92 1         22 $where->{'distribution.archive'} = {regexp => qr/$dist_name/};
93             }
94              
95 9 100       253 if ( my $authors = $self->authors ) {
96 2         35 $where->{'distribution.author'} = {regexp => qr/$authors/i};
97             }
98              
99 9 50       224 if ( my $pinned = $self->pinned ) {
100 0         0 $where->{is_pinned} = 1;
101             }
102             }
103              
104 9         2708 return $where;
105             }
106              
107             #------------------------------------------------------------------------------
108              
109             sub _attrs {
110 9     9   27 my ($self) = @_;
111              
112 9         31 my $attrs = {};
113 9 50       254 if ($self->all) {
114 0         0 $attrs = { prefetch => [qw(distribution)], order_by => ['me.name'] };
115             }
116             else {
117 9         65 $attrs = { prefetch => [qw(package distribution)] };
118             }
119              
120 9         35 return $attrs;
121             }
122              
123              
124             #------------------------------------------------------------------------------
125              
126             sub execute {
127 9     9 0 32 my ($self) = @_;
128              
129 9         47 my $where = $self->_where;
130 9         49 my $attrs = $self->_attrs;
131 9 50       220 my $method = 'search_' . ($self->all ? 'package' : 'registration');
132 9         247 my $rs = $self->repo->db->schema->$method( $where, $attrs );
133              
134 9         6321 my $did_match = 0;
135 9         237 while ( my $it = $rs->next ) {
136              
137             # $it could be a registration or a package object, depending
138             # on whether we are listing a stack or the whole repository
139              
140 5         339 my $string = $it->to_string( $self->format );
141 5         405 my $color = undef;
142              
143 5 50       105 $color = $PINTO_PALETTE_COLOR_0
144             if $it->distribution->is_local;
145              
146 5 50 33     282 $color = $PINTO_PALETTE_COLOR_1
147             if $it->isa('Pinto::Schema::Result::Registration') && $it->is_pinned;
148              
149 5         121 $self->show( $string, { color => $color } );
150 5         65 $did_match++;
151             }
152              
153             # If there are any search criteria and nothing matched,
154             # then the exit status should not be successful.
155 9 100 100     59727 $self->result->failed if keys %$where > 1 && !$did_match;
156              
157 9         299 return $self->result;
158             }
159              
160             #------------------------------------------------------------------------------
161              
162             __PACKAGE__->meta->make_immutable;
163              
164             #------------------------------------------------------------------------------
165              
166             1;
167              
168             __END__
169              
170             =pod
171              
172             =encoding UTF-8
173              
174             =for :stopwords Jeffrey Ryan Thalhammer
175              
176             =head1 NAME
177              
178             Pinto::Action::List - List the contents of a stack
179              
180             =head1 VERSION
181              
182             version 0.13
183              
184             =head1 AUTHOR
185              
186             Jeffrey Ryan Thalhammer <jeff@stratopan.com>
187              
188             =head1 COPYRIGHT AND LICENSE
189              
190             This software is copyright (c) 2015 by Jeffrey Ryan Thalhammer.
191              
192             This is free software; you can redistribute it and/or modify it under
193             the same terms as the Perl 5 programming language system itself.
194              
195             =cut