Skip to content

Commit

Permalink
structured_requirements_for_module
Browse files Browse the repository at this point in the history
  • Loading branch information
rjbs committed Apr 18, 2015
1 parent 7e95586 commit b84f27d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
31 changes: 31 additions & 0 deletions lib/CPAN/Meta/Requirements.pm
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,24 @@ sub requirements_for_module {
return $entry->as_string;
}

=method structured_requirements_for_module
$req->structured_requirements_for_module( $module );
This returns a data structure containing the version requirements for a given
module or undef if the given module has no requirements. This should only be
used for informational purposes such as error messages and should not be
interpreted or used for comparison (see L</accepts_module> instead.)
=cut

sub structured_requirements_for_module {
my ($self, $module) = @_;
my $entry = $self->__entry_for($module);
return unless $entry;
return $entry->as_struct;
}

=method required_modules
This method returns a list of all the modules for which requirements have been
Expand Down Expand Up @@ -589,6 +607,8 @@ sub from_string_hash {

sub as_string { return "== $_[0]{version}" }

sub as_struct { return [ [ '==', "$_[0]{version}" ] ] }

sub as_modifiers { return [ [ exact_version => $_[0]{version} ] ] }

sub _clone {
Expand Down Expand Up @@ -687,6 +707,17 @@ sub from_string_hash {
return join q{, }, @parts;
}

sub as_struct {
my ($self) = @_;

return [
(exists $self->{maximum} ? [ '<=', "$self->{maximum}" ] : ()),
(exists $self->{minimum} ? [ '>=', "$self->{minimum}" ] : ()),
($self->{exclusions}
? (map {; [ '!=', "$_" ] } @{$self->{exclusions}}) : ())
]
}

sub with_exact_version {
my ($self, $version) = @_;
$self = $self->_clone;
Expand Down
18 changes: 18 additions & 0 deletions t/basic.t
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,18 @@ sub dies_ok (&@) {
},
'test exclusion-skipping',
);

is_deeply(
$req->structured_requirements_for_module('Foo'),
# remember, it's okay to change the exact results, as long as the meaning
# is unchanged -- rjbs, 2012-07-11
[
[ '<=', '3' ],
[ '>=', '1' ],
[ '!=', '2' ],
],
"structured requirements for Foo",
);
}

sub foo_1 {
Expand Down Expand Up @@ -226,6 +238,12 @@ sub foo_1 {

is($req->requirements_for_module('Foo'), '== 1', 'requirements_for_module');

is_deeply(
$req->structured_requirements_for_module('Foo'),
[ [ '==', '1' ] ],
'structured_requirements_for_module'
);

# test empty/undef returns
my @list = $req->requirements_for_module('FooBarBamBaz');
my $scalar = $req->requirements_for_module('FooBarBamBaz');
Expand Down

0 comments on commit b84f27d

Please sign in to comment.