File Coverage

blib/lib/Class/Accessor/Children.pm
Criterion Covered Total %
statement 29 29 100.0
branch 10 10 100.0
condition n/a
subroutine 8 8 100.0
pod 3 3 100.0
total 50 50 100.0


line stmt bran cond sub pod time code
1             package Class::Accessor::Children;
2 4     4   137938 use base qw( Class::Accessor );
  4         10  
  4         4045  
3 4     4   26214 use Carp;
  4         10  
  4         386  
4 4     4   22 use vars qw( $VERSION );
  4         9  
  4         1161  
5             $VERSION = '0.02';
6              
7             sub mk_child_accessors {
8 6     6 1 9708 _mk_child_classes( mk_accessors => @_ );
9             }
10             sub mk_child_ro_accessors {
11 1     1 1 107 _mk_child_classes( mk_ro_accessors => @_ );
12             }
13             sub mk_child_wo_accessors {
14 1     1 1 100 _mk_child_classes( mk_wo_accessors => @_ );
15             }
16              
17             sub _mk_child_classes {
18 8     8   19 my $method = shift;
19 8         13 my $base = shift;
20 8 100       516 Carp::croak 'Odd number arguments' if scalar @_ % 2;
21 7         24 while ( scalar @_ ) {
22 9         336 my $name = shift;
23 9         23 my $list = shift;
24 9 100       351 Carp::croak 'Invalid child class name' if ref $name;
25 8 100       54 $list = [ grep {$_ ne ''} split( /\s+/, $list )] unless ref $list;
  2         7  
26 8 100       23 my $child = ( $name ne '' ) ? $base.'::'.$name : $base;
27 8 100       61 if ( ! $child->isa( __PACKAGE__ )) {
28 4     4   25 no strict 'refs';
  4         7  
  4         454  
29 3         9 push( @{$child.'::ISA'}, __PACKAGE__ );
  3         48  
30             }
31 8         58 $child->$method( @$list );
32             }
33             }
34              
35             =head1 NAME
36              
37             Class::Accessor::Children - Automated child-class/accessor generation
38              
39             =head1 SYNOPSIS
40              
41             BEFORE (WITHOUT THIS)
42              
43             package MyClass::Foo;
44             use base qw( Class:Accessor );
45             __PACKAGE__->mk_ro_accessors(qw( jacob michael joshua ethan ));
46              
47             package MyClass::Bar;
48             use base qw( Class:Accessor );
49             __PACKAGE__->mk_ro_accessors(qw( emily emma madison isabella ));
50              
51             package MyClass::Baz;
52             use base qw( Class:Accessor );
53             __PACKAGE__->mk_ro_accessors(qw( haruka haruto miyu yuto ));
54              
55             AFTER (WITH THIS)
56              
57             package MyClass;
58             use base qw( Class::Accessor::Children );
59             __PACKAGE__->mk_child_ro_accessors(
60             Foo => [qw( jacob michael joshua ethan )],
61             Bar => [qw( emily emma madison isabella )],
62             Baz => [qw( haruka haruto miyu yuto )],
63             );
64              
65             =head1 DESCRIPTION
66              
67             This module automagically generates child classes
68             which have accessor/mutator methods.
69              
70             This module inherits C to make accessors.
71              
72             =head1 METHODS
73              
74             This module provides the following methods in addition to all methods
75             provided by C.
76              
77             =head2 mk_child_accessors
78              
79             MyClass->mk_child_accessors( Foo => \@fields, ... );
80              
81             This generates a child class named C
82             which have accessor/mutator methods each named in C<\@fields>.
83              
84             =head2 mk_child_ro_accessors
85              
86             MyClass->mk_child_ro_accessors( Bar => \@fields, ... );
87              
88             This generates a child class named C
89             which have read-only accessors (ie. true accessors).
90              
91             =head2 mk_child_wo_accessors
92              
93             MyClass->mk_child_wo_accessors( Baz => \@fields, ... );
94              
95             This generates a child class named C
96             which have write-only accessor (ie. mutators).
97              
98             =head1 SEE ALSO
99              
100             L
101              
102             =head1 AUTHOR
103              
104             Yusuke Kawasaki L
105              
106             =head1 COPYRIGHT AND LICENSE
107              
108             Copyright (c) 2007 Yusuke Kawasaki. All rights reserved.
109             This program is free software; you can redistribute it and/or
110             modify it under the same terms as Perl itself.
111              
112             =cut
113             1;