Hopen/G/GraphBuilder.pm untested

filter: Statementstmt 0.0% 0 / 58 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 26 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 132 CC 8 SCAR 27.9
CC
8
Cov
0%
_wrapper
42.0
default_goal
6.0
goal
6.0
CRAP 16.2
1 # Build::Hopen::G::GraphBuilder - fluent interface for building graphs
2 package Build::Hopen::G::GraphBuilder;
3 use Build::Hopen;
4 use Build::Hopen::Base;
5 use Exporter 'import';
6  
7 our @EXPORT; BEGIN { @EXPORT=qw(make_GraphBuilder); }
8  
9 our $VERSION = '0.000008'; # TRIAL
10  
11 use Class::Tiny {
12 name => sub { 'ANON' }, # Name is optional; it's here so the
13 # constructor won't croak if you use one.
14  
15 dag => undef,
16 node => undef,
17 };
18  
19 use Class::Method::Modifiers qw(install_modifier);
20  
21 # Docs {{{1
22  
23 =head1 NAME
24  
25 Build::Hopen::G::GraphBuilder - fluent interface for building graphs
26  
27 =head1 SYNOPSIS
28  
29 A GraphBuilder wraps a L<Build::Hopen::G::DAG> and a current
30 L<Build::Hopen::G::Node>. It permits building chains of nodes in a
31 fluent way. For example, in a hopen file:
32  
33 # $Build is a Build::Hopen::G::DAG
34 use language 'C';
35  
36 my $builder = $Build->C::compile(file => 'foo.c');
37 # Now $builder holds $Build (the DAG) and a node created by
38 # C::compile().
39  
40 =head1 ATTRIBUTES
41  
42 =head2 name
43  
44 An optional name, in case you want to identify your Builder instances.
45  
46 =head2 dag
47  
48 The current L<Build::Hopen::G::DAG> instance, if any.
49  
50 =head2 node
51  
52 The current L<Build::Hopen::G::Node> instance, if any.
53  
54 =head1 INSTANCE FUNCTIONS
55  
56 =cut
57  
58 # }}}1
59  
60 =head2 add
61  
62 Adds a node to the graph. Returns the node. Note that this B<does not>
63 change the builder's current node (L</node>).
64  
65 =cut
66  
67 sub add {
68 my $self = shift or croak 'Need an instance';
69 my $node = shift or croak 'Need a node';
70 $self->dag->add($node);
71 return $node;
72 } #add()
73  
74 =head2 default_goal
75  
76 Links the most recent node in the chain to the default goal in the DAG.
77 If the DAG does not have a default goal, adds one called "all".
78 Clears the builder's record of the current node and returns undef.
79  
80 =cut
81  
82 sub default_goal {
83 my $self = shift or croak 'Need an instance';
84 croak "Need a node to link to the goal" unless $self->node;
85  
86 my $goal = $self->dag->default_goal // $self->dag->goal('all');
87 $self->dag->add($self->node); # no harm in it - DAG::add() is idempotent
88 $self->dag->connect($self->node, $goal);
89  
90 $self->node(undef); # Less likely to leak state between goals.
91  
92 return undef;
93 # Return undef because, if this is the last thing in a hopen file,
94 # whatever it returns gets recorded in MY.hopen.pl. Therefore,
95 # return $self causes a copy of the whole graph to be dropped into
96 # MY.hopen.pl, which is a Bad Thing.
97 } #default_goal()
98  
99 =head2 goal
100  
101 Links the most recent node in the chain to the given goal in the DAG.
102 Clears the builder's record of the current node and returns undef.
103  
104 =cut
105  
106 sub goal {
107 my $self = shift or croak 'Need an instance';
108 my $goal_name = shift or croak 'Need a goal name';
109 croak "Need a node to link to the goal" unless $self->node;
110  
111 my $goal = $self->dag->goal($goal_name);
112 $self->dag->add($self->node); # no harm in it - DAG::add() is idempotent
113 $self->dag->connect($self->node, $goal);
114  
115 $self->node(undef); # Less likely to leak state between goals.
116  
117 return undef; # undef: See comment in goal()
118 } #goal()
119  
120 =head1 STATIC FUNCTIONS
121  
122 =head2 make_GraphBuilder
123  
124 Given the name of a subroutine, wrap the given subroutine for use in a
125 GraphBuilder chain such as that shown in the L</SYNOPSIS>. Usage:
126  
127 sub worker {
128 my $graphbuilder = shift;
129 ...
130 return $node; # Will automatically be linked into the chain
131 }
132  
133 make_GraphBuilder 'worker';
134 # now worker can take a DAG or GraphBuilder, and the
135 # return value will be the GraphBuilder.
136  
137 The C<worker> subroutine is called in scalar context.
138  
139 =cut
140  
141 sub _wrapper;
142  
143 sub make_GraphBuilder {
144 my $target = caller;
145 my $funcname = shift or croak 'Need the name of the sub to wrap'; # yum
146  
147 install_modifier $target, 'around', $funcname, \&_wrapper;
148 } #make_GraphBuilder()
149  
150 # The "around" modifier
151 sub _wrapper {
152 my $orig = shift or die 'Need a function to wrap';
153 croak "Need a parameter" unless @_;
154  
155 # Create the GraphBuilder if we don't have one already.
156 my $self = shift;
157 $self = __PACKAGE__->new(dag=>$self)
158 unless ref $self and eval { $self->DOES(__PACKAGE__) };
159 croak "Parameter must be a DAG or Builder"
160 unless $self->dag and eval { $self->dag->DOES('Build::Hopen::G::DAG') };
161  
162 unshift @_, $self; # Put the builder on the arg list
163  
164 # Call the worker
165 my $worker_retval = &{$orig}; # @_ passed to code
166  
167 # If we got a node, remember it.
168 if(ref $worker_retval && eval { $worker_retval->DOES('Build::Hopen::G::Node') } ) {
169 $self->dag->add($worker_retval); # Link it into the graph
170 $self->dag->connect($self->node, $worker_retval) if $self->node;
171  
172 $self->node($worker_retval); # It's now our current node
173 }
174  
175 return $self;
176 }; #_wrapper()
177  
178 1;
179 __END__