lib/UR/Namespace.pm

filter: Statementstmt 15.2% 16 / 105 Branchbran 4.1% 2 / 48 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 23.5% 4 / 17 Podpod 44.4% 4 / 9 totaltotal 12.8% 26 / 203 CC 31 SCAR 37.2
CC
31
Cov
11%
_get_class_names_under_namespace
72.0
__ANON__
72.0
get_data_sources
56.0
CRAP 41.1
« lib/UR/ModuleConfig.pm ↑ Summary lib/UR/Util.pm »
1 package UR::Namespace;
2  
3 266 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 266 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 266 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 266 require UR;
8 266 ▶use UR::AttributeHandlers;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 266 our $VERSION = "0.47"; # UR $VERSION;
10  
11 266 ▶UR::Object::Type->define(
Branch: 2 branches
BranchTrueFalseTotal
$^V lt v5.9.5 ? : 0 266 266
$^V lt v5.9.5 ? : 0 266 266
12 class_name => 'UR::Namespace',
13 is => ['UR::Singleton'],
14 is_abstract => 1,
15 has => [
16 domain => {
17 is => 'Text',
18 is_optional => 1,
19 len => undef,
20 doc => "DNS domain name associated with the namespace in question",
21 },
22 allow_sloppy_primitives => {
23 is => 'Boolean',
24 default_value => 1,
25 doc => 'when true, unrecognized data types will function as UR::Value::SloppyPrimitive'
26 },
27 method_resolution_order => {
28 is => 'Text',
29 value => ($^V lt v5.9.5 ? 'dfs' : 'c3'),
30 valid_values => ($^V lt v5.9.5 ? ['dfs'] : ['dfs', 'c3']),
31 doc => 'Method Resolution Order to use for this namespace. C3 is only supported in Perl >= 5.9.5.',
32 },
33 ],
34 doc => 'The class for a singleton module representing a UR namespace.',
35 );
36  
37 0 ▶sub import {
Subroutine import: not called CC=6 SCAR=37.4
38 0 my $class = shift;
39 0 ▶ return if ($class eq 'UR' or $class eq __PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
if $class eq "UR" or $class eq __PACKAGE__ 0 0 0
Condition0%0/3
$class eq "UR" or $class eq __PACKAGE__
l!l&&r!l&&!r
000
MC/DC0%0/2
$class eq "UR" or $class eq __PACKAGE__
$class eq "UR" $class eq __PACKAGE__
Truth table0%0/3
A or B
A= $class eq "UR"B= $class eq __PACKAGE__
ABresult
1X1
011
000
40  
41 0 my $calling_package;
42 0 ▶ for(my $i = 0; ($calling_package) = caller($i); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if (($calling_package) = caller $i) 0 0 0
43 0 ▶ last unless (substr($calling_package, 0, 4) eq 'UR::');
Branch: 1 branch
BranchTrueFalseTotal
unless substr($calling_package, 0, 4) eq "UR::" 0 0 0
44 }
45 0 ▶ return unless $calling_package;
Branch: 1 branch
BranchTrueFalseTotal
unless $calling_package 0 0 0
46 0 UR::AttributeHandlers::import_support_functions_to_package($calling_package);
47 }
48  
49 0 ▶sub get_member_class {
Subroutine get_member_class: not called CC=1 SCAR=6.9
Pod undocumented
50 0 my $self = shift;
51 0 return UR::Object::Type->get(@_);
52 }
53  
54 # FIXME These should change to using the namespace metadata DB when
55 # that's in place, rather than trolling through the directory tree
56 0 ▶sub get_material_classes {
Subroutine get_material_classes: not called CC=4 SCAR=30.0
Pod documented
57 0 my $self = shift->_singleton_object;
58 0 my @classes;
59 0 ▶ if (my $cached = $self->{material_classes}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cached = $self->{'material_classes'}) { } 0 0 0
60 0 @classes = map { UR::Object::Type->get($_) } @$cached;
61 }
62 else {
63 0 my @names;
64 0 for my $class_name ($self->_get_class_names_under_namespace()) {
65 0 my $class = eval { UR::Object::Type->get($class_name) };
66 0 ▶ next unless $class;
Branch: 1 branch
BranchTrueFalseTotal
unless $class 0 0 0
67 0 push @classes, $class;
68 0 push @names, $class_name;
69 }
70 0 $self->{material_classes} = \@names;
71 }
72 0 return @classes;
73 }
74  
75 # Subclasses can override this method to tell the dynamic module loader
76 # whether it should go ahead and load the given module name or not.
77 # The default behavior is to go ahead and try for them all
78 0 ▶sub should_dynamically_load_class {
Subroutine should_dynamically_load_class: not called CC=1 SCAR=6.9
Pod undocumented
79 # my($self,$class_name) = @_;
80 0 return 1;
81 }
82  
83  
84 sub get_material_class_names
85 0 ▶{
Subroutine get_material_class_names: not called CC=1 SCAR=6.9
Pod documented
86 0 return map {$_->class_name} $_[0]->get_material_classes();
87 }
88  
89 # Returns data source objects for all the data sources of the namespace
90 sub get_data_sources
91 0 ▶{
Subroutine get_data_sources: not called CC=7 SCAR=40.3
Pod documented
92 0 my $class = shift;
93 0 ▶ if ($class eq 'UR' or (ref($class) and $class->id eq 'UR')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq 'UR' or ref $class and $class->id eq 'UR') { } 0 0 0
Condition0%0/6
$class eq "UR" or ref $class and $class->id eq "UR"
l!l&&r!l&&!r
000
ref $class and $class->id eq "UR"
!ll&&!rl&&r
000
MC/DC0%0/3
$class eq "UR" or ref $class and $class->id eq "UR"
$class eq "UR" ref $class $class->id eq "UR"
Truth table0%0/4
A or B and C
A= $class eq "UR"B= ref $classC= $class->id eq "UR"
ABCresult
1XX1
0111
00X0
0100
94 0 return 'UR::DataSource::Meta'; # UR only has 1 "real" data source, the other stuff in that dir are base classes
95  
96 } else {
97 0 my %found;
98 0 my $namespace_name = $class->class;
99  
100 0 foreach my $inc ( @main::INC ) {
101 0 my $path = join('/', $inc,$namespace_name,'DataSource');
102 0 ▶ if (-d $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $path) 0 0 0
103 0 foreach ( glob("\Q${path}\E/*.pm") ) {
104 0 my($module_name) = m/DataSource\/([^\/]+)\.pm$/;
105 0 my $ds_class_name = $namespace_name . '::DataSource::' . $module_name;
106 0 $found{$ds_class_name} = 1;
107 }
108 }
109 }
110 0 my @data_sources = map { $_->get() } keys(%found);
111 0 return @data_sources;
112 }
113 }
114  
115 sub get_base_contexts
116 0 ▶{
Subroutine get_base_contexts: not called CC=1 SCAR=6.9
Pod undocumented
117 0 return shift->_get_class_names_under_namespace("Context");
118 }
119  
120 sub get_vocabulary
121 0 ▶{
Subroutine get_vocabulary: not called CC=1 SCAR=6.9
Pod undocumented
122 0 my $class = shift->_singleton_class_name;
123 0 return $class . "::Vocabulary";
124 }
125  
126 sub get_base_directory_name
127 0 ▶{
Subroutine get_base_directory_name: not called CC=1 SCAR=6.9
Pod documented
128 0 my $class = shift->_singleton_class_name;
129 0 my $dir = $class->__meta__->module_path;
130 0 $dir =~ s/\.pm$//;
131 0 return $dir;
132 }
133  
134 sub get_deleted_module_directory_name
135 0 ▶{
Subroutine get_deleted_module_directory_name: not called CC=1 SCAR=6.9
Pod undocumented
136 0 my $self = shift;
137 0 my $meta = $self->__meta__;
138 0 my $path = $meta->module_path;
139 0 $path =~ s/.pm$//g;
140 0 $path .= "/.deleted";
141 0 return $path;
142 }
143  
144 # FIXME This is misnamed...
145 # It really returns all the package names under the specified directory
146 # (assumming the packages defined in the found files are named like the
147 # pathname of the file), not just those that implement classes
148 sub _get_class_names_under_namespace
149 0 ▶{
Subroutine _get_class_names_under_namespace: not called CC=8 SCAR=42.8
150 0 my $class = shift->_singleton_class_name;
151 0 my $subdir = shift;
152  
153 0 ▶ Carp::confess if ref($class);
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 0 0
154  
155 0 my $dir = $class->get_base_directory_name;
156  
157 0 my $namespace_dir;
158 0 ▶ if (defined($subdir) and length($subdir)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $subdir and length $subdir) { } 0 0 0
Condition0%0/3
defined $subdir and length $subdir
!ll&&!rl&&r
000
MC/DC0%0/2
defined $subdir and length $subdir
defined $subdir length $subdir
Truth table0%0/3
A and B
A= defined $subdirB= length $subdir
ABresult
0X0
100
111
159 0 $namespace_dir = join("/",$dir, $subdir);
160 }
161 else {
162 0 $namespace_dir = $dir;
163 }
164  
165 0 my $namespace = $class;
166 0 my %class_names;
167  
168 0 ▶ my $preprocess = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
169 0 ▶ if ($File::Find::dir =~ m/\/t$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($File::Find::dir =~ m[/t$]) { } 0 0 0
elsif (-e $File::Find::dir . '/UR_IGNORE') { } 0 0 0
170 0 return ();
171 } elsif (-e ($File::Find::dir . "/UR_IGNORE")) {
172 0 return ();
173 } else {
174 0 return @_;
175 }
176 0 };
177  
178 0 ▶ my $wanted = sub {
Subroutine __ANON__: not called CC=8 SCAR=42.8
179 0 ▶ return if -d $File::Find::name; # not interested in directories
Branch: 1 branch
BranchTrueFalseTotal
if -d $File::Find::name 0 0 0
180 0 ▶ return if $File::Find::name =~ /\/\.deleted\//; # .deleted directories are created by ur update classes
Branch: 1 branch
BranchTrueFalseTotal
if $File::Find::name =~ m[/\.deleted/] 0 0 0
181 0 ▶ return if -e $File::Find::name . '/UR_IGNORE'; # ignore a whole directory?
Branch: 1 branch
BranchTrueFalseTotal
if -e $File::Find::name . '/UR_IGNORE' 0 0 0
182 0 ▶ return unless $File::Find::name =~ m/\.pm$/; # must be a perl module
Branch: 1 branch
BranchTrueFalseTotal
unless $File::Find::name =~ /\.pm$/ 0 0 0
183 0 ▶ return unless $File::Find::name =~ m/.*($namespace\/.*)\.pm/;
Branch: 1 branch
BranchTrueFalseTotal
unless $File::Find::name =~ m[.*($namespace/.*)\.pm] 0 0 0
184  
185 0 my $try_class = $1;
186 0 ▶ return if $try_class =~ m([^\w/]); # Skip names that make for illegal package names. Must be word chars or a /
Branch: 1 branch
BranchTrueFalseTotal
if $try_class =~ m[[^\w/]] 0 0 0
187 0 $try_class =~ s/\//::/g;
188 0 ▶ $class_names{$try_class} = 1 if $try_class;
Branch: 1 branch
BranchTrueFalseTotal
if $try_class 0 0 0
189 0 };
190  
191 0 my @dirs_to_search = @INC;
192 0 my $path_to_check = $namespace;
193 0 ▶ $path_to_check .= "/$subdir" if $subdir;
Branch: 1 branch
BranchTrueFalseTotal
if $subdir 0 0 0
194  
195 0 @dirs_to_search = map($_ . '/' . $path_to_check, @dirs_to_search); # only look in places with namespace_name as a subdir
196 0 ▶ unshift(@dirs_to_search, $namespace_dir) if (-d $namespace_dir);
Branch: 1 branch
BranchTrueFalseTotal
if -d $namespace_dir 0 0 0
197  
198 0 ▶ @dirs_to_search = grep { $_ =~ m/\/$path_to_check/ and -d $_ }
Condition0%0/3
$_ =~ m[/$path_to_check] && -d $_
!ll&&!rl&&r
000
MC/DC0%0/2
$_ =~ m[/$path_to_check] && -d $_
$_ =~ m[/$path_to_check] -d $_
Truth table0%0/3
A && B
A= $_ =~ m[/$path_to_check]B= -d $_
ABresult
0X0
100
111
199 @dirs_to_search;
200 0 ▶ return unless @dirs_to_search;
Branch: 1 branch
BranchTrueFalseTotal
unless @dirs_to_search 0 0 0
201 0 find({ wanted => $wanted, preprocess => $preprocess }, @dirs_to_search);
202 0 return sort keys %class_names;
203 }
204  
205 266 1;
206  
207  
208 =pod
209  
210 =head1 NAME
211  
212 UR::Namespace - Manage collections of packages and classes
213  
214 =head1 SYNOPSIS
215  
216 In a file called MyApp.pm:
217  
218 use UR;
219 UR::Object::Type->define(
220 class_name => 'MyApp',
221 is => 'UR::Namespace',
222 );
223  
224 Other programs, as well as modules in the MyApp subdirectory can now put
225  
226 use MyApp;
227  
228 in their code, and they will have access to all the classes and data under
229 the MyApp tree.
230  
231 =head1 DESCRIPTION
232  
233 A UR namespace is the top-level object that represents your data's class
234 structure in the most general way. After use-ing a namespace module, the
235 program gets access to the module autoloader, which will automatically use
236 modules on your behalf if you attempt to interact with their packages in
237 a UR-y way, such as calling get().
238  
239 Most programs will not interact with the Namespace, except to C<use> its
240 package.
241  
242 =head1 Methods
243  
244 =over 4
245  
246 =item get_material_classes
247  
248 my @class_metas = $namespace->get_material_classes();
249  
250 Return a list of L<UR::Object::Type> class metadata object that exist in
251 the given Namespace. Note that this uses File::Find to find C<*.pm> files
252 under the Namespace directory and calls C<UR::Object::Type-E<gt>get($name)>
253 for each package name to get the autoloader to use the package. It's likely
254 to be pretty slow.
255  
256 =item get_material_class_names
257  
258 my @class_names = $namespace->get_material_class_names()
259  
260 Return just the names of the classes produced by C<get_material_classes>.
261  
262 =item get_data_sources
263  
264 my @data_sources = $namespace->get_data_sources()
265  
266 Return the data source objects it finds defined under the DataSource
267 subdirectory of the namespace.
268  
269 =item get_base_directory_name
270  
271 my $path = $namespace->get_base_directory_name()
272  
273 Returns the directory path where the Namespace module was loaded from.
274  
275 =back
276  
277 =head1 SEE ALSO
278  
279 L<UR::Object::Type>, L<UR::DataSource>, L<UR::Context>
280  
281 =cut
« lib/UR/ModuleConfig.pm ↑ Summary lib/UR/Util.pm »