File Coverage

blib/lib/Class/Accessor/Children/Fast.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::Fast;
2 4     4   134079 use base qw( Class::Accessor::Fast );
  4         12  
  4         6148  
3 4     4   15925 use Carp;
  4         11  
  4         385  
4 4     4   23 use vars qw( $VERSION );
  4         5  
  4         1490  
5             $VERSION = '0.02';
6              
7             sub mk_child_accessors {
8 6     6 1 5320 _mk_child_classes( mk_accessors => @_ );
9             }
10             sub mk_child_ro_accessors {
11 1     1 1 226 _mk_child_classes( mk_ro_accessors => @_ );
12             }
13             sub mk_child_wo_accessors {
14 1     1 1 101 _mk_child_classes( mk_wo_accessors => @_ );
15             }
16              
17             sub _mk_child_classes {
18 8     8   14 my $method = shift;
19 8         14 my $base = shift;
20 8 100       534 Carp::croak 'Odd number arguments' if scalar @_ % 2;
21 7         21 while ( scalar @_ ) {
22 9         336 my $name = shift;
23 9         12 my $list = shift;
24 9 100       407 Carp::croak 'Invalid child class name' if ref $name;
25 8 100       25 $list = [ grep {$_ ne ''} split( /\s+/, $list )] unless ref $list;
  2         8  
26 8 100       29 my $child = ( $name ne '' ) ? $base.'::'.$name : $base;
27 8 100       66 if ( ! $child->isa( __PACKAGE__ )) {
28 4     4   24 no strict 'refs';
  4         24  
  4         465  
29 3         5 push( @{$child.'::ISA'}, __PACKAGE__ );
  3         63  
30             }
31 8         72 $child->$method( @$list );
32             }
33             }
34              
35             =head1 NAME
36              
37             Class::Accessor::Children::Fast - Faster, child-class/accessor generation
38              
39             =head1 SYNOPSIS
40              
41             BEFORE (WITHOUT THIS)
42              
43             package MyClass::Foo;
44             use base qw( Class:Accessor::Fast );
45             __PACKAGE__->mk_ro_accessors(qw( jacob michael joshua ethan ));
46              
47             package MyClass::Bar;
48             use base qw( Class:Accessor::Fast );
49             __PACKAGE__->mk_ro_accessors(qw( emily emma madison isabella ));
50              
51             package MyClass::Baz;
52             use base qw( Class:Accessor::Fast );
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::Fast );
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             L
103              
104             =head1 AUTHOR
105              
106             Yusuke Kawasaki L
107              
108             =head1 COPYRIGHT AND LICENSE
109              
110             Copyright (c) 2007 Yusuke Kawasaki. All rights reserved.
111             This program is free software; you can redistribute it and/or
112             modify it under the same terms as Perl itself.
113              
114             =cut
115             1;