1 package CPANPLUS::Dist::Gentoo::Atom;
8 CPANPLUS::Dist::Gentoo::Atom - Gentoo atom object.
16 our $VERSION = '0.10';
20 This class models Gentoo atoms.
28 '<=>' => \&_spaceship,
33 use CPANPLUS::Dist::Gentoo::Version;
35 my $range_rx = qr/(?:<|<=|=|>=|>)/;
36 my $name_rx = qr/[a-zA-Z0-9_+-]+/;
37 my $category_rx = $name_rx;
38 my $version_rx = $CPANPLUS::Dist::Gentoo::Version::version_rx;
42 =head2 C<< new category => $category, name => $name [, version => $version, range => $range, ebuild => $ebuild ] >>
44 Creates a new L<CPANPLUS::Dist::Gentoo::Atom> object from the supplied C<$category>, C<$name>, C<$version>, C<$range> and C<$ebuild>.
50 $class = ref($class) || $class;
54 my ($range, $category, $name, $version);
55 if (defined $args{name}) {
56 ($range, $category, $name, $version) = @args{qw/range category name version/};
57 Carp::confess('Category unspecified') unless defined $category;
58 Carp::confess('Invalid category') unless $category =~ /^$category_rx$/o;
59 Carp::confess('Invalid name') unless $name =~ /^$name_rx$/o;
60 } elsif (defined $args{atom}) {
61 my $atom = $args{atom};
62 $atom =~ m{^($range_rx)?($category_rx)/($name_rx)(?:-v?($version_rx))?$}o
63 or Carp::confess('Invalid atom');
64 ($range, $category, $name, $version) = ($1, $2, $3, $4);
66 Carp::confess('Not enough information for building an atom object');
69 if (defined $version) {
70 unless (Scalar::Util::blessed($version)
71 and $version->isa('CPANPLUS::Dist::Gentoo::Version')) {
72 $version = CPANPLUS::Dist::Gentoo::Version->new($version);
76 if (defined $version) {
78 Carp::confess("Invalid range $range") unless $range =~ /^$range_rx$/o;
83 Carp::confess('Range atoms require a valid version')
84 if defined $range and length $range;
88 category => $category,
92 ebuild => $args{ebuild},
96 =head2 C<new_from_ebuild $ebuild>
98 Creates a new L<CPANPLUS::Dist::Gentoo::Atom> object by inferring the category, name and version from the given C<$ebuild>
102 sub new_from_ebuild {
104 $class = ref($class) || $class;
107 $ebuild = '' unless defined $ebuild;
109 $ebuild =~ m{/($category_rx)/($name_rx)/\2-v?($version_rx)\.ebuild$}o
110 or Carp::confess('Invalid ebuild');
111 my ($category, $name, $version) = ($1, $2, $3);
114 category => $category,
122 eval "sub $_ { \$_[0]->{$_} }" for qw/category name version range ebuild/;
127 Read-only accessor to the atom category.
131 Read-only accessor to the atom name.
135 Read-only accessor to the L<CPANPLUS::Dist::Gentoo::Version> object associated with the atom.
139 Read-only accessor to the atom range.
143 Read-only accessor to the path of an optional ebuild associated with the atom.
145 =head2 C<qualified_name>
147 Returns the qualified name for the atom, i.e. C<$category/$name>.
151 sub qualified_name { join '/', $_[0]->category, $_[0]->name }
154 my ($a1, $a2, $r) = @_;
156 my $v1 = $a1->version;
159 my $blessed = Scalar::Util::blessed($a2);
160 unless ($blessed and $a2->isa(__PACKAGE__)) {
161 if ($blessed and $a2->isa('CPANPLUS::Dist::Gentoo::Version')) {
165 my $maybe_atom = eval { __PACKAGE__->new(atom => $a2) };
167 $v2 = eval { CPANPLUS::Dist::Gentoo::Version->new($a2) };
168 Carp::confess("Can't compare an atom against something that's not an atom, an atom string ($err), a version or a version string ($@)") if $@;
179 my $p1 = $a1->qualified_name;
180 my $p2 = $a2->qualified_name;
181 Carp::confess("Atoms for different packages $p1 and $p2") unless $p1 eq $p2;
184 ($v1, $v2) = ($v2, $v1) if $r;
186 return (defined $v1 or 0) <=> (defined $v2 or 0) unless defined $v1
193 my ($a1, $a2, $r) = @_;
195 my $s1 = $a1->qualified_name;
196 my $v1 = $a1->version;
197 $s1 .= "-$v1" if defined $v1;
200 if (Scalar::Util::blessed($a2) and $a2->isa(__PACKAGE__)) {
201 $s2 = $a2->qualified_name;
202 my $v2 = $a2->version;
203 $s2 .= "-$v2" if defined $v2;
214 my $atom = $a->qualified_name;
216 my $version = $a->version;
217 $atom = $a->range . $atom . '-' . $version if defined $version;
232 Compute the ranged atom representing the logical AND between C<@atoms> with the same category and name.
237 shift unless length ref $_[0];
240 return $a1 unless @_;
243 $a2 = $a2->and(@_) if @_;
245 my $p1 = $a1->qualified_name;
246 my $p2 = $a2->qualified_name;
247 Carp::confess("Atoms for different packages $p1 and $p2") unless $p1 eq $p2;
249 my $v1 = $a1->version;
250 return $a2 unless defined $v1;
251 my $r1 = $a1->range; # Defined if $v1 is defined
253 my $v2 = $a2->version;
254 return $a1 unless defined $v2;
255 my $r2 = $a2->range; # defined if $v2 is defined
257 my $o1 = $order{$r1};
258 my $o2 = $order{$r2};
260 Carp::confess("Incompatible ranges $r1$p1 and $r2$p2") if $o1 * $o2 < 0;
263 ($a1, $a2) = ($a2, $a1);
264 ($v1, $v2) = ($v2, $v1);
265 ($r1, $r2) = ($r2, $r1);
266 ($o1, $o2) = ($o2, $o1);
270 my $r = $r2 eq '=' ? '==' : $r2;
271 Carp::confess("Version mismatch $v1 $r $v2") unless eval "\$a1 $r \$a2";
274 return $a1 < $a2 ? $a2 : $a1;
276 return $a1 < $a2 ? $a1 : $a2;
280 =head2 C<fold @atoms>
282 Returns a list built from C<@atoms> but where there's only one atom for a given category and name.
287 shift unless length ref $_[0];
291 my $key = $atom->qualified_name;
293 my $cur = $seen{$key};
294 $seen{$key} = defined $cur ? $cur->and($atom) : $atom;
302 This class provides overloaded methods for numerical comparison, string comparison and strigification.
306 L<CPANPLUS::Dist::Gentoo>, L<CPANPLUS::Dist::Gentoo::Version>.
310 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
312 You can contact me by mail or on C<irc.perl.org> (vincent).
316 Please report any bugs or feature requests to C<bug-cpanplus-dist-gentoo at rt.cpan.org>, or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CPANPLUS-Dist-Gentoo>.
317 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
321 You can find documentation for this module with the perldoc command.
323 perldoc CPANPLUS::Dist::Gentoo
325 =head1 COPYRIGHT & LICENSE
327 Copyright 2009,2010 Vincent Pit, all rights reserved.
329 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
333 1; # End of CPANPLUS::Dist::Gentoo::Atom