From: Matt S Trout Date: Sat, 22 Feb 2014 05:31:55 +0000 (+0000) Subject: make exists unnecessary X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a5c3a0416ea340dfaa9d9f83fa1a8bf9065766af;p=scpubgit%2FDKit.git make exists unnecessary --- diff --git a/lib/DX/Op/CallRule.pm b/lib/DX/Op/CallRule.pm index 7702009..fb4ce1f 100644 --- a/lib/DX/Op/CallRule.pm +++ b/lib/DX/Op/CallRule.pm @@ -18,7 +18,11 @@ has full_name => (is => 'lazy', builder => sub { sub run { my ($self, $state) = @_; #warn "Call: ".$self->full_name; - my @args = map $self->_expand_argspec($state, $_), @{$self->rule_args}; + my @args; + foreach my $arg (@{$self->rule_args}) { + ($state, my $expanded) = $self->_expand_argspec($state, $arg); + push @args, $expanded; + } my @rules = @{$state->rule_set->rules->{$self->full_name}||[]}; die "No rules for ${\$self->full_name}" unless @rules; my $var = DX::Var->new(id => "rule:".$self->full_name) diff --git a/lib/DX/Role/Op.pm b/lib/DX/Role/Op.pm index 83b9e86..e4dc281 100644 --- a/lib/DX/Role/Op.pm +++ b/lib/DX/Role/Op.pm @@ -14,22 +14,26 @@ sub but { sub _expand_args { my ($self, $state, %spec) = @_; my %args; - @args{keys %spec} = map $self->_expand_argspec($state, $_), values %spec; + foreach my $key (keys %spec) { + ($state, $args{$key}) = $self->_expand_argspec($state, $spec{$key}); + } return $state->expand_vars(%args); } sub _expand_argspec { my ($self, $state, $spec) = @_; if (!ref($spec)) { - $state->scope_var($spec) + ($state->has_scope_var($spec) + ? ($state, $state->scope_var($spec)) + : (map { $_, $_->scope_var($spec) } $state->assign_vars($spec => {}))); } elsif (ref($spec) eq 'ARRAY') { if ($spec->[0] eq 'value') { - +{ bound_value => $spec->[1] }; + ($state, +{ bound_value => $spec->[1] }); } else { die "Arrayref in argspec is not value"; } } elsif (ref($spec) eq 'SCALAR' or ref($spec) eq 'REF') { - return +{ bound_value => $$spec }; + return ($state, +{ bound_value => $$spec }); } else { die "Argspec incomprehensible"; } diff --git a/lib/DX/State.pm b/lib/DX/State.pm index 899c0d9..f9d886b 100644 --- a/lib/DX/State.pm +++ b/lib/DX/State.pm @@ -27,6 +27,11 @@ has dependencies => (is => 'ro', default => sub { {} }); has actions => (is => 'ro', default => sub { {} }); +sub has_scope_var { + my ($self, $name) = @_; + return !!$self->scope->{$name}; +} + sub scope_var { my ($self, $name) = @_; my $id = $self->scope->{$name} diff --git a/t/dot_ssh.t b/t/dot_ssh.t index 1ac8b04..9619022 100644 --- a/t/dot_ssh.t +++ b/t/dot_ssh.t @@ -65,9 +65,8 @@ $solver->add_rule(@$_) for ( [ path => [ qw(PS P) ], [ prop => 'PS', \'path', 'P' ] ], [ info_prop => [ qw(PS N V) ], - [ exists => [ qw(PSI) ], - [ prop => 'PS', \'info', 'PSI' ], - [ prop => 'PSI', 'N', 'V' ] ] ], + [ prop => 'PS', \'info', 'PSI' ], + [ prop => 'PSI', 'N', 'V' ] ], [ mode => [ qw(PS M) ], [ info_prop => 'PS', \'mode', 'M' ] ], [ exists_path => [ qw(PS) ], @@ -287,11 +286,9 @@ $solver->add_predicate( $solver->add_rule(@$_) for ( [ file_in => [ qw(DirStatus FileName FileStatus) ], [ is_directory => qw(DirStatus) ], - [ exists => [ qw(DirPath) ], - [ path => qw(DirStatus DirPath) ], - [ exists => [ qw(FilePath) ], - [ catfile => qw(DirPath FileName FilePath) ], - [ file_at => qw(FileStatus FilePath) ] ] ] ], + [ path => qw(DirStatus DirPath) ], + [ catfile => qw(DirPath FileName FilePath) ], + [ file_at => qw(FileStatus FilePath) ] ], [ is_file => [ qw(PS) ], [ not => [ exists_path => 'PS' ] ], [ act => [ 'PS' ],