lib/Badger/Modules.pm

filter: Statementstmt 89.6% 104 / 116 Branchbran 52.9% 18 / 34 Conditioncond 57.1% 8 / 14 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 86.6% 13 / 15 Podpod 100% 9 / 9 totaltotal 78.5% 154 / 196 CC 26 SCAR 20.4
CC
26
Cov
79%
module
12.4
path
12.0
modules
12.0
CRAP 7.7
« lib/Badger/Data/Facet/Text.pm ↑ Summary lib/Badger/Filesystem/Path.pm »
1 #========================================================================
2 #
3 # Badger::Modules
4 #
5 # DESCRIPTION
6 # Module for loading and instantiating other modules.
7 #
8 # NOTE
9 # Badger::Factory is being cleaved in twain. Badger::Modules will
10 # implement the lower level parts related to finding and loading
11 # modules. Badger::Factory will be a subclass specialised for creating
12 # object instances.
13 #
14 # AUTHOR
15 # Andy Wardley <abw@wardley.org>
16 #
17 #========================================================================
18  
19 package Badger::Modules;
20  
21 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use Badger::Debug ':dump';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 use Badger::Class
24 1 version => 0.01,
25 debug => 0,
26 base => 'Badger::Prototype Badger::Exporter',
27 import => 'class',
28 utils => 'plural blessed textlike dotid camel_case',
29 accessors => 'item items',
30 words => 'ITEM ITEMS ISA TOLERANT BADGER_LOADED',
31 constants => 'PKG ARRAY HASH REFS ONCE DEFAULT',
32 constant => {
33 OBJECT => 'object',
34 FOUND => 'found',
35 FOUND_REF => 'found_ref',
36 PATH_SUFFIX => '_PATH',
37 NAMES_SUFFIX => '_NAMES',
38 DEFAULT_SUFFIX => '_DEFAULT',
39 },
40 methods => {
41 init => \&init_modules,
42 throws => \&item,
43 },
44 messages => {
45 no_item => 'No item(s) specified for factory to manage',
46 no_default => 'No default defined for %s factory',
47 bad_ref => 'Invalid reference for %s factory item %s: %s',
48 bad_method => q{Can't locate object method "%s" via package "%s" at %s line %s},
49 failed => q{Error loading %s module %s as %s: %s},
50 1 ▶ };
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 1 our $ITEM = 'module';
53  
54  
55 sub init_modules {
56 3 ▶ my ($self, $config) = @_;
Subroutine init_modules: called CC=8 SCAR=20.9
Pod documented
57 3 my $class = $self->class;
58 3 my ($item, $items);
59  
60 3 $self->debug("initialising modules: ", $self->dump_data($config)) if DEBUG;
61  
62 $config->{ tolerant } = $class->any_var(TOLERANT)
63 3 ▶ unless defined $config->{ tolerant };
Branch: 1 branch
BranchTrueFalseTotal
unless defined $config->{"tolerant"} 2 1 3
64  
65 # 'item' and 'items' can be specified as config params or we look for
66 # $ITEM and $ITEMS variables in the current package or those of any
67 # base classes. NOTE: $ITEM and $ITEMS must be in the same package
68 3 ▶ unless ($item = $config->{ item }) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($item = $config->{"item"}) 2 1 3
69 2 foreach my $pkg ($class->heritage) {
70 1 ▶ no strict REFS;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 1 ▶ no warnings ONCE;
Subroutine BEGIN: called CC=1 SCAR=0.0
72
73 2 ▶ if (defined ($item = ${ $pkg.PKG.ITEM })) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($item = ${$pkg . '::' . "ITEM";})) 2 0 2
74 2 $items = ${ $pkg.PKG.ITEMS };
75 2 last;
76 }
77 }
78 }
79 3 ▶ return $self->error_msg('no_item')
Branch: 1 branch
BranchTrueFalseTotal
unless $item 0 3 3
80 unless $item;
81  
82 # use 'items' in config, or grokked from $ITEMS, or guess plural
83 3 ▶ $items = $config->{ items } || $items || plural($item);
Condition33%2/6
$config->{'items'} || $items || plural($item)
l!l&&r!l&&!r
030
$config->{"items"} or $items
l!l&&r!l&&!r
003
MC/DC0%0/3
$config->{'items'} || $items || plural($item)
$config->{"items"} $items plural($item)
Truth table25%1/4
A or B || C
A= $config->{"items"}B= $itemsC= plural($item)
ABCresult
1XX1
01X1
0011
0000
84  
85 3 my $ipath = $item.PATH_SUFFIX;
86 3 my $inames = $item.NAMES_SUFFIX;
87 3 my $idefault = $item.DEFAULT_SUFFIX;
88
89 # Merge all XXXX_PATH package vars with any 'xxxx_path' or 'path' config
90 # items. Ditto for XXXX_NAME / 'xxxx_name' / 'aka' and XXXXS/ 'xxxxs'
91
92 3 my @path = @$config{ path => lc $ipath };
93 3 my @names = @$config{ names => lc $inames };
94 3 $self->{ path } = $class->list_vars(uc $ipath, @path);
95 3 $self->{ names } = $class->hash_vars(uc $inames, @names);
96 3 $self->{ $items } = $class->hash_vars(uc $items, $config->{ $items }); # TODO: this could clash
97 3 $self->{ tolerant } = $config->{ tolerant };
98 3 $self->{ items } = $items;
99 3 $self->{ item } = $item;
100 3 $self->{ loaded } = { }; # TODO: make this the same thing?
101  
102 $self->debug(
103 " Item: $self->{ item }\n",
104 "Items: $self->{ items }\n",
105 " Path: ", $ipath, ": ", $self->dump_data($self->{ path }), "\n",
106 "Names: ", $inames, ": ", $self->dump_data($self->{ names })
107 3 ) if DEBUG;
108  
109 3 return $self;
110 }
111  
112  
113 sub path {
114 0 ▶ my $self = shift->prototype;
Subroutine path: not called CC=3 SCAR=24.8
Pod documented
115 return @_
116 ? ($self->{ path } = ref $_[0] eq ARRAY ? shift : [ @_ ])
117 0 ▶ : $self->{ path };
Branch: 2 branches
BranchTrueFalseTotal
@_ ? : 0 0 0
ref $_[0] eq 'ARRAY' ? : 0 0 0
118 }
119  
120  
121 sub names {
122 1 ▶ my $self = shift->prototype;
Subroutine names: called CC=3 SCAR=11.2
Pod documented
123 1 my $names = $self->{ names };
124 1 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 0 1
125 1 ▶ my $args = ref $_[0] eq HASH ? shift : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 1 1
126 1 @$names{ keys %$args } = values %$args;
127 }
128 1 return $names;
129 }
130  
131  
132 sub module_names {
133 10 ▶ my $self = shift;
Subroutine module_names: called CC=1 SCAR=0.0
Pod documented
134 my @bits =
135 10 map { camel_case($_) }
136 10 map { split /[\.]+/ } @_;
137 10 my %seen;
138  
139 return (
140 20 grep { ! $seen{ $_ }++ }
141 10 join( PKG, map { ucfirst $_ } @bits ),
142 join( PKG, @bits )
143 );
144 }
145  
146  
147 sub modules {
148 0 ▶ my $self = shift->prototype;
Subroutine modules: not called CC=3 SCAR=24.8
Pod documented
149 0 my $items = $self->{ $self->{ items } };
150 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
151 # NOTE: this doesn't have any effect... it's a artefact from
152 # Badger::Factory... we need to change $self->{ loaded } to be
153 # $self->{ items }
154 0 ▶ my $args = ref $_[0] eq HASH ? shift : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 0 0
155 0 @$items{ keys %$args } = values %$args;
156 }
157 0 return $items;
158 }
159  
160  
161 sub module {
162 10 ▶ my $self = shift->prototype;
Subroutine module: called CC=12 SCAR=25.2
Pod documented
163 10 my $name = shift;
164 10 my $path = $self->{ path };
165 10 my $loaded = $self->{ loaded };
166 10 my ($module, $base, $alias, $found, $file, $symtab, @names, $size);
167
168 # Run the name through the name map to handle any unusual capitalisation,
169 # spelling, aliases, etc.
170 10 ▶ $name = $self->{ names }->{ $name } || $name;
Condition66%2/3
$self->{'names'}{$name} || $name
l!l&&r!l&&!r
280
MC/DC0%0/2
$self->{'names'}{$name} || $name
$self->{'names'}{$name} $name
Truth table66%2/3
A || B
A= $self->{'names'}{$name}B= $name
ABresult
1X1
011
000
171  
172 # Then expand the name using whatever rules are in effect (e.g. the
173 # default which maps foo_bar to FooBar)
174 # FIXME: probably shouldn't do this if we found an entry in the names lookup
175 10 @names = $self->module_names($name);
176
177 LOOKUP:
178 10 foreach $base (@$path) {
179 17 foreach $alias (@names) {
180 17 $module = join(PKG, $base, $alias);
181  
182 # TODO: look in $self->{ $items } for pre-defined result...
183
184 # See if we've previously loaded a module with this name (true
185 # value) or failed to load a module (defined but false value)
186 17 ▶ if ($found = $loaded->{ $module }) {
Branch: 2 branches
BranchTrueFalseTotal
if ($found = $loaded->{$module}) { } 0 17 17
elsif (defined $found) { } 0 17 17
187 0 $self->debug("$module has already been loaded") if DEBUG;
188 0 return $self->found( $name, $module );
189 }
190 elsif (defined $found) {
191 0 $self->debug("$module has previously been requested but not found") if DEBUG;
192 0 next;
193 }
194  
195 # Look to see if the module already has a symbol table defined
196 1 ▶ no strict REFS;
Subroutine BEGIN: called CC=1 SCAR=0.0
197 17 $symtab = \%{$module.PKG};
198  
199 # We have to be careful because symbols may be defined in a
200 # package's symbols table *before* the module is loaded (e.g. the
201 # $DEBUG package variable that Badger::Debug uses). So we only
202 # assume that the module is loaded if VERSION or BADGER_LOADED is
203 # defined
204 17 ▶ if ($symtab->{ VERSION } || $symtab->{ BADGER_LOADED }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($symtab->{"VERSION"} or $symtab->{"BADGER_LOADED"}) 3 14 17
Condition66%2/3
$symtab->{"VERSION"} or $symtab->{"BADGER_LOADED"}
l!l&&r!l&&!r
3014
MC/DC50%1/2
$symtab->{"VERSION"} or $symtab->{"BADGER_LOADED"}
$symtab->{"VERSION"} $symtab->{"BADGER_LOADED"}
Truth table66%2/3
A or B
A= $symtab->{"VERSION"}B= $symtab->{"BADGER_LOADED"}
ABresult
1X1
011
000
205 3 $self->debug("found an existing VERSION/BADGER_LOADED in $module symbol table") if DEBUG;
206 3 return $self->found( $name, $module );
207 }
208
209 14 $file = $module;
210 14 $file =~ s/::/\//g; # TODO: check Perl maps this to OS
211 14 $file .= '.pm';
212
213 14 $self->debug("Attempting to load $module as $file") if DEBUG;
214  
215 14 eval {
216 # We use eval so that we can "use" the module and force any
217 # import hooks to run. But it might be better to load the
218 # module with "require" and then manually call import()
219 14 require $file;
220 };
221  
222 14 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 9 5 14
223 9 $self->debug("Failed to load $module: $@") if DEBUG;
224 # Don't confuse "Can't locate A/Module/Used/In/Your/Module.pm"
225 # messages with "Can't locate Your/Module.pm". The former is
226 # an error that should be reported, the latter isn't. We convert the
227 # class name to a regex that matches any non-word directory
228 # separators, e.g. Your::Module => Your\W+Module
229 9 my $qmfile = quotemeta($file);
230 9 ▶ $self->failed($name, $module, $@) if $@ !~ /^Can't locate $qmfile.*? in \@INC/;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ =~ /^Can't locate $qmfile.*? in \@INC/ 1 8 9
231 8 next;
232 }
233  
234 # Some filesystems are case-insensitive (like Apple's HFS), so an
235 # attempt to load Badger::Example::foo may succeed, when the
236 # correct package name is actually Badger::Example::Foo. We
237 # double-check by looking to see if anything extra has been added
238 # to the symbol table.
239 5 $self->debug("$module symbol table keys: ", join(', ', keys %$symtab)) if DEBUG;
240 5 ▶ next unless %$symtab;
Branch: 1 branch
BranchTrueFalseTotal
unless %$symtab 0 5 5
241  
242 5 $self->debug("calling $module->import") if DEBUG;
243  
244 # now call the import() method to fire any import actions
245 5 $module->import;
246  
247 # add the $BADGER_LOADED package variable to indicate that the
248 # module has been loaded and add an entry to the internal cache
249 5 ▶ ${ $module.PKG.BADGER_LOADED } ||= 1;
Condition100%2/2
${$module . '::' . "BADGER_LOADED";} ||= 1
l!l
41
MC/DC100%1/1
${$module . '::' . "BADGER_LOADED";} ||= 1
${$module . '::' . "BADGER_LOADED";}
Truth table100%2/2
A
A= ${$module . '::' . "BADGER_LOADED";}
Aresult
11
00
250 5 $loaded->{ $module } = $module;
251  
252 5 return $self->found( $name, $module );
253 }
254 }
255  
256 # add entry to indicate module not found
257 1 $loaded->{ $name } = 0;
258
259 1 return $self->not_found($name);
260 }
261  
262  
263 sub found {
264 # my ($self, $name, $module) = @_;
265 8 ▶ return $_[2];
Subroutine found: called CC=1 SCAR=0.0
Pod documented
266 }
267  
268  
269 sub not_found {
270 1 ▶ my $self = shift;
Subroutine not_found: called CC=2 SCAR=7.2
Pod documented
271  
272 return $self->{ tolerant }
273 ? $self->decline_msg( not_found => $self->{ item } => @_ )
274 1 ▶ : $self->error_msg( not_found => $self->{ item } => @_ );
Branch: 1 branch
BranchTrueFalseTotal
$self->{'tolerant'} ? : 1 0 1
275 }
276  
277  
278 sub failed {
279 1 ▶ my $self = shift;
Subroutine failed: called CC=1 SCAR=0.0
Pod documented
280 1 $self->error_msg( failed => $self->{ item }, @_ );
281 }
282  
283
284  
285  
286 1 1;
287  
288 __END__
« lib/Badger/Data/Facet/Text.pm ↑ Summary lib/Badger/Filesystem/Path.pm »