lib/Class/Factory.pm

filter: Statementstmt 86.8% 99 / 114 Branchbran 66.6% 28 / 42 Conditioncond 36.1% 13 / 36 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 83.3% 15 / 18 Podpod 0.0% 0 / 17 totaltotal 61.7% 155 / 251 CC 36 SCAR 14.5
CC
36
Cov
73%
add_factory_type
7.6
register_factory_type
5.7
get_registered_class
5.3
CRAP 4.3
↑ Summary
1 package Class::Factory;
2  
3 # $Id: Factory.pm 46 2007-11-07 00:06:38Z cwinters $
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 $Class::Factory::VERSION = '1.06';
8  
9 1 my %CLASS_BY_FACTORY_AND_TYPE = ();
10 1 my %FACTORY_INFO_BY_CLASS = ();
11 1 my %REGISTER = ();
12  
13 # Simple constructor -- override as needed
14  
15 5 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
16 5 my ( $pkg, $type, @params ) = @_;
17 5 my $class = $pkg->get_factory_class( $type );
18 5 ▶ return undef unless ( $class );
Branch: 1 branch
BranchTrueFalseTotal
unless $class 3 2 5
19 2 my $self = bless( {}, $class );
20 2 return $self->init( @params );
21 }
22  
23  
24 # Subclasses should override, but if they don't they shouldn't be
25 # penalized...
26  
27 0 ▶sub init { return $_[0] }
Subroutine init: not called CC=1 SCAR=6.9
Pod undocumented
28  
29 # Find the class associated with $object_type
30  
31 7 ▶sub get_factory_class {
Subroutine get_factory_class: called CC=4 SCAR=13.9
Pod undocumented
32 7 my ( $item, $object_type ) = @_;
33 7 ▶ my $class = ref $item || $item;
Condition33%1/3
ref $item || $item
l!l&&r!l&&!r
070
MC/DC0%0/2
ref $item || $item
ref $item $item
Truth table33%1/3
A || B
A= ref $itemB= $item
ABresult
1X1
011
000
34 my $factory_class =
35 7 $CLASS_BY_FACTORY_AND_TYPE{ $class }->{ $object_type };
36 7 ▶ return $factory_class if ( $factory_class );
Branch: 1 branch
BranchTrueFalseTotal
if $factory_class 3 4 7
37  
38 4 $factory_class = $REGISTER{ $class }->{ $object_type };
39 4 ▶ if ( $factory_class ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($factory_class) 2 2 4
40 2 my $added_class =
41 $class->add_factory_type( $object_type, $factory_class );
42 2 return $added_class;
43 }
44 2 $item->factory_error( "Factory type '$object_type' is not defined ",
45 "in '$class'" );
46 2 return undef;
47 }
48  
49  
50 # Associate $object_type with $object_class
51  
52 6 ▶sub add_factory_type {
Subroutine add_factory_type: called CC=7 SCAR=20.3
Pod undocumented
53 6 my ( $item, $object_type, $object_class ) = @_;
54 6 ▶ my $class = ref $item || $item;
Condition33%1/3
ref $item || $item
l!l&&r!l&&!r
060
MC/DC0%0/2
ref $item || $item
ref $item $item
Truth table33%1/3
A || B
A= ref $itemB= $item
ABresult
1X1
011
000
55 6 ▶ unless ( $object_type ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($object_type) 0 6 6
56 0 $item->factory_error( "Cannot add factory type to '$class': no ",
57 "type defined");
58 }
59 6 ▶ unless ( $object_class ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($object_class) 0 6 6
60 0 $item->factory_error( "Cannot add factory type '$object_type' to ",
61 "'$class': no class defined" );
62 }
63  
64 my $set_object_class =
65 6 $CLASS_BY_FACTORY_AND_TYPE{ $class }->{ $object_type };
66 6 ▶ if ( $set_object_class ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($set_object_class) 1 5 6
67 1 $item->factory_log( "Attempt to add type '$object_type' to '$class' ",
68 "redundant; type already exists with class ",
69 "'$set_object_class'" );
70 1 return;
71 }
72  
73 # Make sure the object class looks like a perl module/script
74 # Acceptable formats:
75 # Module.pm Module.ph Module.pl Module
76 5 $object_class =~ m/^([\w:-]+(?:\.(?:pm|ph|pl))?)$/;
77 5 $object_class = $1;
78  
79 5 ▶ if ( $INC{ $object_class } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{$object_class}) { } 0 5 5
80 0 $item->factory_log( "Looks like class '$object_class' was already ",
81 "included; no further work necessary" );
82 }
83 else {
84 5 eval "require $object_class";
85 5 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 2 3 5
86 2 $item->factory_error( "Cannot add factory type '$object_type' to ",
87 "class '$class': factory class '$object_class' ",
88 "cannot be required: $@" );
89 2 return undef;
90 }
91 }
92  
93 # keep track of what classes have been included so far...
94 3 $CLASS_BY_FACTORY_AND_TYPE{ $class }->{ $object_type } = $object_class;
95  
96 # keep track of what factory and type are associated with a loaded
97 # class...
98 3 $FACTORY_INFO_BY_CLASS{ $object_class } = [ $class, $object_type ];
99  
100 3 return $object_class;
101 }
102  
103 3 ▶sub register_factory_type {
Subroutine register_factory_type: called CC=5 SCAR=17.4
Pod undocumented
104 3 my ( $item, $object_type, $object_class ) = @_;
105 3 ▶ my $class = ref $item || $item;
Condition33%1/3
ref $item || $item
l!l&&r!l&&!r
030
MC/DC0%0/2
ref $item || $item
ref $item $item
Truth table33%1/3
A || B
A= ref $itemB= $item
ABresult
1X1
011
000
106 3 ▶ unless ( $object_type ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($object_type) 0 3 3
107 0 $item->factory_error( "Cannot add factory type to '$class': no type ",
108 "defined" );
109 }
110 3 ▶ unless ( $object_class ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($object_class) 0 3 3
111 0 $item->factory_error( "Cannot add factory type '$object_type' to ",
112 "'$class': no class defined" );
113 }
114  
115 3 my $set_object_class = $REGISTER{ $class }->{ $object_type };
116 3 ▶ if ( $set_object_class ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($set_object_class) 1 2 3
117 1 $item->factory_log( "Attempt to register type '$object_type' with ",
118 "'$class' is redundant; type registered with ",
119 "class '$set_object_class'" );
120 1 return;
121 }
122 2 return $REGISTER{ $class }->{ $object_type } = $object_class;
123 }
124  
125  
126 1 ▶sub remove_factory_type {
Subroutine remove_factory_type: called CC=4 SCAR=15.6
Pod undocumented
127 1 my ( $item, @object_types ) = @_;
128 1 ▶ my $class = ref $item || $item;
Condition33%1/3
ref $item || $item
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $item || $item
ref $item $item
Truth table33%1/3
A || B
A= ref $itemB= $item
ABresult
1X1
011
000
129  
130 1 for my $object_type (@object_types) {
131 1 ▶ unless ( $object_type ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($object_type) 0 1 1
132 0 $item->factory_error(
133 "Cannot remove factory type from '$class': no type defined"
134 );
135 }
136  
137 1 delete $CLASS_BY_FACTORY_AND_TYPE{ $class }->{ $object_type };
138 }
139 }
140  
141  
142 1 ▶sub unregister_factory_type {
Subroutine unregister_factory_type: called CC=4 SCAR=15.2
Pod undocumented
143 1 my ( $item, @object_types ) = @_;
144 1 ▶ my $class = ref $item || $item;
Condition33%1/3
ref $item || $item
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $item || $item
ref $item $item
Truth table33%1/3
A || B
A= ref $itemB= $item
ABresult
1X1
011
000
145  
146 1 for my $object_type (@object_types) {
147 1 ▶ unless ( $object_type ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($object_type) 0 1 1
148 0 $item->factory_error(
149 "Cannot remove factory type from '$class': no type defined"
150 );
151 }
152  
153 1 delete $REGISTER{ $class }->{ $object_type };
154  
155 # Also delete from $CLASS_BY_FACTORY_AND_TYPE because if the object
156 # type has already been instantiated, then it will have been processed
157 # by add_factory_type(), thus creating an entry in
158 # $CLASS_BY_FACTORY_AND_TYPE. We can call register_factory_type()
159 # again, but when we try to instantiate an object via
160 # get_factory_class(), it will find the old entry in
161 # $CLASS_BY_FACTORY_AND_TYPE and use that.
162  
163 1 delete $CLASS_BY_FACTORY_AND_TYPE{ $class }->{ $object_type };
164 }
165 }
166  
167  
168 2 ▶sub get_loaded_classes {
Subroutine get_loaded_classes: called CC=3 SCAR=11.8
Pod undocumented
169 2 my ( $item ) = @_;
170 2 ▶ my $class = ref $item || $item;
Condition33%1/3
ref $item || $item
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $item || $item
ref $item $item
Truth table33%1/3
A || B
A= ref $itemB= $item
ABresult
1X1
011
000
171 2 ▶ return () unless ( ref $CLASS_BY_FACTORY_AND_TYPE{ $class } eq 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $CLASS_BY_FACTORY_AND_TYPE{$class} eq "HASH" 0 2 2
172 2 return sort values %{ $CLASS_BY_FACTORY_AND_TYPE{ $class } };
173 }
174  
175 2 ▶sub get_loaded_types {
Subroutine get_loaded_types: called CC=3 SCAR=11.8
Pod undocumented
176 2 my ( $item ) = @_;
177 2 ▶ my $class = ref $item || $item;
Condition33%1/3
ref $item || $item
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $item || $item
ref $item $item
Truth table33%1/3
A || B
A= ref $itemB= $item
ABresult
1X1
011
000
178 2 ▶ return () unless ( ref $CLASS_BY_FACTORY_AND_TYPE{ $class } eq 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $CLASS_BY_FACTORY_AND_TYPE{$class} eq "HASH" 0 2 2
179 2 return sort keys %{ $CLASS_BY_FACTORY_AND_TYPE{ $class } };
180 }
181  
182 1 ▶sub get_registered_classes {
Subroutine get_registered_classes: called CC=3 SCAR=11.8
Pod undocumented
183 1 my ( $item ) = @_;
184 1 ▶ my $class = ref $item || $item;
Condition33%1/3
ref $item || $item
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $item || $item
ref $item $item
Truth table33%1/3
A || B
A= ref $itemB= $item
ABresult
1X1
011
000
185 1 ▶ return () unless ( ref $REGISTER{ $class } eq 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $REGISTER{$class} eq "HASH" 0 1 1
186 1 return sort values %{ $REGISTER{ $class } };
187 }
188  
189 1 ▶sub get_registered_class {
Subroutine get_registered_class: called CC=4 SCAR=16.6
Pod undocumented
190 1 my ( $item, $type ) = @_;
191 1 ▶ unless ( $type ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type) 0 1 1
192 0 warn("No factory type passed");
193 0 return undef;
194 }
195 1 ▶ my $class = ref $item || $item;
Condition33%1/3
ref $item || $item
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $item || $item
ref $item $item
Truth table33%1/3
A || B
A= ref $itemB= $item
ABresult
1X1
011
000
196 1 ▶ return undef unless ( ref $REGISTER{ $class } eq 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $REGISTER{$class} eq "HASH" 0 1 1
197 1 return $REGISTER{ $class }{ $type };
198 }
199  
200 1 ▶sub get_registered_types {
Subroutine get_registered_types: called CC=3 SCAR=11.8
Pod undocumented
201 1 my ( $item ) = @_;
202 1 ▶ my $class = ref $item || $item;
Condition33%1/3
ref $item || $item
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $item || $item
ref $item $item
Truth table33%1/3
A || B
A= ref $itemB= $item
ABresult
1X1
011
000
203 1 ▶ return () unless ( ref $REGISTER{ $class } eq 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $REGISTER{$class} eq "HASH" 0 1 1
204 1 return sort keys %{ $REGISTER{ $class } };
205 }
206  
207 # Return the factory class that created $item (which can be an object
208 # or class)
209  
210 2 ▶sub get_my_factory {
Subroutine get_my_factory: called CC=3 SCAR=12.3
Pod undocumented
211 2 my ( $item ) = @_;
212 2 ▶ my $impl_class = ref( $item ) || $item;
Condition33%1/3
ref $item || $item
l!l&&r!l&&!r
200
MC/DC0%0/2
ref $item || $item
ref $item $item
Truth table33%1/3
A || B
A= ref $itemB= $item
ABresult
1X1
011
000
213 2 my $impl_info = $FACTORY_INFO_BY_CLASS{ $impl_class };
214 2 ▶ if ( ref( $impl_info ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $impl_info eq "ARRAY") 2 0 2
215 2 return $impl_info->[0];
216 }
217 0 return undef;
218 }
219  
220 # Return the type that the factory used to create $item (which can be
221 # an object or class)
222  
223 2 ▶sub get_my_factory_type {
Subroutine get_my_factory_type: called CC=1 SCAR=0.0
Pod undocumented
224 2 my ( $item ) = @_;
225 2 $item->get_factory_type_for($item);
226 }
227  
228  
229 # Return the type that the factory uses to create a given object or class.
230  
231 5 ▶sub get_factory_type_for {
Subroutine get_factory_type_for: called CC=3 SCAR=11.0
Pod undocumented
232 5 my ( $self, $item ) = @_;
233 5 ▶ my $impl_class = ref( $item ) || $item;
Condition66%2/3
ref $item || $item
l!l&&r!l&&!r
320
MC/DC0%0/2
ref $item || $item
ref $item $item
Truth table66%2/3
A || B
A= ref $itemB= $item
ABresult
1X1
011
000
234 5 my $impl_info = $FACTORY_INFO_BY_CLASS{ $impl_class };
235 5 ▶ if ( ref( $impl_info ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $impl_info eq "ARRAY") 4 1 5
236 4 return $impl_info->[1];
237 }
238 1 return undef;
239 }
240  
241 ########################################
242 # Overridable Log / Error
243  
244 0 ▶sub factory_log { shift; warn @_, "\n" }
Subroutine factory_log: not called CC=1 SCAR=6.9
Pod undocumented
245 0 ▶sub factory_error { shift; die @_, "\n" }
Subroutine factory_error: not called CC=1 SCAR=6.9
Pod undocumented
246  
247 1 1;
248  
249 __END__
↑ Summary