File Coverage

gen/attr-PerlBean_Attribute_Single.pl
Criterion Covered Total %
statement 6 6 100.0
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 8 8 100.0


line stmt bran cond sub pod time code
1 1     1   5 use strict;
  1         2  
  1         421  
2              
3             push(@::bean_desc, {
4             bean_opt => {
5             abstract => 'SINGLE bean attribute information',
6             package => 'PerlBean::Attribute::Single',
7             use_perl_version => 5.005,
8             base => [ qw(PerlBean::Attribute)],
9             description => <
10             C contains SINGLE bean attribute information. It is a subclass of C. The code and documentation methods are implemented.
11             EOF
12             short_description => 'contains SINGLE bean attribute information',
13             synopsis => &get_syn(),
14             },
15             attr_opt => [
16             {
17             method_factory_name => 'allow_empty',
18             type => 'BOOLEAN',
19             default_value => 1,
20             short_description => 'the attribute is allowed to be empty',
21             },
22             {
23             method_factory_name => 'allow_isa',
24             type => 'MULTI',
25             unique => 1,
26             short_description => 'the list of allowed classes',
27             },
28             {
29             method_factory_name => 'allow_ref',
30             type => 'MULTI',
31             unique => 1,
32             short_description => 'the list of allowed references',
33             },
34             {
35             method_factory_name => 'allow_rx',
36             type => 'MULTI',
37             unique => 1,
38             short_description => 'the list of allow regular expressions',
39             },
40             {
41             method_factory_name => 'allow_value',
42             type => 'MULTI',
43             unique => 1,
44             short_description => 'allowed values',
45             },
46             ],
47             meth_opt => [
48             {
49             method_name => 'create_method_get',
50             documented => 0,
51             body => <<'THE_EOF',
52             my $self = shift;
53              
54             my $an = $self->get_method_factory_name();
55             my $op = &{$MOF}('get');
56             my $mb = $self->get_method_base();
57             my $pkg_us = $self->get_package_us();
58             my $desc = defined( $self->get_short_description() ) ? $self->get_short_description() : 'not described option';
59              
60             # Create and return the method
61             return( PerlBean::Method->new( {
62             method_name => "$op$mb",
63             documented => $self->is_documented(),
64             volatile => 1,
65             description => <
66             Returns ${desc}.
67             EOF
68             body => <
69             ${IND}my \$self${AO}=${AO}shift;
70              
71             ${IND}return${BFP}(${ACS}\$self->{$pkg_us}{$an}${ACS});
72             EOF
73             } ) );
74             THE_EOF
75             },
76             {
77             method_name => 'create_method_set',
78             documented => 0,
79             body => <<'THE_EOF',
80             my $self = shift;
81              
82             my $an = $self->get_method_factory_name();
83             my $an_esc = $self->_esc_aq($an);
84             my $op = &{$MOF}('set');
85             my $mb = $self->get_method_base();
86             my $ec = $self->get_exception_class();
87             my $pkg = $self->get_package();
88             my $pkg_us = $self->get_package_us();
89              
90             my $desc = defined( $self->get_short_description() ) ? $self->get_short_description() : 'not described option';
91             my $def = defined( $self->get_default_value() ) ? ' Default value at initialization is C<' . $self->get_default_value() . '>.' : '';
92             my $empt = $self->is_allow_empty() ? '' : ' C may not be C.';
93             my $exc = ' On error an exception C<' . $self->get_exception_class() . '> is thrown.';
94             my $attr_overl = $self->_get_overloaded_attribute();
95             my $overl = defined($attr_overl) ? " B Methods B> are overloaded from package C<". $attr_overl->get_package() .'>.': '';
96              
97              
98             # Make body
99             my $body = <
100             ${IND}my \$self${AO}=${AO}shift;
101             ${IND}my \$val${AO}=${AO}shift;
102              
103             EOF
104              
105             # Check if value is allowed to be empty
106             if ( ! $self->is_allow_empty() ) {
107             $body .= <
108             ${IND}# Value for $an_esc is not allowed to be empty
109             ${IND}defined${BFP}(\$val)${AO}||${AO}throw $ec${BFP}("ERROR: ${pkg}::$op$mb, value may not be empty.");
110              
111             EOF
112             }
113              
114             # Check if isa/ref/rx/value is allowed
115             $body .= <
116             ${IND}# Check if isa/ref/rx/value is allowed
117             ${IND}\&_value_is_allowed${BFP}(${ACS}$an_esc,${AC}\$val${ACS})${AO}||${AO}throw $ec${BFP}("ERROR: ${pkg}::$op$mb, the specified value '\$val' is not allowed.");
118              
119             EOF
120              
121             # Assignment and method tail
122             $body .= <
123             ${IND}# Assignment
124             ${IND}\$self->{$pkg_us}{$an}${AO}=${AO}\$val;
125             EOF
126              
127             # Make description
128             my $description = <
129             Set ${desc}. C is the value.${def}${empt}${exc}${overl}
130             EOF
131              
132             # Add clauses to the description
133             my $clauses = $self->mk_doc_clauses();
134             if ($clauses) {
135             $description .= "\n" . $clauses;
136             }
137              
138             # Create and return the method
139             return( PerlBean::Method->new( {
140             method_name => "$op$mb",
141             parameter_description => 'VALUE',
142             documented => $self->is_documented(),
143             volatile => 1,
144             description => $description,
145             body => $body,
146             } ) );
147             THE_EOF
148             },
149             {
150             method_name => 'create_methods',
151             body => <<'EOF',
152             my $self = shift;
153              
154             return(
155             $self->create_method_get(),
156             $self->create_method_set()
157             );
158             EOF
159             },
160             {
161             method_name => 'write_allow_isa',
162             documented => 0,
163             body => <<'THE_EOF',
164             my $self = shift;
165              
166             scalar( $self->values_allow_isa() ) || return('');
167              
168             my $an = $self->_esc_aq( $self->get_method_factory_name() );
169             my $dv = $self->_esc_aq( sort( $self->values_allow_isa() ) );
170             return( "${IND}$an${AO}=>${AO}\[${ACS}$dv${ACS}],\n" );
171             THE_EOF
172             },
173             {
174             method_name => 'write_allow_ref',
175             documented => 0,
176             body => <<'THE_EOF',
177             my $self = shift;
178              
179             scalar( $self->values_allow_ref() ) || return('');
180              
181             my $an = $self->_esc_aq( $self->get_method_factory_name() );
182             my @dv = sort( $self->_esc_aq( $self->values_allow_ref() ) );
183              
184             my $ass = "${IND}$an${AO}=>${AO}\{\n";
185             foreach my $dv (@dv) {
186             $ass .= "${IND}${IND}$dv${AO}=>${AO}1,\n";
187             }
188             $ass .= "${IND}},\n";
189              
190             return($ass);
191             THE_EOF
192             },
193             {
194             method_name => 'write_allow_rx',
195             documented => 0,
196             body => <<'THE_EOF',
197             my $self = shift;
198              
199             scalar( $self->values_allow_rx() ) || return('');
200              
201             my $an = $self->_esc_aq( $self->get_method_factory_name() );
202             my $dv = $self->_esc_aq( sort( $self->values_allow_rx() ) );
203             return( "${IND}$an${AO}=>${AO}\[${ACS}$dv${ACS}],\n" );
204             THE_EOF
205             },
206             {
207             method_name => 'write_allow_value',
208             documented => 0,
209             body => <<'THE_EOF',
210             my $self = shift;
211             my $fh = shift;
212              
213             scalar( $self->values_allow_value() ) || return('');
214              
215             my $an = $self->_esc_aq( $self->get_method_factory_name() );
216             my @dv = sort( $self->_esc_aq( $self->values_allow_value() ) );
217              
218             my $ass = "${IND}$an${AO}=>${AO}\{\n";
219             foreach my $dv (@dv) {
220             $ass .= "${IND}${IND}$dv${AO}=>${AO}1,\n";
221             }
222             $ass .= "${IND}},\n";
223             THE_EOF
224             },
225             {
226             method_name => 'write_default_value',
227             body => <<'THE_EOF',
228             my $self = shift;
229             my $fh = shift;
230              
231             defined( $self->get_default_value() ) || return('');
232              
233             my $an = $self->_esc_aq( $self->get_method_factory_name() );
234             my $dv = $self->_esc_aq( $self->get_default_value() );
235              
236             return( "${IND}$an${AO}=>${AO}$dv,\n" );
237             THE_EOF
238             },
239             {
240             method_name => 'write_constructor_option_code',
241             body => <<'THE_EOF',
242             my $self = shift;
243              
244             my $an = $self->get_method_factory_name();
245             my $op = &{$MOF}('set');
246             my $mb = $self->get_method_base();
247             my $ec = $self->get_exception_class();
248             my $pkg = $self->get_package();
249              
250             # Comment
251             my $code = "${IND}# $an, " . $self->type();
252             $code .= $self->is_mandatory() ? ', mandatory' : '';
253             $code .= defined( $self->get_default_value() ) ? ', with default value' : '';
254             $code .= "\n";
255              
256             # is_mandatory check
257             if ( $self->is_mandatory() ) {
258             $code .= <
259             ${IND}exists${BFP}(${ACS}\$opt->{$an}${ACS})${AO}||${AO}throw $ec${BFP}("ERROR: ${pkg}::_initialize, option '$an' is mandatory.");
260             EOF
261             }
262              
263             if ( $self->is_mandatory() ) {
264             $code .= <
265             ${IND}\$self->$op$mb${BFP}(${ACS}\$opt->{$an}${ACS});
266             EOF
267             }
268             else {
269             if ( defined( $self->get_default_value() ) ) {
270             $code .= <
271             ${IND}\$self->$op$mb${BFP}(${ACS}exists${BFP}(${ACS}\$opt->{$an}${ACS})${AO}?${AO}\$opt->{$an}${AO}:${AO}\$DEFAULT_VALUE{$an}${ACS});
272             EOF
273             }
274             else {
275             $code .= <
276             ${IND}exists${BFP}(${ACS}\$opt->{$an}${ACS})${AO}&&${AO}\$self->$op$mb${BFP}(${ACS}\$opt->{$an}${ACS});
277             EOF
278             }
279             }
280              
281             # Empty line
282             $code .= "\n";
283              
284             return($code);
285             THE_EOF
286             },
287             {
288             method_name => 'write_constructor_option_doc',
289             body => <<'THE_EOF',
290             my $self = shift;
291              
292             # Do nothing if not documented
293             $self->is_documented() || return('');
294              
295             my $an = $self->get_method_factory_name();
296             my $op = &{$MOF}('set');
297             my $mb = $self->get_method_base();
298             my $mand = $self->is_mandatory() ? ' Mandatory option.' : '';
299             my $def = '';
300             if ( defined( $self->get_default_value() ) ) {
301             $def = ' Defaults to B<' . $self->_esc_aq( $self->get_default_value() ) . '>.';
302             }
303              
304             return(<
305              
306             \=item B>
307              
308             Passed to L<$op$mb${BFP}()>.${mand}${def}
309             EOF
310             THE_EOF
311             },
312             {
313             method_name => 'mk_doc_clauses',
314             body => <<'THE_EOF',
315             my $self = shift;
316              
317             # Return empty if no clauses at all
318             return('') if ( ! scalar( $self->values_allow_isa() ) &&
319             ! scalar( $self->values_allow_ref() ) &&
320             ! scalar( $self->values_allow_rx() ) &&
321             ! scalar( $self->values_allow_value() )
322             );
323              
324             # Make the clauses head for documentation
325             my $doc = <
326             \=over
327              
328             EOF
329              
330             # Make body
331             $doc .= $self->mk_doc_clauses_allow_isa(@_);
332             $doc .= $self->mk_doc_clauses_allow_ref(@_);
333             $doc .= $self->mk_doc_clauses_allow_rx(@_);
334             $doc .= $self->mk_doc_clauses_allow_value(@_);
335              
336             # Make tail
337             $doc .= <
338             \=back
339             EOF
340              
341             # Return the clauses for documentation
342             return($doc);
343             THE_EOF
344             },
345             {
346             method_name => 'mk_doc_clauses_allow_isa',
347             documented => 0,
348             body => <<'THE_EOF',
349             my $self = shift;
350              
351             # Return empty string if no values_allow_isa
352             return('') if ( ! scalar( $self->values_allow_isa() ) );
353              
354             # Make clauses head
355             my $clauses = <
356             \=item VALUE must be a (sub)class of:
357              
358             \=over
359              
360             EOF
361              
362             # Make clauses body
363             foreach my $class ( sort( $self->values_allow_isa() ) ) {
364             $clauses .= <
365             \=item ${class}
366              
367             EOF
368             }
369              
370             # Make clauses tail
371             $clauses .= <
372             \=back
373              
374             EOF
375              
376             # Return clauses
377             return($clauses);
378             THE_EOF
379             },
380             {
381             method_name => 'mk_doc_clauses_allow_ref',
382             documented => 0,
383             body => <<'THE_EOF',
384             my $self = shift;
385              
386             # Return empty string if no values_allow_ref
387             return('') if ( ! scalar( $self->values_allow_ref() ) );
388              
389             # Make $or for other clauses that apply and that are written before these
390             # clauses
391             my $or = scalar( $self->values_allow_isa() ) ? 'Or, ' : '';
392              
393             # Make clauses head
394             my $clauses = <
395             \=item ${or}VALUE must be a reference of:
396              
397             \=over
398              
399             EOF
400              
401             # Make clauses body
402             foreach my $class ( sort( $self->values_allow_ref() ) ) {
403             $clauses .= <
404             \=item ${class}
405              
406             EOF
407             }
408              
409             # Make clauses tail
410             $clauses .= <
411             \=back
412              
413             EOF
414              
415             # Return clauses
416             return($clauses);
417             THE_EOF
418             },
419             {
420             method_name => 'mk_doc_clauses_allow_rx',
421             documented => 0,
422             body => <<'THE_EOF',
423             my $self = shift;
424              
425             # Return empty string if no values_allow_rx
426             return('') if ( ! scalar( $self->values_allow_rx() ) );
427              
428             # Make $or for other clauses that apply and that are written before these
429             # clauses
430             my $or = scalar( $self->values_allow_isa() || $self->values_allow_ref() ) ?
431             'Or, ' : '';
432              
433             # Make clauses head
434             my $clauses = <
435             \=item ${or}VALUE must match regular expression:
436              
437             \=over
438              
439             EOF
440              
441             # Make clauses body
442             foreach my $class ( sort( $self->values_allow_rx() ) ) {
443             $clauses .= <
444             \=item ${class}
445              
446             EOF
447             }
448              
449             # Make clauses tail
450             $clauses .= <
451             \=back
452              
453             EOF
454              
455             # Return clauses
456             return($clauses);
457             THE_EOF
458             },
459             {
460             method_name => 'mk_doc_clauses_allow_value',
461             documented => 0,
462             body => <<'THE_EOF',
463             my $self = shift;
464              
465             # Return empty string if no values_allow_value
466             return('') if ( ! scalar( $self->values_allow_value() ) );
467              
468             # Make $or for other clauses that apply and that are written before these
469             # clauses
470             my $or = scalar( $self->values_allow_isa() || $self->values_allow_ref() ||
471             $self->values_allow_rx() ) ? 'Or, ' : '';
472              
473             # Make clauses head
474             my $clauses = <
475             \=item ${or}VALUE must be a one of:
476              
477             \=over
478              
479             EOF
480              
481             # Make clauses body
482             foreach my $val ( sort( $self->values_allow_value() ) ) {
483             $clauses .= <
484             \=item ${val}
485              
486             EOF
487             }
488              
489             # Make clauses tail
490             $clauses .= <
491             \=back
492              
493             EOF
494              
495             # Return clauses
496             return($clauses);
497             THE_EOF
498             },
499             ],
500             sym_opt => [
501             ],
502             use_opt => [
503             {
504             dependency_name => 'PerlBean::Method',
505             },
506             {
507             dependency_name => 'PerlBean::Style',
508             import_list => [ 'qw(:codegen)' ],
509             },
510             ],
511             } );
512              
513             sub get_syn {
514 1     1   6 use IO::File;
  1         3  
  1         260  
515             my $fh = IO::File->new('< syn-PerlBean_Attribute_Single.pl');
516             $fh = IO::File->new('< gen/syn-PerlBean_Attribute_Single.pl') if (! defined($fh));
517             my $syn = '';
518             my $prev_line = $fh->getline ();
519             while (my $line = $fh->getline ()) {
520             $syn .= ' ' . $prev_line;
521             $prev_line = $line;
522             }
523             return($syn);
524             }
525              
526             1;