lib/Badger/Factory/Class.pm

filter: Statementstmt 74.4% 35 / 47 Branchbran 66.6% 8 / 12 Conditioncond 57.1% 4 / 7 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 66.6% 6 / 9 Podpod 100% 5 / 5 totaltotal 71.4% 60 / 84 CC 10 SCAR 13.3
CC
10
Cov
71%
_export_fail_hook
6.0
names
6.0
path
3.0
CRAP 3.8
« lib/Badger/Filesystem/Directory.pm ↑ Summary lib/Badger/Apps.pm »
1 #========================================================================
2 #
3 # Badger::Factory::Class
4 #
5 # DESCRIPTION
6 # Subclass of Badger::Class for creating Badger::Factory sub-classes.
7 #
8 # AUTHOR
9 # Andy Wardley <abw@wardley.org>
10 #
11 #========================================================================
12  
13 package Badger::Factory::Class;
14  
15 27 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 use Badger::Class
17 27 ▶ version => 0.01,
Subroutine BEGIN: called CC=1 SCAR=0.0
18 debug => 0,
19 uber => 'Badger::Class',
20 hooks => 'item path names default',
21 words => 'ITEM ITEMS',
22 utils => 'plural permute_fragments',
23 import => 'CLASS',
24 constants => 'DELIMITER ARRAY HASH',
25 constant => {
26 PATH_SUFFIX => '_PATH',
27 NAMES_SUFFIX => '_NAMES',
28 FACTORY => 'Badger::Factory',
29 27 };
30 # chicken and egg
31 # exports => {
32 # fail => \&_export_fail_hook,
33 # };
34  
35 27 CLASS->export_before(\&_export_before_hook);
36 27 CLASS->export_fail(\&_export_fail_hook);
37  
38 # catch a hook that has the same name as the items, i.e. widgets
39  
40 27 ▶sub _export_before_hook {
Subroutine _export_before_hook: called CC=2 SCAR=7.0
41 27 my ($class, $target) = @_;
42 27 my $klass = class($target, $class);
43 # special-case: we don't want to force the factory base class on
44 # Badger::Class if it's loading this module as the uber parent of a
45 # Factory::Class subclass (e.g. Template::TT3::Factory::Class).
46 27 ▶ return if $target eq 'Badger::Class';
Branch: 1 branch
BranchTrueFalseTotal
if $target eq "Badger::Class" 0 27 27
47 27 $class->debug("$class setting $klass ($target) base class to ", $class->FACTORY)
48 if DEBUG;
49 27 $klass->base($class->FACTORY);
50 }
51  
52  
53 54 ▶sub _export_fail_hook {
Subroutine _export_fail_hook: called CC=6 SCAR=18.0
54 54 my ($class, $target, $symbol, $symbols) = @_;
55 54 my $klass = class($target, $class);
56 54 my $items = $klass->var(ITEMS);
57  
58 # look for $ITEMS or fall back on plural($ITEM)
59 54 ▶ unless ($items) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($items) 54 0 54
60 54 my $item = $klass->var(ITEM);
61 54 ▶ $items = plural($item) if $item;
Branch: 1 branch
BranchTrueFalseTotal
if $item 27 27 54
62 }
63  
64 # $target->debug("looking for $items to match $symbol\n");
65  
66 # if the import symbols matches $items (e.g. widgets) then push the
67 # next argument into the relevant package var (e.g. $WIDGETS)
68 54 ▶ if ($items && $items eq $symbol) {
Branch: 1 branch
BranchTrueFalseTotal
if ($items and $items eq $symbol) { } 1 53 54
Condition100%3/3
$items and $items eq $symbol
!ll&&!rl&&r
27261
MC/DC100%2/2
$items and $items eq $symbol
$items $items eq $symbol
Truth table100%3/3
A and B
A= $itemsB= $items eq $symbol
ABresult
0X0
100
111
69 1 ▶ croak "You didn't specify a value for the '$items' load option."
Branch: 1 branch
BranchTrueFalseTotal
unless @$symbols 0 1 1
70 unless @$symbols;
71 1 $klass->var( uc($items) => shift @$symbols );
72 }
73 else {
74 53 $class->_export_fail($target, $symbol, $symbols);
75 }
76 }
77  
78  
79 0 ▶sub default {
Subroutine default: not called CC=1 SCAR=6.9
Pod documented
80 0 my ($self, $item) = @_;
81 0 $self->var( DEFAULT => $item );
82 0 return $self;
83 }
84  
85  
86 27 ▶sub item {
Subroutine item: called CC=1 SCAR=0.0
Pod documented
87 27 my ($self, $item) = @_;
88 27 $self->var( ITEM => $item );
89 27 return $self;
90 }
91  
92  
93 0 ▶sub items {
Subroutine items: not called CC=1 SCAR=6.9
Pod documented
94 0 my ($self, $items) = @_;
95 0 $self->var( ITEMS => $items );
96 0 return $self;
97 }
98  
99  
100 26 ▶sub path {
Subroutine path: called CC=3 SCAR=11.1
Pod documented
101 26 my ($self, $path) = @_;
102 26 ▶ my $type = $self->var(ITEM)
Condition50%1/2
$self->var('ITEM') || croak("\$ITEM is not defined for $self. Please add an 'item' option")
l!l
260
MC/DC0%0/1
$self->var('ITEM') || croak("\$ITEM is not defined for $self. Please add an 'item' option")
$self->var('ITEM')
Truth table50%1/2
A
A= $self->var('ITEM')
Aresult
11
00
103 || croak "\$ITEM is not defined for $self. Please add an 'item' option";
104 26 my $var = uc($type) . PATH_SUFFIX;
105  
106 26 ▶ $path = [ map { permute_fragments($_) } split(DELIMITER, $path) ]
Branch: 1 branch
BranchTrueFalseTotal
unless ref $path eq "ARRAY" 26 0 26
107 unless ref $path eq ARRAY;
108  
109 26 $self->debug("adding $var => [", join(', ', @$path), "]") if DEBUG;
110 # $self->base(FACTORY);
111  
112 # we use import_symbol() rather than var() so that it gets declared
113 # properly, thus avoiding undefined symbol warnings
114 26 $self->import_symbol( $var => \$path );
115
116 26 return $self;
117 }
118  
119  
120 0 ▶sub names {
Subroutine names: not called CC=2 SCAR=17.9
Pod documented
121 0 my ($self, $map) = @_;
122 0 ▶ my $type = $self->var(ITEM)
Condition0%0/2
$self->var('ITEM') || croak("\$ITEM is not defined for $self. Please add an 'item' option")
l!l
00
MC/DC0%0/1
$self->var('ITEM') || croak("\$ITEM is not defined for $self. Please add an 'item' option")
$self->var('ITEM')
Truth table0%0/2
A
A= $self->var('ITEM')
Aresult
11
00
123 || croak "\$ITEM is not defined for $self. Please add an 'item' option";
124 0 my $var = uc($type) . NAMES_SUFFIX;
125  
126 0 $self->debug("$self adding names $var => {", join(', ', %$map), "}") if DEBUG;
127  
128 # we use import_symbol() rather than var() so that it gets declared
129 # properly, thus avoiding undefined symbol warnings
130 0 $self->import_symbol( $var => \$map );
131
132 0 return $self;
133 }
134  
135  
136 =head1 NAME
137  
138 Badger::Factory::Class - class module for Badger::Factory sub-classes
139  
140 =head1 SYNOPSIS
141  
142 This module can be used to create subclasses of L<Badger::Factory>.
143  
144 package My::Widgets;
145
146 use Badger::Factory::Class
147 version => 0.01,
148 item => 'widget',
149 path => 'My::Widget Your::Widget',
150 widgets => {
151 extra => 'Another::Widget::Module',
152 super => 'Golly::Gosh',
153 },
154 names => {
155 html => 'HTML',
156 color => 'Colour',
157 };
158  
159 package main;
160
161 # class method
162 my $widget = My::Widgets->widget( foo => @args );
163
164 # object method
165 my $widgets = My::Widgets->new;
166 my $widget = $widgets->widget( foo => @args );
167  
168 =head1 DESCRIPTION
169  
170 This module is a subclass of L<Badger::Class> specialised for the purpose
171 of creating L<Badger::Factory> subclasses. It is used by the
172 L<Badger::Codecs> module among others.
173  
174 =head1 METHODS
175  
176 The following methods are provided in addition to those inherited
177 from the L<Badger::Class> base class.
178  
179 =head2 item($name)
180  
181 The singular name of the item that the factory manages. This is used
182 to set the C<$ITEM> package variable for L<Badger::Factory> to use.
183  
184 =head2 items($name)
185  
186 The plural name of the item that the factory manages. This is used
187 to set the C<$ITEMS> package variable for L<Badger::Factory> to use.
188  
189 =head2 path($name)
190  
191 A list of module names that form the search path when loading modules. This
192 will set the relevant package variable depending on the value of C<$ITEMS> (or
193 the regular plural form of C<$ITEM> if C<$ITEMS> is undefined). For example,
194 is C<$ITEMS> is set to C<widgets> then this method will set C<$WIDGETS_PATH>.
195  
196 You can specify the path as a reference to a list of module bases, e.g.
197  
198 use Badger::Factory::Class
199 item => 'widget',
200 path => ['My::Widget', 'Your::Widget'];
201  
202 Or as a single string containing multiple values separated by whitespace.
203  
204 use Badger::Factory::Class
205 item => 'widget',
206 path => 'My::Widget Your::Widget';
207  
208 If you specify it as a single string then you can also include optional
209 and/or alternate parts in parentheses. For example the above can be
210 written more concisely as:
211  
212 use Badger::Factory::Class
213 item => 'widget',
214 path => '(My|Your)::Widget';
215  
216 If the parentheses don't contain a vertical bar then then enclosed fragment
217 is treated as being optional. So instead of writing something like:
218  
219 use Badger::Factory::Class
220 item => 'widget',
221 path => 'Badger::Widget BadgerX::Widget';
222  
223 You can write:
224  
225 use Badger::Factory::Class
226 item => 'widget',
227 path => 'Badger(X)::Widget';
228  
229 See the L<permute_fragments()|Badger::Utils/permute_fragments()> function in
230 L<Badger::Utils> for further details on how fragments are expanded.
231  
232 =head2 names($names)
233  
234 A reference to a hash array of name mappings. This can be used to handle any
235 unusual spellings or capitalisations. See L<Badger::Factory> for further
236 details.
237  
238 =head2 default($name)
239  
240 The default name to use when none is specified in a request for a module.
241  
242 =head1 AUTHOR
243  
244 Andy Wardley L<http://wardley.org/>
245  
246 =head1 COPYRIGHT
247  
248 Copyright (C) 2006-2009 Andy Wardley. All Rights Reserved.
249  
250 This module is free software; you can redistribute it and/or
251 modify it under the same terms as Perl itself.
252  
253 =head1 SEE ALSO
254  
255 L<Badger::Factory>, L<Badger::Codecs>
256  
257 =cut
258  
259 # Local Variables:
260 # mode: perl
261 # perl-indent-level: 4
262 # indent-tabs-mode: nil
263 # End:
264 #
265 # vim: expandtab shiftwidth=4:
266  
267  
268  
269 27 1;
« lib/Badger/Filesystem/Directory.pm ↑ Summary lib/Badger/Apps.pm »