]> git.vpit.fr Git - perl/modules/CPANPLUS-Dist-Gentoo.git/blobdiff - lib/CPANPLUS/Dist/Gentoo/Atom.pm
Use >= as the default atom range
[perl/modules/CPANPLUS-Dist-Gentoo.git] / lib / CPANPLUS / Dist / Gentoo / Atom.pm
index 002b349be48a31dc5ff532f80760a49bf5456df6..bbb6d882ff6df607c05fb28b4d1e04ad11bd11cf 100644 (file)
@@ -3,7 +3,23 @@ package CPANPLUS::Dist::Gentoo::Atom;
 use strict;
 use warnings;
 
-our $VERSION = '0.08';
+=head1 NAME
+
+CPANPLUS::Dist::Gentoo::Atom - Gentoo atom object.
+
+=head1 VERSION
+
+Version 0.10
+
+=cut
+
+our $VERSION = '0.10';
+
+=head1 DESCRIPTION
+
+This class models Gentoo atoms.
+
+=cut
 
 use Carp         ();
 use Scalar::Util ();
@@ -18,27 +34,30 @@ use CPANPLUS::Dist::Gentoo::Version;
 
 my $version_rx = $CPANPLUS::Dist::Gentoo::Version::version_rx;
 
+=head1 METHODS
+
+=head2 C<< new category => $category, name => $name [, version => $version, range => $range, ebuild => $ebuild ] >>
+
+Creates a new L<CPANPLUS::Dist::Gentoo::Atom> object from the supplied C<$category>, C<$name>, C<$version>, C<$range> and C<$ebuild>.
+
+=cut
+
 sub new {
  my $class = shift;
  $class = ref($class) || $class;
 
  my %args = @_;
 
- my ($category, $name, $version);
+ my ($range, $category, $name, $version);
  if (defined $args{name}) {
-  ($category, $name, $version) = @args{qw/category name version/};
+  ($range, $category, $name, $version) = @args{qw/range category name version/};
   Carp::confess('Category unspecified') unless defined $category;
   /[^\w-]/ and Carp::confess('Invalid argument') for $name, $category;
  } elsif (defined $args{atom}) {
   my $atom = $args{atom};
-  $atom =~ m{^([\w-]+)/([\w-]+)-v?($version_rx)$}
+  $atom =~ m{^(<|<=|=|>=|>)?([\w-]+)/([\w-]+)(?:-v?($version_rx))?$}
                                                or Carp::confess('Invalid atom');
-  ($category, $name, $version) = ($1, $2, $3);
- } elsif (defined $args{ebuild}) {
-  my $ebuild = $args{ebuild};
-  $ebuild =~ m{/([\w-]+)/([\w-]+)/\2-v?($version_rx)\.ebuild$}
-                                             or Carp::confess('Invalid ebuild');
-  ($category, $name, $version) = ($1, $2, $3);
+  ($range, $category, $name, $version) = ($1, $2, $3, $4);
  } else {
   Carp::confess('Not enough information for building an atom object');
  }
@@ -50,13 +69,12 @@ sub new {
   }
  }
 
- my $range = $args{range};
  if (defined $version) {
   if (defined $range) {
    Carp::confess("Invalid range $range")
                                       unless grep $range eq $_, qw|< <= = >= >|;
   } else {
-   $range = '=';
+   $range = '>=';
   }
  } else {
   Carp::confess('Range atoms require a valid version')
@@ -72,10 +90,61 @@ sub new {
  }, $class;
 }
 
+=head2 C<new_from_ebuild $ebuild>
+
+Creates a new L<CPANPLUS::Dist::Gentoo::Atom> object by inferring the category, name and version from the given C<$ebuild>
+
+=cut
+
+sub new_from_ebuild {
+ my $class = shift;
+ $class = ref($class) || $class;
+
+ my $ebuild = shift;
+ $ebuild = '' unless defined $ebuild;
+
+ $ebuild =~ m{/([\w-]+)/([\w-]+)/\2-v?($version_rx)\.ebuild$}
+                                             or Carp::confess('Invalid ebuild');
+ my ($category, $name, $version) = ($1, $2, $3);
+
+ return $class->new(
+  category => $category,
+  name     => $name,
+  version  => $version,
+  ebuild   => $ebuild,
+ );
+}
+
 BEGIN {
  eval "sub $_ { \$_[0]->{$_} }" for qw/category name version range ebuild/;
 }
 
+=head2 C<category>
+
+Read-only accessor to the atom category.
+
+=head2 C<name>
+
+Read-only accessor to the atom name.
+
+=head2 C<version>
+
+Read-only accessor to the L<CPANPLUS::Dist::Gentoo::Version> object associated with the atom.
+
+=head2 C<range>
+
+Read-only accessor to the atom range.
+
+=head2 C<ebuild>
+
+Read-only accessor to the path of an optional ebuild associated with the atom.
+
+=head2 C<qualified_name>
+
+Returns the qualified name for the atom, i.e. C<$category/$name>.
+
+=cut
+
 sub qualified_name { join '/', $_[0]->category, $_[0]->name }
 
 sub _spaceship {
@@ -84,16 +153,36 @@ sub _spaceship {
  my $v1 = $a1->version;
 
  my $v2;
- if (Scalar::Util::blessed($a2) and $a2->isa(__PACKAGE__)) {
-  Carp::confess('Can\'t compare atoms of different packages')
-                    if $a1->category ne $a2->category or $a1->name ne $a2->name;
+ my $blessed = Scalar::Util::blessed($a2);
+ unless ($blessed and $a2->isa(__PACKAGE__)) {
+  if ($blessed and $a2->isa('CPANPLUS::Dist::Gentoo::Version')) {
+   $v2 = $a2;
+   $a2 = undef;
+  } else {
+   my $maybe_atom = eval { __PACKAGE__->new(atom => $a2) };
+   if (my $err = $@) {
+    $v2 = eval { CPANPLUS::Dist::Gentoo::Version->new($a2) };
+    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 $@;
+    $a2 = undef;
+   } else {
+    $a2 = $maybe_atom;
+   }
+  }
+ }
+
+ if (defined $a2) {
   $v2 = $a2->version;
- } else {
-  $v2 = $a2;
+
+  my $p1 = $a1->qualified_name;
+  my $p2 = $a2->qualified_name;
+  Carp::confess("Atoms for different packages $p1 and $p2") unless $p1 eq $p2;
  }
 
  ($v1, $v2) = ($v2, $v1) if $r;
 
+ return (defined $v1 or 0) <=> (defined $v2 or 0) unless defined $v1
+                                                     and defined $v2;
+
  return $v1 <=> $v2;
 }
 
@@ -135,10 +224,18 @@ my %order = (
  '>'  =>  2,
 );
 
+=head2 C<and @atoms>
+
+Compute the ranged atom representing the logical AND between C<@atoms> with the same category and name.
+
+=cut
+
 sub and {
  shift unless length ref $_[0];
 
  my $a1 = shift;
+ return $a1 unless @_;
+
  my $a2 = shift;
  $a2 = $a2->and(@_) if @_;
 
@@ -177,6 +274,12 @@ sub and {
  }
 }
 
+=head2 C<fold @atoms>
+
+Returns a list built from C<@atoms> but where there's only one atom for a given category and name.
+
+=cut
+
 sub fold {
  shift unless length ref $_[0];
 
@@ -191,4 +294,37 @@ sub fold {
  return values %seen;
 }
 
-1;
+=pod
+
+This class provides overloaded methods for numerical comparison, string comparison and strigification.
+
+=head1 SEE ALSO
+
+L<CPANPLUS::Dist::Gentoo>, L<CPANPLUS::Dist::Gentoo::Version>.
+
+=head1 AUTHOR
+
+Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
+
+You can contact me by mail or on C<irc.perl.org> (vincent).
+
+=head1 BUGS
+
+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>.
+I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
+
+=head1 SUPPORT
+
+You can find documentation for this module with the perldoc command.
+
+    perldoc CPANPLUS::Dist::Gentoo
+
+=head1 COPYRIGHT & LICENSE
+
+Copyright 2009,2010 Vincent Pit, all rights reserved.
+
+This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
+
+=cut
+
+1; # End of CPANPLUS::Dist::Gentoo::Atom