Algorithm-Dependency

 view release on metacpan or  search on metacpan

lib/Algorithm/Dependency.pm  view on Meta::CPAN

#pod =cut

use 5.005;
use strict;
use Params::Util qw{_INSTANCE _ARRAY};
use Algorithm::Dependency::Item   ();
use Algorithm::Dependency::Source ();

our $VERSION = '1.112';


#####################################################################
# Constructor

#pod =pod
#pod
#pod =head2 new %args
#pod
#pod The constructor creates a new context object for the dependency algorithms to
#pod act in. It takes as argument a series of options for creating the object.
#pod
#pod =over 4
#pod
#pod =item source => $Source
#pod
#pod The only compulsory option is the source of the dependency items. This is
#pod an object of a subclass of L<Algorithm::Dependency::Source>. In practical terms,
#pod this means you will create the source object before creating the
#pod Algorithm::Dependency object.
#pod
#pod =item selected => [ 'A', 'B', 'C', etc... ]
#pod
#pod The C<selected> option provides a list of those items that have already been
#pod 'selected', acted upon, installed, or whatever. If another item depends on one
#pod in this list, we don't have to include it in the output of the C<schedule> or
#pod C<depends> methods.
#pod
#pod =item ignore_orphans => 1
#pod
#pod Normally, the item source is expected to be largely perfect and error free.
#pod An 'orphan' is an item name that appears as a dependency of another item, but
#pod doesn't exist, or has been deleted.
#pod
#pod By providing the C<ignore_orphans> flag, orphans are simply ignored. Without
#pod the C<ignore_orphans> flag, an error will be returned if an orphan is found.
#pod
#pod =back
#pod
#pod The C<new> constructor returns a new Algorithm::Dependency object on success,
#pod or C<undef> on error.
#pod
#pod =cut

sub new {
	my $class  = shift;
	my %args   = @_;
	my $source = _INSTANCE($args{source}, 'Algorithm::Dependency::Source')
		or return undef;

	# Create the object
	my $self = bless {
		source   => $source, # Source object
		selected => {},
		}, $class;

	# Were we given the 'ignore_orphans' flag?
	if ( $args{ignore_orphans} ) {
		$self->{ignore_orphans} = 1;
	}

	# Done, unless we have been given some selected items
	_ARRAY($args{selected}) or return $self;

	# Make sure each of the selected ids exists
	my %selected = ();
	foreach my $id ( @{ $args{selected} } ) {
		# Does the item exist?
		return undef unless $source->item($id);

		# Is it a duplicate
		return undef if $selected{$id};

		# Add to the selected index
		$selected{$id} = 1;
	}

	$self->{selected} = \%selected;
	$self;
}





#####################################################################
# Basic methods

#pod =pod
#pod
#pod =head2 source
#pod
#pod The C<source> method retrieves the L<Algorithm::Dependency::Source> object
#pod for the algorithm context.
#pod
#pod =cut

sub source { $_[0]->{source} }

#pod =pod
#pod
#pod =head2 selected_list
#pod
#pod The C<selected_list> method returns, as a list and in alphabetical order,
#pod the list of the names of the selected items.
#pod
#pod =cut

sub selected_list { sort keys %{$_[0]->{selected}} }

#pod =pod
#pod



( run in 1.028 second using v1.01-cache-2.11-cpan-e93a5daba3e )