Alzabo/MethodMaker.pm

filter: Statementstmt 9.8% 46 / 468 Branchbran 0.0% 0 / 194 Conditioncond 0.0% 0 / 37 MC/DCmcdc 0.0% 0 / 23 Subroutinesub 18.3% 13 / 71 Podpod 0.0% 0 / 19 totaltotal 7.2% 59 / 812 CC 127 SCAR 36.5
CC
127
Cov
7%
name
110.0
new
90.0
make_foreign_key_methods
90.0
CRAP 38.4
« Alzabo/Runtime/RowState/Potential.pm ↑ Summary Alzabo/BackCompat.pm »
1 package Alzabo::MethodMaker;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Alzabo::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Alzabo::Runtime;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Alzabo::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Params::Validate qw( :all );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 0 ▶Params::Validate::validation_options( on_fail => sub { Alzabo::Exception::Params->throw( error => join '', @_ ) } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
12  
13 1 $VERSION = 2.0;
14  
15 # types of methods that can be made - only ones that haven't been
16 # deprecated
17 1 my @options = qw( foreign_keys
18 linking_tables
19 lookup_columns
20 row_columns
21 self_relations
22  
23 tables
24 table_columns
25  
26 insert_hooks
27 update_hooks
28 select_hooks
29 delete_hooks
30 );
31  
32 sub import
33 0 ▶{
Subroutine import: not called CC=4 SCAR=30.0
34 0 my $class = shift;
35  
36 validate( @_, { schema => { type => SCALAR },
37 class_root => { type => SCALAR,
38 optional => 1 },
39 name_maker => { type => CODEREF,
40 optional => 1 },
41 0 ( map { $_ => { optional => 1 } } 'all', @options ) } );
42 0 my %p = @_;
43  
44 0 ▶ return unless exists $p{schema};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $p{"schema"} 0 0 0
45 0 ▶ return unless grep { exists $p{$_} && $p{$_} } 'all', @options;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {exists $p{$_} and $p{$_};} "all", @options 0 0 0
Condition0%0/3
exists $p{$_} && $p{$_}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $p{$_} && $p{$_}
exists $p{$_} $p{$_}
Truth table0%0/3
A && B
A= exists $p{$_}B= $p{$_}
ABresult
0X0
100
111
46  
47 0 my $maker = $class->new(%p);
48  
49 0 $maker->make;
50 }
51  
52 sub new
53 0 ▶{
Subroutine new: not called CC=9 SCAR=45.0
Pod undocumented
54 0 my $class = shift;
55 0 my %p = @_;
56  
57 0 ▶ if ( delete $p{all} )
Branch: 1 branch
BranchTrueFalseTotal
if (delete $p{"all"}) 0 0 0
58 {
59 0 foreach (@options)
60 {
61 0 ▶ $p{$_} = 1 unless exists $p{$_} && ! $p{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $p{$_} and not $p{$_} 0 0 0
Condition0%0/3
exists $p{$_} and not $p{$_}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $p{$_} and not $p{$_}
exists $p{$_} not $p{$_}
Truth table0%0/3
A and not(B)
A= exists $p{$_}B= not $p{$_}
ABresult
0X0
100
111
62 }
63 }
64  
65 0 my $s = Alzabo::Runtime::Schema->load_from_file( name => delete $p{schema} );
66  
67 0 my $class_root;
68 0 ▶ if ( $p{class_root} )
Branch: 1 branch
BranchTrueFalseTotal
if ($p{'class_root'}) { } 0 0 0
69 {
70 0 $class_root = $p{class_root};
71 }
72 else
73 {
74 0 my $x = 0;
75 do
76 0 ▶ {
Branch: 1 branch
BranchTrueFalseTotal
$class_root->isa(__PACKAGE__) and do { do { $class_root = caller $x++; die "No base class could be determined\n" unless $class_root } } 0 0 0
77 0 $class_root = caller($x++);
78 0 ▶ die "No base class could be determined\n" unless $class_root;
Branch: 1 branch
BranchTrueFalseTotal
unless $class_root 0 0 0
79 } while ( $class_root->isa(__PACKAGE__) );
80 }
81  
82 0 my $self;
83  
84 0 ▶ $p{name_maker} = sub { $self->name(@_) } unless ref $p{name_maker};
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless ref $p{"name_maker"} 0 0 0
85  
86 0 $self = bless { opts => \%p,
87 class_root => $class_root,
88 schema => $s,
89 }, $class;
90  
91 0 return $self;
92 }
93  
94 sub make
95 0 ▶{
Subroutine make: not called CC=9 SCAR=43.2
Pod undocumented
96 0 my $self = shift;
97  
98 0 $self->{schema_class} = join '::', $self->{class_root}, 'Schema';
99 0 bless $self->{schema}, $self->{schema_class};
100  
101 0 $self->eval_schema_class;
102 0 $self->load_class( $self->{schema_class} );
103  
104 {
105 # Users can add methods to these superclasses
106 0 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.2
107 0 foreach my $thing ( qw( Table Row ) )
108 {
109 0 @{ "$self->{class_root}::${thing}::ISA" }
110 = ( "Alzabo::Runtime::$thing", "Alzabo::DocumentationContainer" );
111 }
112 }
113  
114 0 foreach my $t ( sort { $a->name cmp $b->name } $self->{schema}->tables )
115 {
116 0 $self->{table_class} = join '::', $self->{class_root}, 'Table', $t->name;
117 0 $self->{row_class} = join '::', $self->{class_root}, 'Row', $t->name;
118  
119 0 bless $t, $self->{table_class};
120 0 $self->eval_table_class;
121 0 $self->{schema}->add_contained_class( table => $self->{table_class} );
122  
123 0 $self->eval_row_class;
124 0 $t->add_contained_class( row => $self->{row_class} );
125  
126 0 ▶ if ( $self->{opts}{tables} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"opts"}{"tables"}) 0 0 0
127 {
128 0 $self->make_table_method($t);
129 }
130  
131 0 $self->load_class( $self->{table_class} );
132 0 $self->load_class( $self->{row_class} );
133  
134 0 ▶ if ( $self->{opts}{table_columns} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"opts"}{"table_columns"}) 0 0 0
135 {
136 0 $self->make_table_column_methods($t);
137 }
138  
139 0 ▶ if ( $self->{opts}{row_columns} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"opts"}{"row_columns"}) 0 0 0
140 {
141 0 $self->make_row_column_methods($t);
142 }
143 0 ▶ if ( grep { $self->{opts}{$_} } qw( foreign_keys linking_tables lookup_columns ) )
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$self->{"opts"}{$_};} "foreign_keys", "linking_tables", "lookup_columns") 0 0 0
144 {
145 0 $self->make_foreign_key_methods($t);
146 }
147  
148 0 foreach ( qw( insert update select delete ) )
149 {
150 0 ▶ if ( $self->{opts}{"$_\_hooks"} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"opts"}{"${_}_hooks"}) 0 0 0
151 {
152 0 $self->make_hooks($t, $_);
153 }
154 }
155 }
156 }
157  
158 sub eval_schema_class
159 0 ▶{
Subroutine eval_schema_class: not called CC=2 SCAR=17.9
Pod undocumented
160 0 my $self = shift;
161  
162 0 eval <<"EOF";
163 package $self->{schema_class};
164  
165 use base qw( Alzabo::Runtime::Schema Alzabo::DocumentationContainer );
166 EOF
167  
168 0 ▶ Alzabo::Exception::Eval->throw( error => $@ ) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
169 }
170  
171 sub eval_table_class
172 0 ▶{
Subroutine eval_table_class: not called CC=2 SCAR=17.9
Pod undocumented
173 0 my $self = shift;
174  
175 0 eval <<"EOF";
176 package $self->{table_class};
177  
178 use base qw( $self->{class_root}::Table );
179  
180 sub _row_class { '$self->{row_class}' }
181  
182 EOF
183  
184 0 ▶ Alzabo::Exception::Eval->throw( error => $@ ) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
185 }
186  
187 sub eval_row_class
188 0 ▶{
Subroutine eval_row_class: not called CC=2 SCAR=17.9
Pod undocumented
189 0 my $self = shift;
190  
191 # Need to load this so that ->can checks can see them
192 0 require Alzabo::Runtime;
193  
194 0 eval <<"EOF";
195 package $self->{row_class};
196  
197 use base qw( $self->{class_root}::Row Alzabo::DocumentationContainer );
198  
199 EOF
200  
201 0 ▶ Alzabo::Exception::Eval->throw( error => $@ ) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
202 }
203  
204 sub make_table_method
205 0 ▶{
Subroutine make_table_method: not called CC=2 SCAR=17.9
Pod undocumented
206 0 my $self = shift;
207 0 my $t = shift;
208  
209 my $name = $self->_make_method
210 ( type => 'table',
211 class => $self->{schema_class},
212 returns => 'table object',
213 0 ▶ code => sub { return $t; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
214 0 ▶ table => $t,
Branch: 1 branch
BranchTrueFalseTotal
my $name = $self->_make_method("type", "table", "class", $self->{"schema_class"}, "returns", "table object", "code", sub { return $t; } , "table", $t) or return 0 0 0
215 ) or return;
216  
217 $self->{schema_class}->add_method_docs
218 0 ( Alzabo::MethodDocs->new
219 ( name => $name,
220 group => 'Methods that return table objects',
221 description => "returns the " . $t->name . " table object",
222 ) );
223 }
224  
225 sub load_class
226 0 ▶{
Subroutine load_class: not called CC=3 SCAR=24.8
Pod undocumented
227 0 my $self = shift;
228 0 my $class = shift;
229  
230 0 eval "use $class;";
231  
232 0 ▶ die $@ if $@ && $@ !~ /^Can\'t locate .* in \@INC/;
Branch: 1 branch
BranchTrueFalseTotal
if $@ and not $@ =~ /^Can\'t locate .* in \@INC/ 0 0 0
Condition0%0/3
$@ and not $@ =~ /^Can\'t locate .* in \@INC/
!ll&&!rl&&r
000
MC/DC0%0/2
$@ and not $@ =~ /^Can\'t locate .* in \@INC/
$@ not $@ =~ /^Can\'t locate .* in \@INC/
Truth table0%0/3
A and not(B)
A= $@B= not $@ =~ /^Can\'t locate .* in \@INC/
ABresult
0X0
100
111
233 }
234  
235 sub make_table_column_methods
236 0 ▶{
Subroutine make_table_column_methods: not called CC=3 SCAR=24.8
Pod undocumented
237 0 my $self = shift;
238 0 my $t = shift;
239  
240 0 foreach my $c ( sort { $a->name cmp $b->name } $t->columns )
241 {
242 0 my $col_name = $c->name;
243  
244 my $name = $self->_make_method
245 ( type => 'table_column',
246 class => $self->{table_class},
247 returns => 'column_object',
248  
249 # We can't just return $c because we may need to go
250 # through the alias bits. And we need to use $_[0] for
251 # the same reason.
252 0 ▶ code => sub { return $_[0]->column($col_name) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
253 0 ▶ column => $c,
Branch: 1 branch
BranchTrueFalseTotal
my $name = $self->_make_method("type", "table_column", "class", $self->{"table_class"}, "returns", "column_object", "code", sub { return $_[0]->column($col_name); } , "column", $c) or next 0 0 0
254 ) or next;
255  
256 $self->{table_class}->add_method_docs
257 0 ( Alzabo::MethodDocs->new
258 ( name => $name,
259 group => 'Methods that return column objects',
260 description => "returns the " . $c->name . " column object",
261 ) );
262 }
263 }
264  
265 sub make_row_column_methods
266 0 ▶{
Subroutine make_row_column_methods: not called CC=3 SCAR=24.8
Pod undocumented
267 0 my $self = shift;
268 0 my $t = shift;
269  
270 0 foreach my $c ( sort { $a->name cmp $b->name } $t->columns )
271 {
272 0 my $col_name = $c->name;
273  
274 my $name = $self->_make_method
275 ( type => 'row_column',
276 class => $self->{row_class},
277 returns => 'scalar value/takes new value',
278 0 ▶ code => sub { my $self = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
279 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
280 {
281 0 $self->update( $col_name => $_[0] );
282 }
283 0 return $self->select($col_name); },
284 0 ▶ column => $c,
Branch: 1 branch
BranchTrueFalseTotal
my $name = $self->_make_method("type", "row_column", "class", $self->{"row_class"}, "returns", "scalar value/takes new value", "code", sub { my $self = shift(); if (@_) { $self->update($col_name, $_[0]); } ; return $self->select($col_name); } , "column", $c) or next 0 0 0
285 ) or next;
286  
287 $self->{row_class}->add_method_docs
288 0 ( Alzabo::MethodDocs->new
289 ( name => $name,
290 group => 'Methods that update/return a column value',
291 spec => [ { type => SCALAR } ],
292 description =>
293 "returns the value of the " . $c->name . " column for a row. Given a value, it will also update the row first.",
294 ) );
295 }
296 }
297  
298 sub make_foreign_key_methods
299 0 ▶{
Subroutine make_foreign_key_methods: not called CC=9 SCAR=45.0
Pod undocumented
300 0 my $self = shift;
301 0 my $t = shift;
302  
303 0 foreach my $other_t ( sort { $a->name cmp $b->name } $t->schema->tables )
304 {
305 0 ▶ my @fk = $t->foreign_keys_by_table($other_t)
Branch: 1 branch
BranchTrueFalseTotal
my(@fk) = $t->foreign_keys_by_table($other_t) or next 0 0 0
306 or next;
307  
308 0 ▶ if ( @fk == 2 && $fk[0]->table_from eq $fk[0]->table_to &&
Branch: 1 branch
BranchTrueFalseTotal
if (@fk == 2 and $fk[0]->table_from eq $fk[0]->table_to and $fk[1]->table_from eq $fk[1]->table_to) 0 0 0
Condition0%0/6
@fk == 2 and $fk[0]->table_from eq $fk[0]->table_to and $fk[1]->table_from eq $fk[1]->table_to
!ll&&!rl&&r
000
@fk == 2 and $fk[0]->table_from eq $fk[0]->table_to
!ll&&!rl&&r
000
MC/DC0%0/3
@fk == 2 and $fk[0]->table_from eq $fk[0]->table_to and $fk[1]->table_from eq $fk[1]->table_to
@fk == 2 $fk[0]->table_from eq $fk[0]->table_to $fk[1]->table_from eq $fk[1]->table_to
Truth table0%0/4
A and B and C
A= @fk == 2B= $fk[0]->table_from eq $fk[0]->table_toC= $fk[1]->table_from eq $fk[1]->table_to
ABCresult
0XX0
10X0
1100
1111
309 $fk[1]->table_from eq $fk[1]->table_to )
310 {
311 0 ▶ unless ($fk[0]->is_one_to_one)
Branch: 1 branch
BranchTrueFalseTotal
unless ($fk[0]->is_one_to_one) 0 0 0
312 {
313 0 ▶ $self->make_self_relation($fk[0]) if $self->{opts}{self_relations};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"opts"}{"self_relations"} 0 0 0
314 }
315 0 next;
316 }
317  
318 0 foreach my $fk (@fk)
319 {
320 0 $self->_make_fk_method($fk);
321 }
322 }
323 }
324  
325 sub _make_method
326 0 ▶{
Subroutine _make_method: not called CC=4 SCAR=26.4
327 0 my $self = shift;
328 0 my %p = validate @_, { type => { type => SCALAR },
329 class => { type => SCALAR },
330 returns => { type => SCALAR, optional => 1 },
331 code => { type => CODEREF },
332  
333 # Stuff we can pass through to name_maker
334 foreign_key => { optional => 1 },
335 foreign_key_2 => { optional => 1 },
336 column => { optional => 1 },
337 table => { optional => 1 },
338 parent => { optional => 1 },
339 plural => { optional => 1 },
340 };
341  
342 0 ▶ my $name = $self->{opts}{name_maker}->( %p )
Branch: 1 branch
BranchTrueFalseTotal
my $name = $self->{"opts"}{"name_maker"}->(%p) or return 0 0 0
343 or return;
344  
345 0 my ($code_name, $debug_name) = ("$p{class}::$name",
346 "$p{class}\->$name");
347  
348 0 ▶ if ( $p{class}->can($name) )
Branch: 1 branch
BranchTrueFalseTotal
if ($p{"class"}->can($name)) 0 0 0
349 {
350 0 warn "MethodMaker: Creating $p{type} method $debug_name will override"
351 . " the method of the same name in the parent class\n";
352 }
353  
354 1 ▶ no strict 'refs'; # We use symbolic references here
Subroutine BEGIN: called CC=1 SCAR=0.0
355 0 ▶ if ( defined &$code_name )
Branch: 1 branch
BranchTrueFalseTotal
if (defined &$code_name) 0 0 0
356 {
357 # This should probably always be shown to the user, not just
358 # when debugging mode is turned on, because name clashes can
359 # cause confusion - whichever subroutine happens first will
360 # arbitrarily win.
361  
362 0 warn "MethodMaker: skipping $p{type} method $debug_name, subroutine already exists\n";
363 0 return;
364 }
365  
366 0 if (Alzabo::Debug::METHODMAKER)
367 {
368 my $message = "Making $p{type} method $debug_name";
369 $message .= ": returns $p{returns}" if $p{returns};
370 print STDERR "$message\n";
371 }
372  
373 0 *$code_name = $p{code};
374 0 return $name;
375 }
376  
377 sub _make_fk_method
378 0 ▶{
Subroutine _make_fk_method: not called CC=7 SCAR=40.3
379 0 my $self = shift;
380 0 my $fk = shift;
381 0 my $table_to = $fk->table_to->name;
382  
383 # The table may be a linking or lookup table. If we are
384 # supposed to make that kind of method we will and then we'll
385 # skip to the next foreign table.
386 $self->make_linking_table_method($fk)
387 0 ▶ if $self->{opts}{linking_tables};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"opts"}{"linking_tables"} 0 0 0
388  
389 $self->make_lookup_columns_methods($fk)
390 0 ▶ if $self->{opts}{lookup_columns};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"opts"}{"lookup_columns"} 0 0 0
391  
392 0 ▶ return unless $self->{opts}{foreign_keys};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"opts"}{"foreign_keys"} 0 0 0
393  
394 0 ▶ if ($fk->is_one_to_many)
Branch: 1 branch
BranchTrueFalseTotal
if ($fk->is_one_to_many) { } 0 0 0
395 {
396 my $name = $self->_make_method
397 ( type => 'foreign_key',
398 class => $self->{row_class},
399 returns => 'row cursor',
400 0 ▶ code => sub { my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
401 0 return $self->rows_by_foreign_key( foreign_key => $fk, @_ ); },
402 0 ▶ foreign_key => $fk,
Branch: 1 branch
BranchTrueFalseTotal
my $name = $self->_make_method("type", "foreign_key", "class", $self->{"row_class"}, "returns", "row cursor", "code", sub { my $self = shift(); return $self->rows_by_foreign_key("foreign_key", $fk, @_); } , "foreign_key", $fk, "plural", 1) or return 0 0 0
403 plural => 1,
404 ) or return;
405  
406 $self->{row_class}->add_method_docs
407 0 ( Alzabo::MethodDocs->new
408 ( name => $name,
409 group => 'Methods that return cursors for foreign keys',
410 description =>
411 "returns a cursor containing related rows from the " . $fk->table_to->name . " table",
412 spec => 'same as Alzabo::Runtime::Table->rows_where',
413 ) );
414 }
415 # Singular method name
416 else
417 {
418 my $name = $self->_make_method
419 ( type => 'foreign_key',
420 class => $self->{row_class},
421 returns => 'single row',
422 0 ▶ code => sub { my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
423 0 return $self->rows_by_foreign_key( foreign_key => $fk, @_ ); },
424 0 ▶ foreign_key => $fk,
Branch: 1 branch
BranchTrueFalseTotal
my $name = $self->_make_method("type", "foreign_key", "class", $self->{"row_class"}, "returns", "single row", "code", sub { my $self = shift(); return $self->rows_by_foreign_key("foreign_key", $fk, @_); } , "foreign_key", $fk, "plural", 0) or return 0 0 0
425 plural => 0,
426 ) or return;
427  
428 $self->{row_class}->add_method_docs
429 0 ( Alzabo::MethodDocs->new
430 ( name => $name,
431 group => 'Methods that return a single row for foreign keys',
432 description =>
433 "returns a single related row from the " . $fk->table_to->name . " table",
434 spec => 'same as Alzabo::Runtime::Table->one_row',
435 ) );
436 }
437 }
438  
439 sub make_self_relation
440 0 ▶{
Subroutine make_self_relation: not called CC=5 SCAR=34.0
Pod undocumented
441 0 my $self = shift;
442 0 my $fk = shift;
443  
444 0 my (@pairs, @reverse_pairs);
445 0 ▶ if ($fk->is_one_to_many)
Branch: 1 branch
BranchTrueFalseTotal
if ($fk->is_one_to_many) { } 0 0 0
446 {
447 0 @pairs = map { [ $_->[0], $_->[1]->name ] } $fk->column_pairs;
448 0 @reverse_pairs = map { [ $_->[1], $_->[0]->name ] } $fk->column_pairs;
449 }
450 else
451 {
452 0 @pairs = map { [ $_->[1], $_->[0]->name ] } $fk->column_pairs;
453 0 @reverse_pairs = map { [ $_->[0], $_->[1]->name ] } $fk->column_pairs;
454 }
455  
456 0 my $table = $fk->table_from;
457  
458 my $name = $self->_make_method
459 ( type => 'self_relation',
460 class => $self->{row_class},
461 returns => 'single row',
462 0 ▶ code => sub { my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
463 0 my @where = map { [ $_->[0], '=', $self->select( $_->[1] ) ] } @pairs;
464 0 return $table->one_row( where => \@where, @_ ); },
465 0 ▶ foreign_key => $fk,
Branch: 1 branch
BranchTrueFalseTotal
my $name = $self->_make_method("type", "self_relation", "class", $self->{"row_class"}, "returns", "single row", "code", sub { my $self = shift(); my(@where) = map({[$_->[0], "=", $self->select($_->[1])];} @pairs); return $table->one_row("where", \@where, @_); } , "foreign_key", $fk, "parent", 1) or last 0 0 0
466 parent => 1,
467 ) or last;
468  
469 0 ▶ if ($name)
Branch: 1 branch
BranchTrueFalseTotal
if ($name) 0 0 0
470 {
471 $self->{row_class}->add_method_docs
472 0 ( Alzabo::MethodDocs->new
473 ( name => $name,
474 group => 'Methods that return a parent row',
475 description =>
476 "a single parent row from the same table",
477 spec => 'same as Alzabo::Runtime::Table->one_row',
478 ) );
479 }
480  
481 $name = $self->_make_method
482 ( type => 'self_relation',
483 class => $self->{row_class},
484 returns => 'row cursor',
485 code =>
486 0 ▶ sub { my $self = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
487 0 my %p = @_;
488 0 my @where = map { [ $_->[0], '=', $self->select( $_->[1] ) ] } @reverse_pairs;
489 0 ▶ if ( $p{where} )
Branch: 1 branch
BranchTrueFalseTotal
if ($p{"where"}) 0 0 0
490 {
491 0 @where = ( '(', @where, ')' );
492  
493 push @where,
494 0 ▶ Alzabo::Utils::is_arrayref( $p{where}->[0] ) ? @{ $p{where} } : $p{where};
Branch: 1 branch
BranchTrueFalseTotal
Alzabo::Utils::is_arrayref($p{'where'}[0]) ? : 0 0 0
495  
496 0 delete $p{where};
497 }
498 0 return $table->rows_where( where => \@where,
499 %p ); },
500 0 ▶ foreign_key => $fk,
Branch: 1 branch
BranchTrueFalseTotal
$name = $self->_make_method("type", "self_relation", "class", $self->{"row_class"}, "returns", "row cursor", "code", sub { my $self = shift(); my(%p) = @_; my(@where) = map({[$_->[0], "=", $self->select($_->[1])];} @reverse_pairs); if ($p{"where"}) { @where = ("(", @where, ")"); push @where, Alzabo::Utils::is_arrayref($p{"where"}[0]) ? @{$p{"where"};} : $p{"where"}; delete $p{"where"}; } ; return $table->rows_where("where", \@where, %p); } , "foreign_key", $fk, "parent", 0) or return 0 0 0
501 parent => 0,
502 ) or return;
503  
504 $self->{row_class}->add_method_docs
505 0 ( Alzabo::MethodDocs->new
506 ( name => $name,
507 group => 'Methods that return child rows',
508 description =>
509 "a row cursor of child rows from the same table",
510 spec => 'same as Alzabo::Runtime::Table->rows_where',
511 ) );
512 }
513  
514 sub make_linking_table_method
515 0 ▶{
Subroutine make_linking_table_method: not called CC=8 SCAR=42.8
Pod undocumented
516 0 my $self = shift;
517 0 my $fk = shift;
518  
519 0 ▶ return unless $fk->table_to->primary_key_size == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless $fk->table_to->primary_key_size == 2 0 0 0
520  
521 # Find the foreign key from the linking table to the _other_ table
522 0 my $fk_2;
523 {
524 0 my @fk = $fk->table_to->all_foreign_keys;
525 0 ▶ return unless @fk == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @fk == 2 0 0 0
526  
527 # Get the foreign key that's not the one we already have
528 0 ▶ $fk_2 = $fk[0]->is_same_relationship_as($fk) ? $fk[1] : $fk[0];
Branch: 1 branch
BranchTrueFalseTotal
$fk[0]->is_same_relationship_as($fk) ? : 0 0 0
529 }
530  
531 0 ▶ return unless $fk_2;
Branch: 1 branch
BranchTrueFalseTotal
unless $fk_2 0 0 0
532  
533 # Not a linking table unless all the PK columns in the linking
534 # table are part of the link.
535 0 ▶ return unless $fk->table_to->primary_key_size == $fk->table_to->columns;
Branch: 1 branch
BranchTrueFalseTotal
unless $fk->table_to->primary_key_size == $fk->table_to->columns 0 0 0
536  
537 # Not a linking table unless the PK in the middle table is the
538 # same size as the sum of the two table's PK sizes
539 0 ▶ return unless ( $fk->table_to->primary_key_size ==
Branch: 1 branch
BranchTrueFalseTotal
unless $fk->table_to->primary_key_size == $fk->table_from->primary_key_size + $fk_2->table_to->primary_key_size 0 0 0
540 ( $fk->table_from->primary_key_size + $fk_2->table_to->primary_key_size ) );
541  
542 0 my $s = $fk->table_to->schema;
543 0 my @t = ( $fk->table_to, $fk_2->table_to );
544 0 my $select = [ $t[1] ];
545  
546 my $name = $self->_make_method
547 ( type => 'linking_table',
548 class => $self->{row_class},
549 returns => 'row cursor',
550 code =>
551 0 ▶ sub { my $self = shift;
Subroutine __ANON__: not called CC=4 SCAR=30.0
552 0 my %p = @_;
553 0 ▶ if ( $p{where} )
Branch: 1 branch
BranchTrueFalseTotal
if ($p{"where"}) 0 0 0
554 {
555 0 ▶ $p{where} = [ $p{where} ] unless Alzabo::Utils::is_arrayref( $p{where}[0] );
Branch: 1 branch
BranchTrueFalseTotal
unless Alzabo::Utils::is_arrayref($p{"where"}[0]) 0 0 0
556 }
557 0 foreach my $pair ( $fk->column_pairs )
558 {
559 0 push @{ $p{where} }, [ $pair->[1], '=', $self->select( $pair->[0]->name ) ];
560 }
561  
562 0 return $s->join( tables => [[@t, $fk_2]],
563 select => $select,
564 %p ); },
565 0 ▶ foreign_key => $fk,
Branch: 1 branch
BranchTrueFalseTotal
my $name = $self->_make_method("type", "linking_table", "class", $self->{"row_class"}, "returns", "row cursor", "code", sub { my $self = shift(); my(%p) = @_; if ($p{"where"}) { $p{"where"} = [$p{"where"}] unless Alzabo::Utils::is_arrayref($p{"where"}[0]); } ; foreach my $pair ($fk->column_pairs) { push @{$p{"where"};}, [$pair->[1], "=", $self->select($pair->[0]->name)]; } ; return $s->join("tables", [[@t, $fk_2]], "select", $select, %p); } , "foreign_key", $fk, "foreign_key_2", $fk_2) or return 0 0 0
566 foreign_key_2 => $fk_2,
567 ) or return;
568  
569 $self->{row_class}->add_method_docs
570 0 ( Alzabo::MethodDocs->new
571 ( name => $name,
572 group => 'Methods that follow a linking table',
573 description =>
574 "a row cursor of related rows from the " . $fk_2->table_to->name . " table, " .
575 "via the " . $fk->table_to->name . " linking table",
576 spec => 'same as Alzabo::Runtime::Table->rows_where',
577 ) );
578 }
579  
580 sub make_lookup_columns_methods
581 0 ▶{
Subroutine make_lookup_columns_methods: not called CC=7 SCAR=40.3
Pod undocumented
582 0 my $self = shift;
583 0 my $fk = shift;
584  
585 0 ▶ return if $fk->is_one_to_many;
Branch: 1 branch
BranchTrueFalseTotal
if $fk->is_one_to_many 0 0 0
586  
587 # Make sure the relationship is to the foreign table's primary key
588 0 my @to = $fk->columns_to;
589 0 ▶ return unless ( ( scalar grep { $_->is_primary_key } @to ) == @to &&
Branch: 1 branch
BranchTrueFalseTotal
unless scalar grep({$_->is_primary_key;} @to) == @to and $fk->table_to->primary_key_size == @to 0 0 0
Condition0%0/3
scalar grep({$_->is_primary_key;} @to) == @to and $fk->table_to->primary_key_size == @to
!ll&&!rl&&r
000
MC/DC0%0/2
scalar grep({$_->is_primary_key;} @to) == @to and $fk->table_to->primary_key_size == @to
scalar grep({$_->is_primary_key;} @to) == @to $fk->table_to->primary_key_size == @to
Truth table0%0/3
A and B
A= scalar grep({$_->is_primary_key;} @to) == @toB= $fk->table_to->primary_key_size == @to
ABresult
0X0
100
111
590 ( $fk->table_to->primary_key_size == @to ) );
591  
592 0 foreach ( sort { $a->name cmp $b->name } $fk->table_to->columns )
593 {
594 0 ▶ next if $_->is_primary_key;
Branch: 1 branch
BranchTrueFalseTotal
if $_->is_primary_key 0 0 0
595  
596 0 my $col_name = $_->name;
597  
598 my $name = $self->_make_method
599 ( type => 'lookup_columns',
600 class => $self->{row_class},
601 returns => 'scalar value of column',
602 code =>
603 0 ▶ sub { my $self = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
604 0 my $row = $self->rows_by_foreign_key( foreign_key => $fk, @_ );
605 0 ▶ return unless $row;
Branch: 1 branch
BranchTrueFalseTotal
unless $row 0 0 0
606 0 return $row->select($col_name) },
607 0 ▶ foreign_key => $fk,
Branch: 1 branch
BranchTrueFalseTotal
my $name = $self->_make_method("type", "lookup_columns", "class", $self->{"row_class"}, "returns", "scalar value of column", "code", sub { my $self = shift(); my $row = $self->rows_by_foreign_key("foreign_key", $fk, @_); return unless $row; return $row->select($col_name); } , "foreign_key", $fk, "column", $_) or next 0 0 0
608 column => $_,
609 ) or next;
610  
611 $self->{row_class}->add_method_docs
612 0 ( Alzabo::MethodDocs->new
613 ( name => $name,
614 group => 'Methods that follow a lookup table',
615 description =>
616 "returns the value of " . (join '.', $fk->table_to->name, $col_name) . " for the given row by following the foreign key relationship",
617 spec => 'same as Alzabo::Runtime::Table->rows_where',
618 ) );
619 }
620 }
621  
622 sub make_hooks
623 0 ▶{
Subroutine make_hooks: not called CC=5 SCAR=31.0
Pod undocumented
624 0 my $self = shift;
625 0 my $table = shift;
626 0 my $type = shift;
627  
628 0 ▶ my $class = $type eq 'insert' ? $self->{table_class} : $self->{row_class};
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'insert' ? : 0 0 0
629  
630 0 my $pre = "pre_$type";
631 0 my $post = "post_$type";
632  
633 0 ▶ return unless $class->can($pre) || $class->can($post);
Branch: 1 branch
BranchTrueFalseTotal
unless $class->can($pre) or $class->can($post) 0 0 0
Condition0%0/3
$class->can($pre) or $class->can($post)
l!l&&r!l&&!r
000
MC/DC0%0/2
$class->can($pre) or $class->can($post)
$class->can($pre) $class->can($post)
Truth table0%0/3
A or B
A= $class->can($pre)B= $class->can($post)
ABresult
1X1
011
000
634  
635 0 my $method = join '::', $class, $type;
636  
637 {
638 0 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.2
639 0 ▶ return if *{$method}{CODE};
Branch: 1 branch
BranchTrueFalseTotal
if *{$method;}{"CODE"} 0 0 0
640 }
641  
642 0 print STDERR "Making $type hooks method $class\->$type\n"
643 if Alzabo::Debug::METHODMAKER;
644  
645 0 my $meth = "make_$type\_hooks";
646 0 $self->$meth($table);
647 }
648  
649 sub make_insert_hooks
650 0 ▶{
Subroutine make_insert_hooks: not called CC=4 SCAR=30.0
Pod undocumented
651 0 my $self = shift;
652  
653 0 my $code = '';
654 0 $code .= " return \$s->schema->run_in_transaction( sub {\n";
655 0 $code .= " my \$new;\n";
656 0 ▶ $code .= " \$s->pre_insert(\\\%p);\n" if $self->{table_class}->can('pre_insert');
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"table_class"}->can("pre_insert") 0 0 0
657 0 $code .= " \$new = \$s->SUPER::insert(\%p);\n";
658 0 ▶ $code .= " \$s->post_insert({\%p, row => \$new});\n" if $self->{table_class}->can('post_insert');
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"table_class"}->can("post_insert") 0 0 0
659 0 $code .= " return \$new;\n";
660 0 $code .= " } );\n";
661  
662 0 eval <<"EOF";
663 {
664 package $self->{table_class};
665 sub insert
666 {
667 my \$s = shift;
668 my \%p = \@_;
669  
670 $code
671  
672 }
673 }
674 EOF
675  
676 0 ▶ Alzabo::Exception::Eval->throw( error => $@ ) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
677  
678 my $hooks =
679 0 $self->_hooks_doc_string( $self->{table_class}, 'pre_insert', 'post_insert' );
680  
681 $self->{table_class}->add_class_docs
682 0 ( Alzabo::ClassDocs->new
683 ( group => 'Hooks',
684 description => "$hooks",
685 ) );
686 }
687  
688 sub _hooks_doc_string
689 0 ▶{
Subroutine _hooks_doc_string: not called CC=5 SCAR=34.0
690 0 my $self = shift;
691 0 my ($class, $hook1, $hook2) = @_;
692  
693 0 my @hooks;
694 0 ▶ push @hooks, $hook1 if $class->can($hook1);
Branch: 1 branch
BranchTrueFalseTotal
if $class->can($hook1) 0 0 0
695  
696 0 ▶ push @hooks, $hook2 if $class->can($hook2);
Branch: 1 branch
BranchTrueFalseTotal
if $class->can($hook2) 0 0 0
697  
698 0 my $hooks = 'has';
699 0 ▶ $hooks .= @hooks > 1 ? '' : ' a ';
Branch: 1 branch
BranchTrueFalseTotal
@hooks > 1 ? : 0 0 0
700 0 $hooks .= join ' and ', @hooks;
701 0 ▶ $hooks .= @hooks > 1 ? ' hooks' : ' hook';
Branch: 1 branch
BranchTrueFalseTotal
@hooks > 1 ? : 0 0 0
702  
703 0 return $hooks;
704 }
705  
706 sub make_update_hooks
707 0 ▶{
Subroutine make_update_hooks: not called CC=4 SCAR=30.0
Pod undocumented
708 0 my $self = shift;
709  
710 0 my $code = '';
711 0 $code .= " \$s->schema->run_in_transaction( sub {\n";
712 0 ▶ $code .= " \$s->pre_update(\\\%p);\n" if $self->{row_class}->can('pre_update');
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"row_class"}->can("pre_update") 0 0 0
713 0 $code .= " \$s->SUPER::update(\%p);\n";
714 0 ▶ $code .= " \$s->post_update(\\\%p);\n" if $self->{row_class}->can('post_update');
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"row_class"}->can("post_update") 0 0 0
715 0 $code .= " } );\n";
716  
717 0 eval <<"EOF";
718 {
719 package $self->{row_class};
720  
721 sub update
722 {
723 my \$s = shift;
724 my \%p = \@_;
725  
726 $code
727  
728 }
729 }
730 EOF
731  
732 0 ▶ Alzabo::Exception::Eval->throw( error => $@ ) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
733  
734 my $hooks =
735 0 $self->_hooks_doc_string( $self->{row_class}, 'pre_update', 'post_update' );
736  
737 $self->{row_class}->add_class_docs
738 0 ( Alzabo::ClassDocs->new
739 ( group => 'Hooks',
740 description => "$hooks",
741 ) );
742 }
743  
744 sub make_select_hooks
745 0 ▶{
Subroutine make_select_hooks: not called CC=4 SCAR=30.0
Pod undocumented
746 0 my $self = shift;
747  
748 0 my ($pre, $post) = ('', '');
749 $pre = " \$s->pre_select(\\\@cols);\n"
750 0 ▶ if $self->{row_class}->can('pre_update');
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"row_class"}->can("pre_update") 0 0 0
751  
752 $post = " \$s->post_select(\\\%r);\n"
753 0 ▶ if $self->{row_class}->can('post_update');
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"row_class"}->can("post_update") 0 0 0
754  
755 0 eval <<"EOF";
756 {
757 package $self->{row_class};
758  
759 sub select
760 {
761 my \$s = shift;
762 my \@cols = \@_;
763  
764 return \$s->schema->run_in_transaction( sub {
765  
766 $pre
767  
768 my \@r;
769 my %r;
770  
771 if (wantarray)
772 {
773 \@r{ \@cols } = \$s->SUPER::select(\@cols);
774 }
775 else
776 {
777 \$r{ \$cols[0] } = (scalar \$s->SUPER::select(\$cols[0]));
778 }
779 $post
780 return wantarray ? \@r{\@cols} : \$r{ \$cols[0] };
781 } );
782 }
783  
784 sub select_hash
785 {
786 my \$s = shift;
787 my \@cols = \@_;
788  
789 return \$s->schema->run_in_transaction( sub {
790  
791 $pre
792  
793 my \%r = \$s->SUPER::select_hash(\@cols);
794  
795 $post
796  
797 return \%r;
798 } );
799 }
800 }
801 EOF
802  
803 0 ▶ Alzabo::Exception::Eval->throw( error => $@ ) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
804  
805 my $hooks =
806 0 $self->_hooks_doc_string( $self->{row_class}, 'pre_select', 'post_select' );
807  
808 $self->{row_class}->add_class_docs
809 0 ( Alzabo::ClassDocs->new
810 ( group => 'Hooks',
811 description => "$hooks",
812 ) );
813 }
814  
815 sub make_delete_hooks
816 0 ▶{
Subroutine make_delete_hooks: not called CC=4 SCAR=30.0
Pod undocumented
817 0 my $self = shift;
818  
819 0 my $code = '';
820 0 $code .= " \$s->schema->run_in_transaction( sub {\n";
821 0 ▶ $code .= " \$s->pre_delete(\\\%p);\n" if $self->{row_class}->can('pre_delete');
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"row_class"}->can("pre_delete") 0 0 0
822 0 $code .= " \$s->SUPER::delete(\%p);\n";
823 0 ▶ $code .= " \$s->post_delete(\\\%p);\n" if $self->{row_class}->can('post_delete');
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"row_class"}->can("post_delete") 0 0 0
824 0 $code .= " } );\n";
825  
826 0 eval <<"EOF";
827 package $self->{row_class};
828  
829 sub delete
830 {
831 my \$s = shift;
832 my \%p = \@_;
833  
834 $code
835  
836 }
837 EOF
838  
839 0 ▶ Alzabo::Exception::Eval->throw( error => $@ ) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
840  
841 my $hooks =
842 0 $self->_hooks_doc_string( $self->{row_class}, 'pre_delete', 'post_delete' );
843  
844 $self->{row_class}->add_class_docs
845 0 ( Alzabo::ClassDocs->new
846 ( group => 'Hooks',
847 description => "$hooks",
848 ) );
849 }
850  
851 sub name
852 0 ▶{
Subroutine name: not called CC=10 SCAR=47.0
Pod undocumented
853 0 my $self = shift;
854 0 my %p = @_;
855  
856 0 ▶ return $p{table}->name if $p{type} eq 'table';
Branch: 1 branch
BranchTrueFalseTotal
if $p{"type"} eq "table" 0 0 0
857  
858 0 ▶ return $p{column}->name if $p{type} eq 'table_column';
Branch: 1 branch
BranchTrueFalseTotal
if $p{"type"} eq "table_column" 0 0 0
859  
860 0 ▶ return $p{column}->name if $p{type} eq 'row_column';
Branch: 1 branch
BranchTrueFalseTotal
if $p{"type"} eq "row_column" 0 0 0
861  
862 0 ▶ if ( $p{type} eq 'foreign_key' )
Branch: 1 branch
BranchTrueFalseTotal
if ($p{"type"} eq "foreign_key") 0 0 0
863 {
864 0 return $p{foreign_key}->table_to->name;
865 }
866  
867 0 ▶ if ( $p{type} eq 'linking_table' )
Branch: 1 branch
BranchTrueFalseTotal
if ($p{"type"} eq "linking_table") 0 0 0
868 {
869 0 my $method = $p{foreign_key}->table_to->name;
870 0 my $tname = $p{foreign_key}->table_from->name;
871 0 $method =~ s/^$tname\_?//;
872 0 $method =~ s/_?$tname$//;
873  
874 0 return $method;
875 }
876  
877 0 return join '_', map { lc $_->name } $p{foreign_key}->table_to, $p{column}
878 0 ▶ if $p{type} eq 'lookup_columns';
Branch: 1 branch
BranchTrueFalseTotal
if $p{"type"} eq "lookup_columns" 0 0 0
879  
880 0 ▶ return $p{column}->name if $p{type} eq 'lookup_columns';
Branch: 1 branch
BranchTrueFalseTotal
if $p{"type"} eq "lookup_columns" 0 0 0
881  
882 return $p{parent} ? 'parent' : 'children'
883 0 ▶ if $p{type} eq 'self_relation';
Branch: 2 branches
BranchTrueFalseTotal
if $p{"type"} eq "self_relation" 0 0 0
$p{'parent'} ? : 0 0 0
884  
885 0 die "unknown type in call to naming sub: $p{type}\n";
886 }
887  
888 package Alzabo::DocumentationContainer;
889  
890 1 my %store;
891 sub add_method_docs
892 0 ▶{
Subroutine add_method_docs: not called CC=3 SCAR=24.8
893 0 my $class = shift;
894  
895 0 my $docs = shift;
896  
897 0 my $store = $class->_get_store($class);
898  
899 0 my $group = $docs->group;
900 0 my $name = $docs->name;
901  
902 0 ▶ $store->{methods}{by_group}{$group} ||= Tie::IxHash->new;
Condition0%0/3
$store->{"methods"}{"by_group"}{$group} ||= "Tie::IxHash"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$store->{"methods"}{"by_group"}{$group} ||= "Tie::IxHash"->new
$store->{"methods"}{"by_group"}{$group} "Tie::IxHash"->new
Truth table0%0/3
A ||= B
A= $store->{"methods"}{"by_group"}{$group}B= "Tie::IxHash"->new
ABresult
1X1
011
000
903 0 $store->{methods}{by_group}{$group}->Push( $name => $docs );
904  
905 0 ▶ $store->{methods}{by_name} ||= Tie::IxHash->new;
Condition0%0/3
$store->{"methods"}{"by_name"} ||= "Tie::IxHash"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$store->{"methods"}{"by_name"} ||= "Tie::IxHash"->new
$store->{"methods"}{"by_name"} "Tie::IxHash"->new
Truth table0%0/3
A ||= B
A= $store->{"methods"}{"by_name"}B= "Tie::IxHash"->new
ABresult
1X1
011
000
906 0 $store->{methods}{by_name}->Push( $name => $docs );
907 }
908  
909 sub add_class_docs
910 0 ▶{
Subroutine add_class_docs: not called CC=2 SCAR=17.9
911 0 my $class = shift;
912  
913 0 my $docs = shift;
914  
915 0 my $store = $class->_get_store($class);
916  
917 0 my $group = $docs->group;
918  
919 0 ▶ $store->{class}{by_group}{$group} ||= [];
Condition0%0/2
$store->{"class"}{"by_group"}{$group} ||= []
l!l
00
MC/DC0%0/1
$store->{"class"}{"by_group"}{$group} ||= []
$store->{"class"}{"by_group"}{$group}
Truth table0%0/2
A
A= $store->{"class"}{"by_group"}{$group}
Aresult
11
00
920 0 push @{ $store->{class}{by_group}{$group} }, $docs;
921 }
922  
923 sub add_contained_class
924 0 ▶{
Subroutine add_contained_class: not called CC=1 SCAR=6.9
925 0 my $class = shift;
926  
927 0 my ($type, $contained) = @_;
928  
929 0 my $store = $class->_get_store($class);
930  
931 0 push @{ $store->{contained_classes}{by_name} }, $contained;
932  
933 0 push @{ $store->{contained_classes}{by_type}{$type} }, $contained;
934 }
935  
936 sub _get_store
937 0 ▶{
Subroutine _get_store: not called CC=3 SCAR=24.8
938 0 my $class = shift;
939 0 ▶ $class = ref $class || $class;
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
940  
941 0 ▶ $store{$class} ||= {};
Condition0%0/2
$store{$class} ||= {}
l!l
00
MC/DC0%0/1
$store{$class} ||= {}
$store{$class}
Truth table0%0/2
A
A= $store{$class}
Aresult
11
00
942  
943 0 return $store{$class};
944 }
945  
946 sub method_names
947 0 ▶{
Subroutine method_names: not called CC=1 SCAR=6.9
948 0 my $class = shift;
949  
950 0 my $store = $class->_get_store($class);
951  
952 0 return $store->{methods}{by_name}->Keys;
953 }
954  
955 sub methods_by_name
956 0 ▶{
Subroutine methods_by_name: not called CC=1 SCAR=6.9
957 0 my $class = shift;
958  
959 0 my $store = $class->_get_store($class);
960  
961 0 return $store->{methods}{by_name}->Values;
962 }
963  
964 sub method_groups
965 0 ▶{
Subroutine method_groups: not called CC=1 SCAR=6.9
966 0 my $class = shift;
967  
968 0 my $store = $class->_get_store($class);
969  
970 0 return keys %{ $store->{methods}{by_group} };
971 }
972  
973 sub methods_by_group
974 0 ▶{
Subroutine methods_by_group: not called CC=2 SCAR=17.9
975 0 my $class = shift;
976  
977 0 my $store = $class->_get_store($class);
978  
979 0 my $group = shift;
980  
981 return $store->{methods}{by_group}{$group}->Values
982 0 ▶ if exists $store->{methods}{by_group}{$group};
Branch: 1 branch
BranchTrueFalseTotal
if exists $store->{"methods"}{"by_group"}{$group} 0 0 0
983 }
984  
985 sub class_groups
986 0 ▶{
Subroutine class_groups: not called CC=1 SCAR=6.9
987 0 my $class = shift;
988  
989 0 my $store = $class->_get_store($class);
990  
991 0 return keys %{ $store->{class}{by_group} };
992 }
993  
994 sub class_docs_by_group
995 0 ▶{
Subroutine class_docs_by_group: not called CC=2 SCAR=17.9
996 0 my $class = shift;
997  
998 0 my $store = $class->_get_store($class);
999  
1000 0 my $group = shift;
1001  
1002 0 return @{ $store->{class}{by_name}{$group} }
1003 0 ▶ if exists $store->{class}{by_name}{$group};
Branch: 1 branch
BranchTrueFalseTotal
if exists $store->{"class"}{"by_name"}{$group} 0 0 0
1004 }
1005  
1006 sub class_docs
1007 0 ▶{
Subroutine class_docs: not called CC=1 SCAR=6.9
1008 0 my $class = shift;
1009  
1010 0 my $store = $class->_get_store($class);
1011  
1012 0 my $group = shift;
1013  
1014 0 return map { @{ $store->{class}{by_group}{$_} } }
1015 0 keys %{ $store->{class}{by_group} };
1016 }
1017  
1018 sub contained_classes
1019 0 ▶{
Subroutine contained_classes: not called CC=2 SCAR=17.9
1020 0 my $class = shift;
1021  
1022 0 my $store = $class->_get_store($class);
1023  
1024 0 return @{ $store->{contained_classes}{by_name} }
1025 0 ▶ if exists $store->{contained_classes}{by_name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $store->{"contained_classes"}{"by_name"} 0 0 0
1026  
1027 0 return;
1028 }
1029  
1030 sub method
1031 0 ▶{
Subroutine method: not called CC=2 SCAR=17.9
1032 0 my $class = shift;
1033  
1034 0 my $store = $class->_get_store($class);
1035  
1036 0 my $name = shift;
1037  
1038 return $store->{methods}{by_name}->FETCH($name)
1039 0 ▶ if exists $store->{methods}{by_name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $store->{"methods"}{"by_name"} 0 0 0
1040 }
1041  
1042 sub docs_as_pod
1043 0 ▶{
Subroutine docs_as_pod: not called CC=8 SCAR=42.8
1044 0 my $self = shift;
1045 0 ▶ my $class = ref $self || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
1046 0 my $contained = shift;
1047  
1048 0 my $store = $class->_get_store($class);
1049  
1050 0 my $pod;
1051  
1052 0 ▶ $pod .= "=pod\n\n" unless $contained;
Branch: 1 branch
BranchTrueFalseTotal
unless $contained 0 0 0
1053  
1054 0 $pod .= "=head1 $class\n\n";
1055  
1056 0 foreach my $class_doc ( $class->class_docs )
1057 {
1058 0 $pod .= $class_doc->as_pod;
1059 }
1060  
1061 0 foreach my $group ( $class->method_groups )
1062 {
1063 0 $pod .= "=head2 $group\n\n";
1064  
1065 0 foreach my $method ( $class->methods_by_group($group) )
1066 {
1067 0 $pod .= $method->as_pod;
1068 }
1069 }
1070  
1071 0 $pod .= $_ foreach $self->contained_docs;
1072  
1073 0 ▶ $pod .= "=cut\n\n" unless $contained;
Branch: 1 branch
BranchTrueFalseTotal
unless $contained 0 0 0
1074  
1075 0 return $pod;
1076 }
1077  
1078 sub contained_docs
1079 0 ▶{
Subroutine contained_docs: not called CC=1 SCAR=6.9
1080 0 my $self = shift;
1081  
1082 0 return map { $_->docs_as_pod(1) } $self->contained_classes;
1083 }
1084  
1085 package Alzabo::Docs;
1086  
1087 0 ▶sub group { shift->{group} }
Subroutine group: not called CC=1 SCAR=6.9
1088 0 ▶sub description { shift->{description} }
Subroutine description: not called CC=1 SCAR=6.9
1089  
1090 # copied from Params::ValidatePP
1091 {
1092 1 my %type_to_string =
1093 ( Params::Validate::SCALAR() => 'scalar',
1094 Params::Validate::ARRAYREF() => 'arrayref',
1095 Params::Validate::HASHREF() => 'hashref',
1096 Params::Validate::CODEREF() => 'coderef',
1097 Params::Validate::GLOB() => 'glob',
1098 Params::Validate::GLOBREF() => 'globref',
1099 Params::Validate::SCALARREF() => 'scalarref',
1100 Params::Validate::UNDEF() => 'undef',
1101 Params::Validate::OBJECT() => 'object',
1102 );
1103  
1104 sub _typemask_to_strings
1105 0 ▶ {
Subroutine _typemask_to_strings: not called CC=4 SCAR=30.0
1106 0 shift;
1107 0 my $mask = shift;
1108  
1109 0 my @types;
1110 0 foreach ( Params::Validate::SCALAR, Params::Validate::ARRAYREF,
1111 Params::Validate::HASHREF, Params::Validate::CODEREF,
1112 Params::Validate::GLOB, Params::Validate::GLOBREF,
1113 Params::Validate::SCALARREF, Params::Validate::UNDEF,
1114 Params::Validate::OBJECT )
1115 {
1116 0 ▶ push @types, $type_to_string{$_} if $mask & $_;
Branch: 1 branch
BranchTrueFalseTotal
if $mask & $_ 0 0 0
1117 }
1118 0 ▶ return @types ? @types : ('unknown');
Branch: 1 branch
BranchTrueFalseTotal
@types ? : 0 0 0
1119 }
1120 }
1121  
1122 1 package Alzabo::MethodDocs;
1123  
1124 1 ▶use Params::Validate qw( validate SCALAR ARRAYREF HASHREF );
Subroutine BEGIN: called CC=1 SCAR=0.0
1125  
1126 1 ▶use base qw(Alzabo::Docs);
Subroutine BEGIN: called CC=1 SCAR=0.0
1127  
1128 sub new
1129 0 ▶{
Subroutine new: not called CC=1 SCAR=6.9
1130 0 my $class = shift;
1131 0 my %p = validate( @_, { name => { type => SCALAR },
1132 group => { type => SCALAR },
1133 description => { type => SCALAR },
1134 spec => { type => SCALAR | ARRAYREF | HASHREF,
1135 default => undef },
1136 } );
1137  
1138 0 return bless \%p, $class;
1139 }
1140  
1141 0 ▶sub name { shift->{name} }
Subroutine name: not called CC=1 SCAR=6.9
1142 0 ▶sub spec { shift->{spec} }
Subroutine spec: not called CC=1 SCAR=6.9
1143  
1144 sub as_pod
1145 0 ▶{
Subroutine as_pod: not called CC=9 SCAR=45.0
1146 0 my $self = shift;
1147  
1148 0 my $desc = ucfirst $self->{description};
1149  
1150 0 my $spec = $self->spec;
1151  
1152 0 my $params;
1153 0 ▶ if ( defined $spec )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $spec) 0 0 0
1154 {
1155 0 ▶ if ( Alzabo::Utils::is_arrayref( $spec ) )
Branch: 2 branches
BranchTrueFalseTotal
if (Alzabo::Utils::is_arrayref($spec)) { } 0 0 0
elsif (Alzabo::Utils::is_hashref($spec)) { } 0 0 0
1156 {
1157 0 $params = "=over 4\n\n";
1158  
1159 0 foreach my $p (@$spec)
1160 {
1161 0 $params .= "=item * ";
1162 0 ▶ if ( exists $p->{type} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $p->{"type"}) 0 0 0
1163 {
1164 # hack!
1165 my $types =
1166 0 join ', ', $self->_typemask_to_strings( $p->{type} );
1167 0 $params .= "($types)";
1168 }
1169 0 $params .= "\n\n";
1170 }
1171  
1172 0 $params .= "=back\n\n";
1173 }
1174 elsif ( Alzabo::Utils::is_hashref($spec) )
1175 {
1176 0 $params = "=over 4\n\n";
1177  
1178 0 ▶ while ( my ($name, $p) = each %$spec )
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$spec) 0 0 0
1179 {
1180 0 $params .= "=item * $name ";
1181 0 ▶ if ( exists $p->{type} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $p->{"type"}) 0 0 0
1182 {
1183 # hack!
1184 my $types =
1185 0 join ', ', $self->_typemask_to_strings( $p->{type} );
1186 0 $params .= "($types)";
1187 }
1188 0 $params .= "\n\n";
1189 }
1190  
1191 0 $params .= "=back\n\n";
1192 }
1193 else
1194 {
1195 0 $params = "Parameters: $spec\n\n";
1196 }
1197 }
1198  
1199 0 my $pod = <<"EOF";
1200 =head3 $self->{name}
1201  
1202 $desc
1203  
1204 EOF
1205 0 ▶ $pod .= $params if $params;
Branch: 1 branch
BranchTrueFalseTotal
if $params 0 0 0
1206  
1207 0 return $pod;
1208 }
1209  
1210  
1211 package Alzabo::ClassDocs;
1212  
1213 1 ▶use Params::Validate qw( validate SCALAR );
Subroutine BEGIN: called CC=1 SCAR=0.0
1214  
1215 1 ▶use base qw(Alzabo::Docs);
Subroutine BEGIN: called CC=1 SCAR=0.0
1216  
1217 sub new
1218 0 ▶{
Subroutine new: not called CC=1 SCAR=6.9
1219 0 my $class = shift;
1220 0 my %p = validate( @_, { group => { type => SCALAR },
1221 description => { type => SCALAR },
1222 } );
1223  
1224 0 return bless \%p, $class;
1225 }
1226  
1227 sub as_pod
1228 0 ▶{
Subroutine as_pod: not called CC=1 SCAR=6.9
1229 0 my $self = shift;
1230  
1231 0 return ucfirst "$self->{description}\n\n";
1232 }
1233  
1234 1 1;
1235  
1236  
1237 __END__
« Alzabo/Runtime/RowState/Potential.pm ↑ Summary Alzabo/BackCompat.pm »