Hopen/G/Runnable.pm

filter: Statementstmt 96.3% 51 / 54 Branchbran 100% 10 / 10 Conditioncond 100% 8 / 8 MC/DCmcdc 100% 5 / 5 Subroutinesub 88.2% 14 / 17 Podpod 100% 2 / 2 totaltotal 95.8% 90 / 96 CC 10 SCAR 11.0
CC
10
Cov
97%
passthrough
6.0
run
5.0
__ANON__
2.0
CRAP 3.0
« Hopen/G/Node.pm ↑ Summary Hopen/Scope/Hash.pm »
1 # Data::Hopen::G::Runnable - parent class for anything runnable in a hopen graph
2 package Data::Hopen::G::Runnable;
3 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use Data::Hopen::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 14 our $VERSION = '0.000018';
7  
8 14 ▶use Data::Hopen;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 14 ▶use Data::Hopen::Scope::Hash;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 14 ▶use Data::Hopen::Util::Data qw(forward_opts);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 14 ▶use Data::Hopen::Util::NameSet;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 14 ▶use Hash::Merge;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # Docs {{{1
15  
16 =head1 NAME
17  
18 Data::Hopen::G::Runnable - parent class for runnable things in a hopen graph
19  
20 =head1 SYNOPSIS
21  
22 Anything with L</run> inherits from this. TODO should this be a role?
23  
24 =head1 ATTRIBUTES
25  
26 =head2 need
27  
28 (B<Not currently used>)
29 Inputs this Runnable requires.
30 A L<Data::Hopen::Util::NameSet>, with the restriction that C<need> may not
31 contain regexes. ("Sorry, I can't run unless you give me every variable
32 in the world that starts with Q." I don't think so!)
33 Or maybe later an arrayref? TODO.
34  
35 =head2 scope
36  
37 If defined, a L<Data::Hopen::Scope> that will have the final say on the
38 data used by L</run>. This is the basis of the fine-grained override
39 mechanism in hopen.
40  
41 =head2 want
42  
43 (B<Not currently used>)
44 Inputs this Runnable accepts but does not require.
45 A L<Data::Hopen::Util::NameSet>, which may include regexes.
46 Or maybe later an arrayref? TODO.
47  
48 =cut
49  
50 # }}}1
51  
52 14 ▶use parent 'Data::Hopen::G::Entity';
Subroutine BEGIN: called CC=1 SCAR=0.0
53 use Class::Tiny {
54 # NOTE: want and need are not currently used.
55 0 ▶ want => sub { Data::Hopen::Util::NameSet->new },
Subroutine __ANON__: not called CC=1 SCAR=6.9
56 0 ▶ need => sub { Data::Hopen::Util::NameSet->new },
Subroutine __ANON__: not called CC=1 SCAR=6.9
57  
58 58 ▶ scope => sub { Data::Hopen::Scope::Hash->new },
Subroutine __ANON__: called CC=1 SCAR=0.0
59 14 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 =head1 FUNCTIONS
62  
63 =head2 run
64  
65 Run the operation, whatever that means. Returns a new hashref.
66 Usage:
67  
68 my $hrOutputs = $op->run([options])
69  
70 Options are:
71  
72 =over
73  
74 =item -context
75  
76 A L<Data::Hopen::Scope> or subclass including the inputs the caller wants to
77 pass to the Runnable. The L</scope> of the Runnable itself may override
78 values in the C<context>.
79  
80 =item -phase
81  
82 If given, the phase that is currently under way in a build-system run.
83  
84 =item -visitor
85  
86 If given, an instance that supports C<visit_goal()> and C<visit_node()> calls.
87 A L<Data::Hopen::G::DAG> instance invokes those calls after processing each
88 goal or other node, respectively. They are invoked I<after> the goal or
89 node has run. They are, however, given access to the L<Data::Hopen::Scope>
90 that the node used for its inputs, in the C<$node_inputs> parameter. Example:
91  
92 $visitor->visit_goal($goal, $node_inputs);
93  
94 The return value from C<visit_goal()> or C<visit_node()> is ignored.
95  
96 =item -nocontext
97  
98 If C<< -nocontext=>1 >> is specified, don't link a context scope into
99 this one. May not be specified together with C<-context>.
100  
101 =back
102  
103 See the source for this function, which contains as an example of setting the
104 scope.
105  
106 =cut
107  
108 sub run {
109 114 ▶ my ($self, %args) = getparameters('self', [qw(; context phase visitor nocontext)], @_);
Subroutine run: called CC=5 SCAR=16.1
Pod documented
110 114 my $context_scope = $args{context}; # which may be undef - that's OK
111 114 ▶ croak "Can't combine -context and -nocontext" if $args{context} && $args{nocontext};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"context"} and $args{"nocontext"} 1 113 114
Condition100%3/3
$args{"context"} and $args{"nocontext"}
!ll&&!rl&&r
111021
MC/DC100%2/2
$args{"context"} and $args{"nocontext"}
$args{"context"} $args{"nocontext"}
Truth table100%3/3
A and B
A= $args{"context"}B= $args{"nocontext"}
ABresult
0X0
100
111
112  
113 # Link the outer scope to our scope
114 113 ▶ my $saver = $args{nocontext} ? undef : $self->scope->outerize($context_scope);
Branch: 1 branch
BranchTrueFalseTotal
$args{'nocontext'} ? : 1 112 113
115  
116 113 ▶ hlog { '->', ref($self), $self->name, 'input', Dumper($self->scope->as_hashref) } 3;
Subroutine __ANON__: called CC=1 SCAR=0.0
117  
118 113 my $retval = $self->_run(forward_opts(\%args, {'-'=>1}, qw[phase visitor]));
119  
120 105 ▶ die "$self\->_run() did not return a hashref" unless ref $retval eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $retval eq "HASH" 1 104 105
121 # Prevent errors about `non-hashref 1` or `invalid key`.
122  
123 104 ▶ hlog { '<-', ref $self, $self->name, 'output', Dumper($retval) } 3;
Subroutine __ANON__: called CC=1 SCAR=0.0
124  
125 104 return $retval;
126 } #run()
127  
128 =head2 _run
129  
130 The internal method that implements L</run>. Must be implemented by
131 subclasses. When C<_run> is called, C<< $self->scope >> has been hooked
132 to the context scope, if any.
133  
134 Parameters are C<-phase> and C<-visitor>, and are always passed by name
135 (C<< -phase=>$p, -visitor=>$v >>). C<_run> is always called in scalar context,
136 and B<must> return a new hashref.
137  
138 I recommend starting your C<_run> function with:
139  
140 my ($self, %args) = getparameters('self', [qw(; phase visitor)], @_);
141  
142 and working from there.
143  
144 =cut
145  
146 sub _run {
147 # uncoverable subroutine
148 0 ▶ die('Unimplemented'); # uncoverable statement
Subroutine _run: not called CC=1 SCAR=0.0
149 }
150  
151 =head2 passthrough
152  
153 Returns a new hashref of this Runnable's local values, as defined
154 by L<Data::Hopen::Scope/local>. Usage:
155  
156 my $hashref = $runnable->passthrough([-context => $outer_scope]);
157 # To use $outer_scope as the context
158 my $hashref = $runnable->passthrough(-nocontext => 1);
159 # To ignore the context
160  
161 Other valid options include L<-levels|Data::Hopen::Scope/$levels>.
162  
163 =cut
164  
165 sub passthrough {
166 57 ▶ my ($self, %args) = getparameters('self', ['*'], @_);
Subroutine passthrough: called CC=6 SCAR=17.9
Pod documented
167 57 my $outer_scope = $args{context}; # which may be undef - that's OK
168 57 ▶ croak "Can't combine -context and -nocontext" if $args{context} && $args{nocontext};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"context"} and $args{"nocontext"} 1 56 57
Condition100%3/3
$args{"context"} and $args{"nocontext"}
!ll&&!rl&&r
5511
MC/DC100%2/2
$args{"context"} and $args{"nocontext"}
$args{"context"} $args{"nocontext"}
Truth table100%3/3
A and B
A= $args{"context"}B= $args{"nocontext"}
ABresult
0X0
100
111
169  
170 # Link the outer scope to our scope
171 56 ▶ my $saver = $args{nocontext} ? undef : $self->scope->outerize($outer_scope);
Branch: 1 branch
BranchTrueFalseTotal
$args{'nocontext'} ? : 55 1 56
172  
173 # Copy the names
174 56 ▶ my $levels = $args{levels} // 'local';
Condition100%2/2
$args{"levels"} // "local"
l!l
533
MC/DC100%1/1
$args{"levels"} // "local"
$args{"levels"}
Truth table100%2/2
A
A= $args{"levels"}
Aresult
11
00
175 56 my @names = @{$self->scope->names(-levels=>$levels)};
176 56 my $retval = {};
177 56 $retval->{$_} = $self->scope->find($_, -levels=>$levels) foreach @names;
178  
179 56 return $retval;
180 } #passthrough()
181  
182 14 1;
183 __END__
« Hopen/G/Node.pm ↑ Summary Hopen/Scope/Hash.pm »