File Coverage

blib/lib/Pinto/Schema/Result/Package.pm
Criterion Covered Total %
statement 90 107 84.1
branch 15 32 46.8
condition 13 29 44.8
subroutine 32 40 80.0
pod 0 11 0.0
total 150 219 68.4


line stmt bran cond sub pod time code
1 54     54   70446 use utf8;
  54         128  
  54         445  
2              
3             package Pinto::Schema::Result::Package;
4              
5             # Created by DBIx::Class::Schema::Loader
6             # DO NOT MODIFY THE FIRST PART OF THIS FILE
7              
8              
9 54     54   2321 use strict;
  54         111  
  54         1231  
10 54     54   336 use warnings;
  54         111  
  54         1732  
11              
12 54     54   297 use Moose;
  54         136  
  54         667  
13 54     54   348101 use MooseX::NonMoose;
  54         2051  
  54         471  
14 54     54   386444 use MooseX::MarkAsMethods autoclean => 1;
  54         143  
  54         500  
15             extends 'DBIx::Class::Core';
16              
17              
18             __PACKAGE__->table("package");
19              
20              
21             __PACKAGE__->add_columns(
22             "id", { data_type => "integer", is_auto_increment => 1, is_nullable => 0 },
23             "name", { data_type => "text", is_nullable => 0 },
24             "version", { data_type => "text", is_nullable => 0 },
25             "file", { data_type => "text", default_value => \"null", is_nullable => 1 },
26             "sha256", { data_type => "text", default_value => \"null", is_nullable => 1 },
27             "distribution", { data_type => "integer", is_foreign_key => 1, is_nullable => 0 },
28             );
29              
30              
31             __PACKAGE__->set_primary_key("id");
32              
33              
34             __PACKAGE__->add_unique_constraint( "name_distribution_unique", [ "name", "distribution" ] );
35              
36              
37             __PACKAGE__->belongs_to(
38             "distribution",
39             "Pinto::Schema::Result::Distribution",
40             { id => "distribution" },
41             { is_deferrable => 0, on_delete => "CASCADE", on_update => "NO ACTION" },
42             );
43              
44              
45             __PACKAGE__->has_many(
46             "registrations", "Pinto::Schema::Result::Registration",
47             { "foreign.package" => "self.id" }, { cascade_copy => 0, cascade_delete => 0 },
48             );
49              
50              
51             with 'Pinto::Role::Schema::Result';
52              
53             # Created by DBIx::Class::Schema::Loader v0.07033 @ 2013-03-04 12:39:54
54             # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:wYrDViIlHDocM5byRBn1Qg
55              
56             #------------------------------------------------------------------------------
57              
58             # ABSTRACT: Represents a Package provided by a Distribution
59              
60             #------------------------------------------------------------------------------
61              
62 54     54   318422 use String::Format;
  54         6210  
  54         3571  
63              
64 54     54   344 use MooseX::Types::Moose qw(Bool);
  54         122  
  54         597  
65              
66 54     54   259450 use Pinto::Target::Package;
  54         464  
  54         2498  
67 54     54   561 use Pinto::Util qw(itis throw);
  54         131  
  54         4606  
68              
69             use overload (
70 54         476 '""' => 'to_string',
71             '<=>' => 'numeric_compare',
72             'cmp' => 'string_compare',
73             fallback => undef
74 54     54   377 );
  54         121  
75              
76             #------------------------------------------------------------------------------
77              
78             our $VERSION = '0.13'; # VERSION
79              
80             #------------------------------------------------------------------------------
81              
82             __PACKAGE__->inflate_column(
83             'version' => {
84             inflate => sub { version->parse( $_[0] ) },
85             deflate => sub { $_[0]->stringify() },
86             }
87             );
88              
89             #------------------------------------------------------------------------------
90             # Schema::Loader does not create many-to-many relationships for us. So we
91             # must create them by hand here...
92              
93             __PACKAGE__->many_to_many( revisions => 'registration', 'revision' );
94              
95             #------------------------------------------------------------------------------
96              
97             has is_main_module => (
98             is => 'ro',
99             isa => Bool,
100             init_arg => undef,
101             default => sub { $_[0]->distribution->main_module->id eq $_[0]->id },
102             lazy => 1,
103             );
104              
105             #------------------------------------------------------------------------------
106              
107             sub FOREIGNBUILDARGS {
108 215     215 0 876 my ( $class, $args ) = @_;
109              
110 215   50     772 $args ||= {};
111 215 100       873 $args->{version} = 0 if not defined $args->{version};
112              
113             # We're no longer storing the file path and sha digests of each package
114             # because the paths in the META are often wrong anyway, and that would
115             # cause Dist::Metadata to blow up. I had hoped this information would be
116             # used to figure out which distribution a given file came from. But I've
117             # decided that is out of scope for Pinto. Eventually, we'll remove
118             # these from the schema entirely.
119              
120 215   100     820 $args->{file} ||= '';
121 215   50     1942 $args->{sha256} ||= '';
122              
123 215         1380 return $args;
124             }
125              
126             #------------------------------------------------------------------------------
127              
128             sub register {
129 199     199 0 1456 my ( $self, %args ) = @_;
130              
131 199         691 my $stack = $args{stack};
132 199         596 my $pin = $args{pin};
133              
134 199         6002 my $struct = {
135             revision => $stack->head->id,
136             is_pinned => $pin,
137             package_name => $self->name,
138             distribution => $self->get_column('distribution')
139             };
140              
141 199         26706 $self->create_related( registrations => $struct );
142              
143 199         443775 return $self;
144             }
145              
146             #------------------------------------------------------------------------------
147              
148             sub vname {
149 375     375 0 277821 my ($self) = @_;
150              
151 375         8184 return $self->name . '~' . $self->version;
152             }
153              
154             #------------------------------------------------------------------------------
155              
156             sub as_target {
157 0     0 0 0 my ($self) = @_;
158              
159 0         0 return Pinto::Target::Package->new(
160             name => $self->name,
161             version => $self->version
162             );
163             }
164              
165              
166             #------------------------------------------------------------------------------
167              
168             sub is_simile {
169 219     219 0 1977 my($self) = @_;
170              
171 219         4039 my $package = $self->name;
172 219         7103 my $file = $self->file;
173              
174             # Some older version of Pinto did not record the filename of each
175             # package. In that case we must assume that it is a simile.
176 219 50       3564 return 1 if not $file;
177              
178             # The following code was taken from simile() in PAUSE/pmfile.pm
179              
180             # MakeMaker gives them the chance to have the file Simple.pm in
181             # this directory but have the package HTML::Simple in it.
182             # Afaik, they wouldn't be able to do so with deeper nested packages
183 219         1774 $file =~ s|.*/||;
184 219         1500 $file =~ s|\.pm(?:\.PL)?||;
185 219         4737 my $ret = $package =~ m/\b\Q$file\E$/;
186 219   50     951 $ret ||= 0;
187 219 50       788 unless ($ret) {
188             # Apache::mod_perl_guide stuffs it into Version.pm
189 0 0       0 $ret = 1 if lc $file eq 'version';
190             }
191              
192 219         1220 return $ret;
193             }
194              
195             #------------------------------------------------------------------------------
196              
197             sub can_index {
198 218     218 0 905 my ($self) = @_;
199              
200             # Workaround for Net::LibIDN (see GH #194)
201 218 50 33     6590 return 1 if $self->name eq 'Net::LibIDN'
202             and $self->file eq '_LibIDN.pm';
203              
204             # Workaround for FCGI
205 218 50 33     10413 return 1 if $self->name eq 'FCGI'
206             and $self->file eq 'FCGI.PL';
207              
208 218         4130 return $self->is_simile;
209             }
210              
211             #------------------------------------------------------------------------------
212              
213             sub flags {
214 0     0 0 0 my ($self) = @_;
215              
216 0         0 my $format = '%m%s?%x';
217 0         0 return $self->to_string($format);
218             }
219              
220             #------------------------------------------------------------------------------
221              
222             sub to_string {
223 87     87 0 21738 my ( $self, $format ) = @_;
224              
225             # my ($pkg, $file, $line) = caller;
226             # warn __PACKAGE__ . " stringified from $file at line $line";
227              
228             my %fspec = (
229 1     1   103 'p' => sub { $self->name() },
230 76     76   33462 'P' => sub { $self->vname() },
231 0 0   0   0 'x' => sub { $self->can_index ? 'x' : '-'},
232 0 0   0   0 'M' => sub { $self->is_main_module ? 'm' : '-'},
233 1     1   104 'v' => sub { $self->version->stringify() },
234 1 50   1   129 'm' => sub { $self->distribution->is_devel() ? 'd' : 'r' },
235 1     1   100 'h' => sub { $self->distribution->path() },
236 0     0   0 'H' => sub { $self->distribution->native_path() },
237 0     0   0 'f' => sub { $self->distribution->archive },
238 1 50   1   99 's' => sub { $self->distribution->is_local() ? 'l' : 'f' },
239 1     1   102 'S' => sub { $self->distribution->source() },
240 76     76   5421 'a' => sub { $self->distribution->author() },
241 1     1   105 'd' => sub { $self->distribution->name() },
242 76     76   83210 'D' => sub { $self->distribution->vname() },
243 1     1   103 'V' => sub { $self->distribution->version() },
244 1     1   101 'u' => sub { $self->distribution->uri() },
245 0     0   0 'F' => sub { $self->flags },
246 87         2641 );
247              
248             # Some attributes are just undefined, usually because of
249             # oddly named distributions and other old stuff on CPAN.
250 54     54   56015 no warnings 'uninitialized'; ## no critic qw(NoWarnings);
  54         153  
  54         23432  
251              
252 87   66     545 $format ||= $self->default_format();
253 87         604 return String::Format::stringf( $format, %fspec );
254             }
255              
256             #-------------------------------------------------------------------------------
257              
258             sub default_format {
259 75     75 0 171 my ($self) = @_;
260              
261 75         314 return '%a/%D/%P'; # AUTHOR/DIST_VNAME/PKG_VNAME
262             }
263              
264             #-------------------------------------------------------------------------------
265              
266             sub numeric_compare {
267 58     58 0 516 my ( $pkg_a, $pkg_b ) = @_;
268              
269 58         156 my $pkg = __PACKAGE__;
270 58 50 33     1077 throw "Can only compare $pkg objects"
271             if not( itis( $pkg_a, $pkg ) && itis( $pkg_b, $pkg ) );
272              
273 58 100       1564 return 0 if $pkg_a->id == $pkg_b->id;
274              
275 54 100       3097 throw "Cannot compare packages with different names: $pkg_a <=> $pkg_b"
276             if $pkg_a->name ne $pkg_b->name;
277              
278 53   100     2732 my $r = ( $pkg_a->version <=> $pkg_b->version )
279             || ( $pkg_a->distribution->mtime <=> $pkg_b->distribution->mtime );
280              
281             # No two non-identical packages can be considered equal!
282 53 100       1800 throw "Unable to determine ordering: $pkg_a <=> $pkg_b" if not $r;
283              
284 51         326 return $r;
285             }
286              
287             #-------------------------------------------------------------------------------
288              
289             sub string_compare {
290 0     0 0   my ( $pkg_a, $pkg_b ) = @_;
291              
292 0           my $pkg = __PACKAGE__;
293 0 0 0       throw "Can only compare $pkg objects"
294             if not( itis( $pkg_a, $pkg ) && itis( $pkg_b, $pkg ) );
295              
296 0 0         return 0 if $pkg_a->id() == $pkg_b->id();
297              
298 0   0       my $r = ( $pkg_a->name cmp $pkg_b->name )
299             || ( $pkg_a->version <=> $pkg_b->version );
300              
301 0           return $r;
302             }
303              
304             #-------------------------------------------------------------------------------
305              
306             __PACKAGE__->meta->make_immutable;
307              
308             #-------------------------------------------------------------------------------
309             1;
310              
311             __END__
312              
313             =pod
314              
315             =encoding UTF-8
316              
317             =for :stopwords Jeffrey Ryan Thalhammer
318              
319             =head1 NAME
320              
321             Pinto::Schema::Result::Package - Represents a Package provided by a Distribution
322              
323             =head1 VERSION
324              
325             version 0.13
326              
327             =head1 NAME
328              
329             Pinto::Schema::Result::Package
330              
331             =head1 TABLE: C<package>
332              
333             =head1 ACCESSORS
334              
335             =head2 id
336              
337             data_type: 'integer'
338             is_auto_increment: 1
339             is_nullable: 0
340              
341             =head2 name
342              
343             data_type: 'text'
344             is_nullable: 0
345              
346             =head2 version
347              
348             data_type: 'text'
349             is_nullable: 0
350              
351             =head2 file
352              
353             data_type: 'text'
354             default_value: null
355             is_nullable: 1
356              
357             =head2 sha256
358              
359             data_type: 'text'
360             default_value: null
361             is_nullable: 1
362              
363             =head2 distribution
364              
365             data_type: 'integer'
366             is_foreign_key: 1
367             is_nullable: 0
368              
369             =head1 PRIMARY KEY
370              
371             =over 4
372              
373             =item * L</id>
374              
375             =back
376              
377             =head1 UNIQUE CONSTRAINTS
378              
379             =head2 C<name_distribution_unique>
380              
381             =over 4
382              
383             =item * L</name>
384              
385             =item * L</distribution>
386              
387             =back
388              
389             =head1 RELATIONS
390              
391             =head2 distribution
392              
393             Type: belongs_to
394              
395             Related object: L<Pinto::Schema::Result::Distribution>
396              
397             =head2 registrations
398              
399             Type: has_many
400              
401             Related object: L<Pinto::Schema::Result::Registration>
402              
403             =head1 L<Moose> ROLES APPLIED
404              
405             =over 4
406              
407             =item * L<Pinto::Role::Schema::Result>
408              
409             =back
410              
411             =head1 AUTHOR
412              
413             Jeffrey Ryan Thalhammer <jeff@stratopan.com>
414              
415             =head1 COPYRIGHT AND LICENSE
416              
417             This software is copyright (c) 2015 by Jeffrey Ryan Thalhammer.
418              
419             This is free software; you can redistribute it and/or modify it under
420             the same terms as the Perl 5 programming language system itself.
421              
422             =cut