blib/lib/UR/Namespace.pm untested

filter: Statementstmt 0.0% 0 / 114 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 15 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 202 SCAR 62.3
CC
22
Cov
0%
_get_class_names_under_namespace
240.0
import
20.0
get_material_classes
12.0
CRAP 506.0
1 package UR::Namespace;
2  
3 use strict;
4 use warnings;
5 use File::Find;
6  
7 require UR;
8 use UR::AttributeHandlers;
9 our $VERSION = "0.46"; # UR $VERSION;
10  
11 UR::Object::Type->define(
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 sub import {
38 my $class = shift;
39 return if ($class eq 'UR' or $class eq __PACKAGE__);
40  
41 my $calling_package;
42 for(my $i = 0; ($calling_package) = caller($i); $i++) {
43 last unless (substr($calling_package, 0, 4) eq 'UR::');
44 }
45 return unless $calling_package;
46 UR::AttributeHandlers::import_support_functions_to_package($calling_package);
47 }
48  
49 sub get_member_class {
50 my $self = shift;
51 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 sub get_material_classes {
57 my $self = shift->_singleton_object;
58 my @classes;
59 if (my $cached = $self->{material_classes}) {
60 @classes = map { UR::Object::Type->get($_) } @$cached;
61 }
62 else {
63 my @names;
64 for my $class_name ($self->_get_class_names_under_namespace()) {
65 my $class = eval { UR::Object::Type->get($class_name) };
66 next unless $class;
67 push @classes, $class;
68 push @names, $class_name;
69 }
70 $self->{material_classes} = \@names;
71 }
72 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 sub should_dynamically_load_class {
79 # my($self,$class_name) = @_;
80 return 1;
81 }
82  
83  
84 sub get_material_class_names
85 {
86 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 {
92 my $class = shift;
93 if ($class eq 'UR' or (ref($class) and $class->id eq 'UR')) {
94 return 'UR::DataSource::Meta'; # UR only has 1 "real" data source, the other stuff in that dir are base classes
95  
96 } else {
97 my %found;
98 my $namespace_name = $class->class;
99  
100 foreach my $inc ( @main::INC ) {
101 my $path = join('/', $inc,$namespace_name,'DataSource');
102 if (-d $path) {
103 foreach ( glob($path . '/*.pm') ) {
104 my($module_name) = m/DataSource\/([^\/]+)\.pm$/;
105 my $ds_class_name = $namespace_name . '::DataSource::' . $module_name;
106 $found{$ds_class_name} = 1;
107 }
108 }
109 }
110 my @data_sources = map { $_->get() } keys(%found);
111 return @data_sources;
112 }
113 }
114  
115 sub get_base_contexts
116 {
117 return shift->_get_class_names_under_namespace("Context");
118 }
119  
120 sub get_vocabulary
121 {
122 my $class = shift->_singleton_class_name;
123 return $class . "::Vocabulary";
124 }
125  
126 sub get_base_directory_name
127 {
128 my $class = shift->_singleton_class_name;
129 my $dir = $class->__meta__->module_path;
130 $dir =~ s/\.pm$//;
131 return $dir;
132 }
133  
134 sub get_deleted_module_directory_name
135 {
136 my $self = shift;
137 my $meta = $self->__meta__;
138 my $path = $meta->module_path;
139 $path =~ s/.pm$//g;
140 $path .= "/.deleted";
141 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 {
150 my $class = shift->_singleton_class_name;
151 my $subdir = shift;
152  
153 Carp::confess if ref($class);
154  
155 my $dir = $class->get_base_directory_name;
156  
157 my $namespace_dir;
158 if (defined($subdir) and length($subdir)) {
159 $namespace_dir = join("/",$dir, $subdir);
160 }
161 else {
162 $namespace_dir = $dir;
163 }
164  
165 my $namespace = $class;
166 my %class_names;
167  
168 my $preprocess = sub {
169 if ($File::Find::dir =~ m/\/t$/) {
170 return ();
171 } elsif (-e ($File::Find::dir . "/UR_IGNORE")) {
172 return ();
173 } else {
174 return @_;
175 }
176 };
177  
178 my $wanted = sub {
179 return if -d $File::Find::name; # not interested in directories
180 return if $File::Find::name =~ /\/\.deleted\//; # .deleted directories are created by ur update classes
181 return if -e $File::Find::name . '/UR_IGNORE'; # ignore a whole directory?
182 return unless $File::Find::name =~ m/\.pm$/; # must be a perl module
183 return unless $File::Find::name =~ m/.*($namespace\/.*)\.pm/;
184  
185 my $try_class = $1;
186 return if $try_class =~ m([^\w/]); # Skip names that make for illegal package names. Must be word chars or a /
187 $try_class =~ s/\//::/g;
188 $class_names{$try_class} = 1 if $try_class;
189 };
190  
191 my @dirs_to_search = @INC;
192 my $path_to_check = $namespace;
193 $path_to_check .= "/$subdir" if $subdir;
194  
195 @dirs_to_search = map($_ . '/' . $path_to_check, @dirs_to_search); # only look in places with namespace_name as a subdir
196 unshift(@dirs_to_search, $namespace_dir) if (-d $namespace_dir);
197  
198 @dirs_to_search = grep { $_ =~ m/\/$path_to_check/ and -d $_ }
199 @dirs_to_search;
200 return unless @dirs_to_search;
201 find({ wanted => $wanted, preprocess => $preprocess }, @dirs_to_search);
202 return sort keys %class_names;
203 }
204  
205 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