Skip to content

Commit

Permalink
Split out G::D::Dependencies from G::D::Node
Browse files Browse the repository at this point in the history
  • Loading branch information
Leont committed Apr 30, 2011
1 parent 6272ee0 commit 9257f71
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 32 deletions.
4 changes: 2 additions & 2 deletions lib/Graph/Dependency.pm
Expand Up @@ -54,7 +54,7 @@ sub _node_sorter {
return if $seen->{$current}++;
Carp::croak("$current has a circular dependency, aborting!\n") if $loop{$current};
my $node = $self->get_node($current) or Carp::croak("Node $current doesn't exist");
$self->_node_sorter($_, $list, $seen, %loop, $current => 1) for $node->all_dependencies;
$self->_node_sorter($_, $list, $seen, %loop, $current => 1) for $node->dependencies->all;
push @{$list}, $current;
return;
}
Expand All @@ -76,7 +76,7 @@ sub run {
next if $seen_phony{$node_name}++;
}
else {
my @files = grep { !$self->get_node($_)->phony } $node->all_dependencies;
my @files = grep { !$self->get_node($_)->phony } $node->dependencies->all;
next if -e $node_name and not grep { $newer->($node_name, $_) } @files;
}
my $action = $self->get_action($node->action) or Carp::croak("Action ${ \$node->action } doesn't exist");
Expand Down
46 changes: 46 additions & 0 deletions lib/Graph/Dependency/Dependencies.pm
@@ -0,0 +1,46 @@
package Graph::Dependency::Dependencies;
use Any::Moose;
use Any::Moose 'Util::TypeConstraints';

coerce 'Graph::Dependency::Dependencies', from 'HashRef[Str]', via { Graph::Dependency::Dependencies->new(dependencies => $_) };

has dependencies => (
isa => 'HashRef[Str]',
default => sub { {} },
init_arg => 'dependencies',
traits => ['Hash'],
handles => {
all => 'keys',
_kv => 'kv',
has => 'count',
get => 'get',
set => 'set',
delete => 'delete',
_flat => 'elements',
_types => 'values',
},
);

sub types {
my $self = shift;
return List::MoreUtils::uniq($self->_types);
}

sub for_type {
my ($self, $wanted_type) = @_;
my @ret;
for my $pair ($self->_kv) {
my ($name, $type) = @{$pair};
push @ret, $name if $type eq $wanted_type;
}
return @ret;
}

sub to_hashref {
my $self = shift;
return { $self->_flat_elements };
}

1;

__END__
36 changes: 6 additions & 30 deletions lib/Graph/Dependency/Node.pm
Expand Up @@ -17,37 +17,13 @@ has phony => (
required => 1,
);

has _dependencies => (
isa => 'HashRef[Str]',
default => sub { {} },
init_arg => 'dependencies',
traits => ['Hash'],
handles => {
all_dependencies => 'keys',
_kv_dependencies => 'kv',
has_dependencies => 'count',
get_dependency => 'get',
set_dependency => 'set',
delete_dependency => 'delete',
_flat_dependencies => 'elements',
},
has dependencies => (
is => 'ro',
isa => 'Graph::Dependency::Dependencies',
coerce => 1,
default => sub { Graph::Dependency::Dependencies->new },
);

sub dependency_types {
my $self = shift;
return List::MoreUtils::uniq($self->_dependency_types);
}

sub dependencies_for_type {
my ($self, $wanted_type) = @_;
my @ret;
for my $pair ($self->_kv_dependencies) {
my ($name, $type) = @{$pair};
push @ret, $name if $type eq $wanted_type;
}
return @ret;
}

has action => (
is => 'rw',
isa => 'Str',
Expand All @@ -71,7 +47,7 @@ sub to_hashref {
return {
name => $self->name,
phony => $self->phony,
dependencies => { $self->_flat_dependencies },
dependencies => $self->dependencies->to_hashref,
action => $self->action,
arguments => { $self->_arguments },
};
Expand Down

0 comments on commit 9257f71

Please sign in to comment.