lib/UR/Object/Type/ModuleWriter.pm

filter: Statementstmt 8.1% 40 / 492 Branchbran 0.0% 0 / 258 Conditioncond 0.0% 0 / 85 MC/DCmcdc 0.0% 0 / 50 Subroutinesub 25.7% 9 / 35 Podpod 0.0% 0 / 15 totaltotal 5.2% 49 / 935 CC 173 SCAR 77.6
CC
173
Cov
5%
_get_display_fields_for_property
3906.0
resolve_class_description_perl
3652.3
rewrite_module_header
429.7
CRAP 2340.5
« blib/lib/UR/DataSource/File.pm ↑ Summary blib/lib/UR/DataSource/Filesystem.pm »
1 package UR::Object::Type::ModuleWriter; # to help the installer
2  
3 package UR::Object::Type; # hold methods for the class which cover Module Read/Write.
4  
5 266 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 266 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 266 require UR;
8 266 ▶use List::MoreUtils qw(first_index);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 266 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 266 our $VERSION = "0.47"; # UR $VERSION;
12  
13 266 our %meta_classes;
14 266 our $bootstrapping = 1;
15 266 our @partially_defined_classes;
16 266 our $pwd_at_compile_time = cwd();
17  
18 0 ▶sub resolve_class_description_perl {
Subroutine resolve_class_description_perl: not called CC=63 SCAR=82.0
Pod undocumented
19 0 my $self = $_[0];
20  
21 266 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 0 my @isa = @{ $self->class_name . "::ISA" };
23 266 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 0 ▶ unless (@isa) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@isa) 0 0 0
26 0 my @i = ${ $self->is };
27 0 my @parent_class_objects = map { UR::Object::Type->is_loaded(class_name => $_) } @i;
28 0 ▶ die "Parent class objects not all loaded for " . $self->class_name unless (@i == @parent_class_objects);
Branch: 1 branch
BranchTrueFalseTotal
unless @i == @parent_class_objects 0 0 0
29 0 @isa = map { $_->class_name } @parent_class_objects;
30 }
31  
32 0 ▶ unless (@isa) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@isa) 0 0 0
33 0 my @i = ${ $self->is };
34 0 my @parent_class_objects = map { UR::Object::Type->is_loaded(class_name => $_) } @i;
35  
36 0 ▶ unless (@i and @i == @parent_class_objects) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@i and @i == @parent_class_objects) 0 0 0
Condition0%0/3
@i and @i == @parent_class_objects
!ll&&!rl&&r
000
MC/DC0%0/2
@i and @i == @parent_class_objects
@i @i == @parent_class_objects
Truth table0%0/3
A and B
A= @iB= @i == @parent_class_objects
ABresult
0X0
100
111
37 0 Carp::confess("No inheritance meta-data found for ( @i / @parent_class_objects)" . $self->class_name)
38 }
39  
40 0 @isa = map { $_->class_name } @parent_class_objects;
41 }
42  
43 0 my $class_name = $self->class_name;
44 0 my @parent_classes = $self->parent_class_metas;
45 0 my $has_table = $self->has_table;
46  
47 # For getting default values for some of the properties
48 0 my $class_meta_meta = UR::Object::Type->get(class_name => 'UR::Object::Type');
49  
50 0 my $perl = '';
51  
52 0 ▶ unless (@isa == 1 and $isa[0] =~ /^UR::Object|UR::Entity$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@isa == 1 and $isa[0] =~ /^UR::Object|UR::Entity$/) 0 0 0
Condition0%0/3
@isa == 1 and $isa[0] =~ /^UR::Object|UR::Entity$/
!ll&&!rl&&r
000
MC/DC0%0/2
@isa == 1 and $isa[0] =~ /^UR::Object|UR::Entity$/
@isa == 1 $isa[0] =~ /^UR::Object|UR::Entity$/
Truth table0%0/3
A and B
A= @isa == 1B= $isa[0] =~ /^UR::Object|UR::Entity$/
ABresult
0X0
100
111
53 0 ▶ $perl .= " is => " . (@isa == 1 ? "'@isa',\n" : pprint_arrayref(\@isa) . ",\n");
Branch: 1 branch
BranchTrueFalseTotal
@isa == 1 ? : 0 0 0
54 }
55 0 ▶ $perl .= " table_name => " . ($self->table_name ? "'" . $self->table_name . "'" : 'undef') . ",\n" if $self->data_source_id;
Branch: 2 branches
BranchTrueFalseTotal
if $self->data_source_id 0 0 0
$self->table_name ? : 0 0 0
56 0 ▶ $perl .= " is_abstract => 1,\n" if $self->is_abstract;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_abstract 0 0 0
57 0 ▶ $perl .= " er_role => '" . $self->er_role . "',\n" if ($self->er_role and ($self->er_role ne $class_meta_meta->property_meta_for_name('er_role')->default_value));
Branch: 1 branch
BranchTrueFalseTotal
if $self->er_role and $self->er_role ne $class_meta_meta->property_meta_for_name("er_role")->default_value 0 0 0
Condition0%0/3
$self->er_role and $self->er_role ne $class_meta_meta->property_meta_for_name("er_role")->default_value
!ll&&!rl&&r
000
MC/DC0%0/2
$self->er_role and $self->er_role ne $class_meta_meta->property_meta_for_name("er_role")->default_value
$self->er_role $self->er_role ne $class_meta_meta->property_meta_for_name("er_role")->default_value
Truth table0%0/3
A and B
A= $self->er_roleB= $self->er_role ne $class_meta_meta->property_meta_for_name("er_role")->default_value
ABresult
0X0
100
111
58  
59 0 ▶ if ($self->{type_has}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"type_has"}) 0 0 0
60 0 my @keys = qw(is_optional is);
61 0 my %type_has = @{$self->{type_has}};
62 0 my @type_has_names = keys %type_has;
63 0 my $section_src;
64 0 for my $name (@type_has_names) {
65 0 my $struct = $type_has{$name};
66 0 $section_src .= pprint_subsection($name, _section_lines($struct, @keys));
67 }
68 0 ▶ if ($section_src) {
Branch: 1 branch
BranchTrueFalseTotal
if ($section_src) 0 0 0
69 0 $perl .= pprint_section('type_has', $section_src);
70 }
71 }
72  
73  
74 # Meta-property attributes
75 0 my @property_meta_property_names;
76 0 ▶ if ($self->{'attributes_have'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"attributes_have"}) 0 0 0
77 @property_meta_property_names = sort { $self->{'attributes_have'}->{$a}->{'position_in_module_header'}
78 <=>
79 0 $self->{'attributes_have'}->{$b}->{'position_in_module_header'} }
80 0 keys %{$self->{'attributes_have'}};
81 0 my $section_src = '';
82 0 foreach my $meta_name ( @property_meta_property_names ) {
83 0 my $this_meta_struct = $self->{'attributes_have'}->{$meta_name};
84  
85 # The attributes_have structure gets propogated to subclasses, but it only needs to appear
86 # in the class definition of the most-parent class
87 0 my $expected_name = $class_name . '::attributes_have';
88 0 ▶ next unless ( $this_meta_struct->{'is_specified_in_module_header'} eq $expected_name);
Branch: 1 branch
BranchTrueFalseTotal
unless $this_meta_struct->{"is_specified_in_module_header"} eq $expected_name 0 0 0
89  
90 # We want these to appear first
91 0 my @this_meta_properties;
92 # skip the ones we've already done
93 0 my @exclude_keys = qw(is_specified_in_module_header position_in_module_header);
94 0 my @keys = _exclude_items([keys %$this_meta_struct], \@exclude_keys);
95  
96 0 $section_src .= pprint_subsection($meta_name, _section_lines($this_meta_struct, @keys));
97 }
98 0 ▶ if ($section_src) {
Branch: 1 branch
BranchTrueFalseTotal
if ($section_src) 0 0 0
99 0 $perl .= pprint_section('attributes_have', $section_src);
100 }
101 }
102  
103 0 ▶ if (exists $self->{'first_sub_classification_method_name'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"first_sub_classification_method_name"}) 0 0 0
104 # This gets overridden by UR::Object::Type to cache the value it finds from parent
105 # classes in __first_sub_classification_method_name, so we can't just get the
106 # property through the normal channels
107 0 $perl .= " first_sub_classification_method_name => '" . $self->{'first_sub_classification_method_name'} ."',\n";
108 }
109  
110 # These property names are either written in other places in this sub, or shouldn't be written out
111 0 my %addl_property_names = map { $_ => 1 } $self->__meta__->all_property_type_names;
112 0 my @specified = qw/is class_name table_name id_by er_role is_abstract generated data_source_id schema_name doc namespace id first_sub_classification_method_name property_metas pproperty_names id_property_metas meta_class_name id_generator valid_signals roles/;
113 0 delete @addl_property_names{@specified};
114 0 for my $property_name (sort keys %addl_property_names) {
115 0 my $property_obj = $self->__meta__->property_meta_for_name($property_name);
116 0 ▶ next if ($property_obj->is_calculated or $property_obj->is_delegated);
Branch: 1 branch
BranchTrueFalseTotal
if $property_obj->is_calculated or $property_obj->is_delegated 0 0 0
Condition0%0/3
$property_obj->is_calculated or $property_obj->is_delegated
l!l&&r!l&&!r
000
MC/DC0%0/2
$property_obj->is_calculated or $property_obj->is_delegated
$property_obj->is_calculated $property_obj->is_delegated
Truth table0%0/3
A or B
A= $property_obj->is_calculatedB= $property_obj->is_delegated
ABresult
1X1
011
000
117  
118 0 my $property_value = $self->$property_name;
119 0 ▶ my $default_value = $property_obj && $property_obj->default_value;
Condition0%0/3
$property_obj && $property_obj->default_value
!ll&&!rl&&r
000
MC/DC0%0/2
$property_obj && $property_obj->default_value
$property_obj $property_obj->default_value
Truth table0%0/3
A && B
A= $property_objB= $property_obj->default_value
ABresult
0X0
100
111
120 # If the property is set on the class object
121 # and both the value and default are numeric and numerically different,
122 # or stringly different than the default
123 266 ▶ no warnings qw( numeric uninitialized );
Subroutine BEGIN: called CC=1 SCAR=0.0
124 0 ▶ if ( defined $property_value and
Branch: 1 branch
BranchTrueFalseTotal
if (defined $property_value and $property_value + 0 eq $property_value && $default_value + 0 eq $default_value && $property_value != $default_value || $property_value ne $default_value) 0 0 0
Condition0%0/12
defined $property_value and $property_value + 0 eq $property_value && $default_value + 0 eq $default_value && $property_value != $default_value || $property_value ne $default_value
!ll&&!rl&&r
000
$property_value + 0 eq $property_value and $default_value + 0 eq $default_value and $property_value != $default_value or $property_value ne $default_value
l!l&&r!l&&!r
000
$property_value + 0 eq $property_value and $default_value + 0 eq $default_value and $property_value != $default_value
!ll&&!rl&&r
000
$property_value + 0 eq $property_value and $default_value + 0 eq $default_value
!ll&&!rl&&r
000
MC/DC0%0/5
defined $property_value and $property_value + 0 eq $property_value && $default_value + 0 eq $default_value && $property_value != $default_value || $property_value ne $default_value
defined $property_value $property_value + 0 eq $property_value $default_value + 0 eq $default_value $property_value != $default_value $property_value ne $default_value
Truth table0%0/8
A and B and C and D or E
A= defined $property_valueB= $property_value + 0 eq $property_valueC= $default_value + 0 eq $default_valueD= $property_value != $default_valueE= $property_value ne $default_value
ABCDEresult
0XXXX0
10XX00
110X00
111000
1111X1
10XX11
110X11
111011
125 ( ($property_value + 0 eq $property_value and
126 $default_value + 0 eq $default_value and
127 $property_value != $default_value)
128 or
129 ($property_value ne $default_value)
130 )
131 ) {
132 # then it should show up in the class definition
133 0 my $value = $self->$property_name;
134 0 ▶ if (ref $value eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 0 0 0
135 0 $value = pprint_arrayref($value);
136 } else {
137 0 $value = qq('$value');
138 }
139 0 $perl .= " $property_name => $value,\n";
140 }
141 }
142  
143 0 my %properties_by_section;
144 0 my %id_property_names = map { $_ => 1 } $self->direct_id_property_names;
145 0 my @properties = $self->direct_property_metas;
146 0 foreach my $property_meta ( @properties ) {
147 0 my $mentioned_section = $property_meta->is_specified_in_module_header;
148 0 ▶ next unless $mentioned_section; # skip implied properites
Branch: 1 branch
BranchTrueFalseTotal
unless $mentioned_section 0 0 0
149 0 ($mentioned_section) = ($mentioned_section =~ m/::(\w+)$/);
150  
151 0 ▶ if (($mentioned_section and $mentioned_section eq 'id_implied')
Branch: 2 branches
BranchTrueFalseTotal
if ($mentioned_section and $mentioned_section eq 'id_implied' or $id_property_names{$property_meta->property_name}) { } 0 0 0
elsif ($mentioned_section) { } 0 0 0
Condition0%0/6
$mentioned_section and $mentioned_section eq "id_implied" or $id_property_names{$property_meta->property_name}
l!l&&r!l&&!r
000
$mentioned_section and $mentioned_section eq "id_implied"
!ll&&!rl&&r
000
MC/DC0%0/3
$mentioned_section and $mentioned_section eq "id_implied" or $id_property_names{$property_meta->property_name}
$mentioned_section $mentioned_section eq "id_implied" $id_property_names{$property_meta->property_name}
Truth table0%0/5
A and B or C
A= $mentioned_sectionB= $mentioned_section eq "id_implied"C= $id_property_names{$property_meta->property_name}
ABCresult
11X1
0X11
1011
0X00
1000
152 or $id_property_names{$property_meta->property_name}) {
153  
154 0 push @{$properties_by_section{'id_by'}}, $property_meta;
155  
156 } elsif ($mentioned_section) {
157 0 push @{$properties_by_section{$mentioned_section}}, $property_meta;
158  
159 } else {
160 0 push @{$properties_by_section{'has'}}, $property_meta;
161 }
162 }
163  
164 0 my %sections_seen;
165 0 my $data_source_id = $self->data_source_id;
166 0 ▶ my ($data_source) = ($data_source_id ? UR::DataSource->get($data_source_id) : undef);
Branch: 1 branch
BranchTrueFalseTotal
$data_source_id ? : 0 0 0
167 0 foreach my $section ( ( 'id_by', 'has', 'has_many', 'has_optional', keys(%properties_by_section) ) ) {
168 0 ▶ next unless ($properties_by_section{$section});
Branch: 1 branch
BranchTrueFalseTotal
unless $properties_by_section{$section} 0 0 0
169 0 ▶ next if ($sections_seen{$section});
Branch: 1 branch
BranchTrueFalseTotal
if $sections_seen{$section} 0 0 0
170 0 $sections_seen{$section} = 1;
171  
172 # New properites (will have position_in_module_header == undef) should go at the end
173 my @properties = sort { my $pos_a = defined($a->{'position_in_module_header'})
174 0 ▶ ? $a->{'position_in_module_header'}
Branch: 1 branch
BranchTrueFalseTotal
defined $UR::Object::Type::a->{'position_in_module_header'} ? : 0 0 0
175 : 1000000;
176 my $pos_b = defined($b->{'position_in_module_header'})
177 0 ▶ ? $b->{'position_in_module_header'}
Branch: 1 branch
BranchTrueFalseTotal
defined $UR::Object::Type::b->{'position_in_module_header'} ? : 0 0 0
178 : 1000000;
179 0 $pos_a <=> $pos_b;
180 }
181 0 @{$properties_by_section{$section}};
182  
183 0 my $section_src = '';
184 0 foreach my $property_meta ( @properties ) {
185 0 my $name = $property_meta->property_name;
186 0 my @fields = $self->_get_display_fields_for_property(
187 $property_meta,
188 has_table => $has_table,
189 section => $section,
190 data_source => $data_source,
191 attributes_have => \@property_meta_property_names);
192  
193 0 $section_src .= pprint_subsection($name, @fields);
194 }
195  
196 0 $perl .= pprint_section($section, $section_src);
197 }
198  
199 0 my $unique_groups = $self->unique_property_set_hashref;
200 0 ▶ if ($unique_groups and keys %$unique_groups) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unique_groups and keys %$unique_groups) 0 0 0
Condition0%0/3
$unique_groups and keys %$unique_groups
!ll&&!rl&&r
000
MC/DC0%0/2
$unique_groups and keys %$unique_groups
$unique_groups keys %$unique_groups
Truth table0%0/3
A and B
A= $unique_groupsB= keys %$unique_groups
ABresult
0X0
100
111
201  
202 0 $perl .= " unique_constraints => [\n";
203 0 for my $unique_group_name (keys %$unique_groups) {
204 0 my $property_names = join(' ', sort { $a cmp $b } @{ $unique_groups->{$unique_group_name}});
205 0 $perl .= " { "
206 . "properties => [qw/$property_names/], "
207 . "sql => '" . $unique_group_name . "'"
208 . " },\n";
209 }
210 0 $perl .= " ],\n";
211 }
212  
213 0 ▶ $perl .= " schema_name => '" . $self->schema_name . "',\n" if $self->schema_name;
Branch: 1 branch
BranchTrueFalseTotal
if $self->schema_name 0 0 0
214 0 ▶ $perl .= " data_source => '" . $self->data_source_id . "',\n" if $self->data_source_id;
Branch: 1 branch
BranchTrueFalseTotal
if $self->data_source_id 0 0 0
215  
216 0 my $print_id_generator;
217 0 ▶ if (my $id_generator = $self->id_generator) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $id_generator = $self->id_generator) 0 0 0
218 0 ▶ if ($self->data_source_id and $id_generator eq '-urinternal') {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->data_source_id and $id_generator eq '-urinternal') { } 0 0 0
elsif (not $self->data_source_id and $id_generator eq '-urinternal') { } 0 0 0
Condition0%0/3
$self->data_source_id and $id_generator eq "-urinternal"
!ll&&!rl&&r
000
MC/DC0%0/2
$self->data_source_id and $id_generator eq "-urinternal"
$self->data_source_id $id_generator eq "-urinternal"
Truth table0%0/3
A and B
A= $self->data_source_idB= $id_generator eq "-urinternal"
ABresult
0X0
100
111
219 0 $print_id_generator = 1;
220 ▶ } elsif (! $self->data_source_id and $id_generator eq '-urinternal') {
Condition0%0/3
not $self->data_source_id and $id_generator eq "-urinternal"
!ll&&!rl&&r
000
MC/DC0%0/2
not $self->data_source_id and $id_generator eq "-urinternal"
not $self->data_source_id $id_generator eq "-urinternal"
Truth table0%0/3
not(A) and B
A= not $self->data_source_idB= $id_generator eq "-urinternal"
ABresult
0X0
100
111
221 0 $print_id_generator = 0;
222 } else {
223 0 $print_id_generator = 1;
224 }
225 0 ▶ $perl .= " id_generator => '$id_generator',\n" if ($print_id_generator);
Branch: 1 branch
BranchTrueFalseTotal
if $print_id_generator 0 0 0
226 }
227  
228 0 ▶ if ($self->roles and @{ $self->roles }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->roles and @{$self->roles;}) 0 0 0
Condition0%0/3
$self->roles and @{$self->roles;}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->roles and @{$self->roles;}
$self->roles @{$self->roles;}
Truth table0%0/3
A and B
A= $self->rolesB= @{$self->roles;}
ABresult
0X0
100
111
229 0 $perl .= " roles => " . pprint_arrayref($self->roles) . ",\n";
230 }
231  
232 0 ▶ if (my $valid_signals = $self->valid_signals) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $valid_signals = $self->valid_signals) 0 0 0
233 0 ▶ if (ref($valid_signals) ne 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $valid_signals ne 'ARRAY') { } 0 0 0
elsif (@$valid_signals) { } 0 0 0
234 0 Carp::croak("The 'valid_signals' metadata for class $class_name must be an arrayref, got: ".Data::Dumper::Dumper($valid_signals));
235 } elsif (@$valid_signals) {
236 0 $perl .= " valid_signals => ['" . join("', '", @$valid_signals) . "'],\n";
237 }
238 }
239  
240 0 my $doc = $self->doc;
241 0 ▶ if (defined($doc)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $doc) 0 0 0
242 0 $doc = Dumper($doc);
243 0 $doc =~ s/\$VAR1 = //;
244 0 $doc =~ s/;\s*$//;
245 }
246 0 ▶ $perl .= " doc => $doc,\n" if defined($doc);
Branch: 1 branch
BranchTrueFalseTotal
if defined $doc 0 0 0
247 0 return $perl;
248 }
249  
250 0 ▶sub resolve_module_header_source {
Subroutine resolve_module_header_source: not called CC=1 SCAR=6.9
Pod undocumented
251 0 my $self = shift;
252 0 my $class_name = $self->class_name;
253 0 my $perl = "class $class_name {\n";
254 0 $perl .= $self->resolve_class_description_perl;
255 0 $perl .= "};\n";
256 0 return $perl;
257 }
258  
259 266 my $next_line_prefix = "\n";
260 266 my $deep_indent_prefix = "\n" . (" " x 55);
261  
262 0 ▶sub _get_display_fields_for_property {
Subroutine _get_display_fields_for_property: not called CC=62 SCAR=82.7
263 0 my $self = shift;
264 0 my $property = shift;
265 0 my %params = @_;
266  
267 0 ▶ if (not $property->is_specified_in_module_header) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($property->is_specified_in_module_header) 0 0 0
268 # we omit showing implied properties which have no additional data,
269 # unless they have their own docs, a specified column, etc.
270 0 return();
271 }
272  
273 0 my @fields;
274 0 my $property_name = $property->property_name;
275  
276 0 my $type = $property->data_type;
277 0 ▶ if ($type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type) 0 0 0
278 0 ▶ push @fields, "is => '$type'" if $type;
Branch: 1 branch
BranchTrueFalseTotal
if $type 0 0 0
279 }
280  
281 0 ▶ if (defined($property->data_length) and length($property->data_length)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $property->data_length and length $property->data_length) 0 0 0
Condition0%0/3
defined $property->data_length and length $property->data_length
!ll&&!rl&&r
000
MC/DC0%0/2
defined $property->data_length and length $property->data_length
defined $property->data_length length $property->data_length
Truth table0%0/3
A and B
A= defined $property->data_lengthB= length $property->data_length
ABresult
0X0
100
111
282 0 push @fields, "len => " . $property->data_length;
283 }
284  
285 0 ▶ if ($property->is_legacy_eav) {
Branch: 4 branches
BranchTrueFalseTotal
if ($property->is_legacy_eav) { } 0 0 0
elsif ($property->is_delegated) { } 0 0 0
elsif ($property->is_calculated) { } 0 0 0
elsif ($params{'has_table'} and not $property->is_transient) { } 0 0 0
286 # temp hack for entity attribute values
287 0 push @fields, "is_legacy_eav => 1";
288 }
289 elsif ($property->is_delegated) {
290 # do nothing
291 }
292 elsif ($property->is_calculated) {
293 0 ▶ if ($property->column_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($property->column_name) 0 0 0
294 0 ▶ if (! $property->is_mutable) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($property->is_mutable) 0 0 0
295 0 push @fields, q(is_mutable => 0);
296 }
297 0 push @fields, q(column_name => ') . $property->column_name . q(');
298 }
299 0 ▶ if (my $calc_from = $property->calculate_from) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $calc_from = $property->calculate_from) 0 0 0
300 0 ▶ if ($calc_from and @$calc_from == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($calc_from and @$calc_from == 1) { } 0 0 0
elsif ($calc_from) { } 0 0 0
Condition0%0/3
$calc_from and @$calc_from == 1
!ll&&!rl&&r
000
MC/DC0%0/2
$calc_from and @$calc_from == 1
$calc_from @$calc_from == 1
Truth table0%0/3
A and B
A= $calc_fromB= @$calc_from == 1
ABresult
0X0
100
111
301 0 push @fields, "calculate_from => '" . $calc_from->[0] . "'";
302 } elsif ($calc_from) {
303 0 push @fields, "calculate_from => [ '" . join("', '", @$calc_from) . "' ]";
304 }
305 }
306  
307 0 my $calc_source;
308 0 foreach my $calc_type ( qw( calculate calculate_sql calculate_perl calculate_js ) ) {
309 0 ▶ if ($property->$calc_type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($property->$calc_type) 0 0 0
310 0 $calc_source = 1;
311 0 push @fields, "$calc_type => q(" . $property->$calc_type . ")";
312 }
313 }
314  
315 0 ▶ push @fields, 'is_calculated => 1' unless ($calc_source);
Branch: 1 branch
BranchTrueFalseTotal
unless $calc_source 0 0 0
316 }
317 ▶ elsif ($params{has_table} && ! $property->is_transient) {
Condition0%0/3
$params{"has_table"} and not $property->is_transient
!ll&&!rl&&r
000
MC/DC0%0/2
$params{"has_table"} and not $property->is_transient
$params{"has_table"} not $property->is_transient
Truth table0%0/3
A and not(B)
A= $params{"has_table"}B= not $property->is_transient
ABresult
0X0
100
111
318 0 ▶ unless ($property->column_name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($property->column_name) 0 0 0
319 0 die("no column for property on class with table: " . $property->property_name .
320 " class: " . $self->class_name . "?");
321 }
322  
323 0 my $ds = $params{'data_source'};
324 0 ▶ my $should_uc = ($ds && $ds->table_and_column_names_are_upper_case);
Condition0%0/3
$ds && $ds->table_and_column_names_are_upper_case
!ll&&!rl&&r
000
MC/DC0%0/2
$ds && $ds->table_and_column_names_are_upper_case
$ds $ds->table_and_column_names_are_upper_case
Truth table0%0/3
A && B
A= $dsB= $ds->table_and_column_names_are_upper_case
ABresult
0X0
100
111
325  
326 0 my $cname = $property->column_name;
327 0 my $pname = $property->property_name;
328 0 ▶ my $expected_cname = $should_uc ? uc($pname) : $pname;
Branch: 1 branch
BranchTrueFalseTotal
$should_uc ? : 0 0 0
329 0 ▶ if ($cname ne $expected_cname) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cname ne $expected_cname) 0 0 0
330 0 push @fields, "column_name => '" . $cname . "'";
331 }
332 }
333  
334 0 ▶ if (defined($property->default_value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $property->default_value) 0 0 0
335 0 my $value = $property->default_value;
336 0 ▶ if (! looks_like_number($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&looks_like_number($value)) 0 0 0
337 0 $value = "'$value'";
338 }
339 0 push @fields, "default_value => $value";
340 }
341  
342 0 ▶ if (my @id_by = eval { $property->get_property_name_pairs_for_join }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@id_by) = eval { do { $property->get_property_name_pairs_for_join } }) 0 0 0
343 0 ▶ unless (defined $property->reverse_as) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $property->reverse_as) 0 0 0
344 push @fields, "id_by => "
345 . (@id_by > 1 ? '[ ' : '')
346 0 ▶ . join(", ", map { "'" . $_->[0] . "'" } @id_by)
Branch: 2 branches
BranchTrueFalseTotal
@id_by > 1 ? : 0 0 0
@id_by > 1 ? : 0 0 0
347 . (@id_by > 1 ? ' ]' : '');
348 }
349  
350 0 ▶ if (defined $property->id_class_by) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $property->id_class_by) 0 0 0
351 0 push @fields, sprintf("id_class_by => '%s'", $property->id_class_by);
352 }
353 }
354  
355 0 ▶ if ($property->via) {
Branch: 1 branch
BranchTrueFalseTotal
if ($property->via) 0 0 0
356 0 push @fields, "via => '" . $property->via . "'";
357 0 ▶ if ($property->to and $property->to ne $property->property_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($property->to and $property->to ne $property->property_name) 0 0 0
Condition0%0/3
$property->to and $property->to ne $property->property_name
!ll&&!rl&&r
000
MC/DC0%0/2
$property->to and $property->to ne $property->property_name
$property->to $property->to ne $property->property_name
Truth table0%0/3
A and B
A= $property->toB= $property->to ne $property->property_name
ABresult
0X0
100
111
358 0 push @fields, "to => '" . $property->to . "'";
359 }
360  
361 0 ▶ if ($property->is_mutable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($property->is_mutable) 0 0 0
362 # via properties are not usually mutable
363 0 push @fields, 'is_mutable => 1';
364 }
365  
366 0 my $via_property_name = $property->via;
367 0 ▶ if ($via_property_name eq '__self__') {
Branch: 1 branch
BranchTrueFalseTotal
if ($via_property_name eq "__self__") 0 0 0
368 0 $via_property_name = $property->to;
369 }
370 0 my $via = $property->class_name->__meta__->properties(property_name => $via_property_name);
371 0 ▶ if (! $via) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $via) { } 0 0 0
elsif ($property->is_many ne $via->is_many) { } 0 0 0
372 # maybe via a method?? Safer to use is_many than not
373 0 push @fields, 'is_many => 1';
374  
375 } elsif ($property->is_many ne $via->is_many) {
376 0 push @fields, 'is_many => ' . $property->is_many;
377 }
378 }
379 0 ▶ if ($property->reverse_as) {
Branch: 1 branch
BranchTrueFalseTotal
if ($property->reverse_as) 0 0 0
380 0 push @fields, "reverse_as => '" . $property->reverse_as . "'";
381  
382 0 ▶ if ($property->is_mutable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($property->is_mutable) 0 0 0
383 # reverse_as properties are not usually mutable
384 0 push @fields, 'is_mutable => 1';
385 }
386 }
387  
388 0 ▶ if ($property->constraint_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($property->constraint_name) 0 0 0
389 0 push @fields, "constraint_name => '" . $property->constraint_name . "'";
390 }
391  
392 0 ▶ if ($property->where) {
Branch: 1 branch
BranchTrueFalseTotal
if ($property->where) 0 0 0
393 0 my @where_parts = ();
394  
395 0 my @where = @{ $property->where };
396 0 ▶ while (@where) {
Branch: 1 branch
BranchTrueFalseTotal
if (@where) 0 0 0
397 0 my $prop_name = shift @where;
398 0 my $comparison = shift @where;
399 # wrap 'property operator' with quotes if it contains space
400 0 ▶ if (index($prop_name, ' ') >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($prop_name, " ") != -1) 0 0 0
401 0 $prop_name = "'$prop_name'";
402 }
403 0 ▶ if (! ref($comparison)) {
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $comparison) { } 0 0 0
elsif (ref $comparison eq 'HASH') { } 0 0 0
elsif (ref $comparison eq 'ARRAY') { } 0 0 0
404 # It's a strictly equals comparison.
405 # wrap it in quotes...
406 0 $comparison = "'$comparison'";
407  
408 } elsif (ref($comparison) eq 'HASH') {
409 # It's a more complicated operator
410 0 my @operator_parts = ();
411 0 foreach my $key ( 'operator', 'value', keys %$comparison ) {
412 0 ▶ if ($comparison->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($comparison->{$key}) 0 0 0
413 0 ▶ if (ref($comparison->{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $comparison->{$key}) 0 0 0
414 0 my $class_name = $property->class_name;
415 0 Carp::croak("Modulewriter doesn't know how to handle property $property_name of class $class_name. Its 'where' has a non-scalar value for the '$key' key");
416 }
417 0 push @operator_parts, "$key => '" . delete($comparison->{$key}) . "'";
418 }
419 }
420 0 $comparison = '{ ' . join(', ', @operator_parts) . ' } ';
421 } elsif (ref($comparison) eq 'ARRAY') {
422 0 $comparison = pprint_arrayref($comparison);
423 } else {
424 0 my $class_name = $property->class_name;
425 0 Carp::croak("Modulewriter doesn't know how to handle property $property_name of class $class_name. Its 'where' is not a simple scalar, hashref, or arrayref.");
426 }
427 0 push @where_parts, "$prop_name => $comparison";
428 }
429 0 push @fields, 'where => [ ' . join(', ', @where_parts) . ' ]';
430 }
431  
432 0 ▶ if (my $valid_values_arrayref = $property->valid_values) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $valid_values_arrayref = $property->valid_values) 0 0 0
433 0 push @fields, "valid_values => " . pprint_arrayref($valid_values_arrayref);
434 }
435  
436 0 ▶ if (my $example_values_arrayref = $property->example_values) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $example_values_arrayref = $property->example_values) 0 0 0
437 0 push @fields, "example_values => " . pprint_arrayref($example_values_arrayref);
438 }
439  
440 # All the things like is_optional, is_many, etc
441 # show only true values, false is default
442 # section can be things like 'has', 'has_optional' or 'has_transient_many_optional'
443 0 my $section = $params{'section'};
444 0 $section =~ m/^has_(.*)/;
445 0 ▶ my @sections = split('_',$1 || '');
Condition0%0/2
$1 || ''
l!l
00
MC/DC0%0/1
$1 || ''
$1
Truth table0%0/2
A
A= $1
Aresult
11
00
446  
447 0 for my $std_field_name (qw/optional abstract transient constant classwide many deprecated/) {
448 0 ▶ next if (grep { $std_field_name eq $_ } @sections); # Don't print is_optional if we're in the has_optional section
Branch: 1 branch
BranchTrueFalseTotal
if grep {$std_field_name eq $_;} @sections 0 0 0
449 0 my $property_name = "is_" . $std_field_name;
450 0 ▶ push @fields, "$property_name => " . $property->$property_name if $property->$property_name;
Branch: 1 branch
BranchTrueFalseTotal
if $property->$property_name 0 0 0
451 }
452  
453  
454 0 foreach my $meta_property ( @{$params{'attributes_have'}} ) {
455 0 my $value = $property->{$meta_property};
456 0 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
457 0 ▶ my $format = looks_like_number($value) ? "%s => %s" : "%s => '%s'";
Branch: 1 branch
BranchTrueFalseTotal
&looks_like_number($value) ? : 0 0 0
458 0 push @fields, sprintf($format, $meta_property, $value);
459 }
460 }
461  
462 0 my $desc = $property->doc;
463 0 ▶ if ($desc && length($desc)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($desc and length $desc) 0 0 0
Condition0%0/3
$desc and length $desc
!ll&&!rl&&r
000
MC/DC0%0/2
$desc and length $desc
$desc length $desc
Truth table0%0/3
A and B
A= $descB= length $desc
ABresult
0X0
100
111
464 0 $desc =~ s/([\$\@\%\\\"])/\\$1/g;
465 0 $desc =~ s/\n/\\n/g;
466 0 ▶ if ($desc =~ /'/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($desc =~ /'/) { } 0 0 0
467 0 $desc = "q($desc)";
468 } else {
469 0 $desc = "'$desc'";
470 }
471 0 push @fields, $next_line_prefix . "doc => $desc";
472 }
473  
474 0 return @fields;
475 }
476  
477 0 ▶sub module_base_name {
Subroutine module_base_name: not called CC=1 SCAR=6.9
Pod undocumented
478 0 my $file_name = shift->class_name;
479 0 $file_name =~ s/::/\//g;
480 0 $file_name .= ".pm";
481 0 return $file_name;
482 }
483  
484 0 ▶sub module_path {
Subroutine module_path: not called CC=5 SCAR=34.0
Pod undocumented
485 0 my $self = shift;
486 0 my $base_name = $self->module_base_name;
487 0 my $path = $INC{$base_name};
488 0 ▶ return _abs_path_relative_to_pwd_at_compile_time($path) if $path;
Branch: 1 branch
BranchTrueFalseTotal
if $path 0 0 0
489  
490 0 my $namespace;
491 0 my $first_slash = index($base_name, '/');
492 0 ▶ if ($first_slash >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first_slash >= 0) { } 0 0 0
493 # Normal case...
494 0 $namespace = substr($base_name, 0, $first_slash);
495 0 $namespace .= ".pm";
496 } else {
497 # This module must _be_ the namespace
498 0 $namespace = $base_name;
499 }
500  
501 0 for my $dir (map { _abs_path_relative_to_pwd_at_compile_time($_) } grep { -d $_ } @INC) {
502 0 ▶ if (-e $dir . "/" . $namespace) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $dir . '/' . $namespace) 0 0 0
503 0 my $try_path = $dir . '/' . $base_name;
504 0 return $try_path;
505 }
506 }
507 0 return;
508 }
509  
510 0 ▶sub _abs_path_relative_to_pwd_at_compile_time { # not a method
Subroutine _abs_path_relative_to_pwd_at_compile_time: not called CC=2 SCAR=17.9
511 0 my $path = shift;
512 0 ▶ if ($path !~ /^[\/\\]/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($path =~ m(^[/\\])) 0 0 0
513 0 $path = $pwd_at_compile_time . '/' . $path;
514 }
515 0 my $path2 = Cwd::abs_path($path);
516 0 return $path2;
517 }
518  
519  
520 0 ▶sub module_directory {
Subroutine module_directory: not called CC=4 SCAR=30.0
Pod undocumented
521 0 my $self = shift;
522 0 my $base_name = $self->module_base_name;
523 0 my $path = $self->module_path;
524 0 ▶ return unless defined($path) and length($path);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path and length $path 0 0 0
Condition0%0/3
defined $path and length $path
!ll&&!rl&&r
000
MC/DC0%0/2
defined $path and length $path
defined $path length $path
Truth table0%0/3
A and B
A= defined $pathB= length $path
ABresult
0X0
100
111
525 0 ▶ unless ($path =~ s/$base_name$//) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($path =~ s/$base_name$//) 0 0 0
526 0 Carp::confess("Failed to find base name $base_name at the end of path $path!")
527 }
528 0 return $path;
529 }
530  
531 0 ▶sub module_data_subdirectory {
Subroutine module_data_subdirectory: not called CC=1 SCAR=6.9
Pod undocumented
532 0 my $self = shift;
533 0 my $path = $self->module_path;
534 0 $path =~ s/.pm$//;
535 0 return $path;
536 }
537  
538  
539 0 ▶sub module_source_lines {
Subroutine module_source_lines: not called CC=2 SCAR=17.9
Pod undocumented
540 0 my $self = shift;
541 0 my $file_name = $self->module_path;
542 0 my $in = IO::File->new("<$file_name");
543 0 ▶ unless ($in) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in) 0 0 0
544 0 return (undef,undef,undef);
545 }
546 0 my @module_src = <$in>;
547 0 $in->close;
548 return @module_src
549 0 }
550  
551 0 ▶sub module_source {
Subroutine module_source: not called CC=1 SCAR=6.9
Pod undocumented
552 0 join("",shift->module_source_lines);
553 }
554  
555 0 ▶sub module_header_positions {
Subroutine module_header_positions: not called CC=13 SCAR=52.0
Pod undocumented
556 0 my $self = shift;
557  
558 0 my @module_src = $self->module_source_lines;
559 0 my $namespace = $self->namespace;
560 0 my $class_name = $self->class_name;
561  
562 0 ▶ unless ($self->namespace) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->namespace) 0 0 0
563 0 die "No namespace on $self->{class_name}?"
564 }
565  
566 0 ▶ $namespace = 'UR' if $namespace eq $self->class_name;
Branch: 1 branch
BranchTrueFalseTotal
if $namespace eq $self->class_name 0 0 0
567  
568 0 my $state = 'before';
569 0 my ($begin,$end,$use);
570 0 ▶ for (my $n = 0; $n < @module_src; $n++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < @module_src) 0 0 0
571 0 my $line = $module_src[$n];
572 0 ▶ if ($state eq 'before') {
Branch: 2 branches
BranchTrueFalseTotal
if ($state eq 'before') { } 0 0 0
elsif ($state eq 'during') { } 0 0 0
573 0 ▶ if ($line and $line =~ /^use $namespace;/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line and $line =~ /^use $namespace;/) 0 0 0
Condition0%0/3
$line and $line =~ /^use $namespace;/
!ll&&!rl&&r
000
MC/DC0%0/2
$line and $line =~ /^use $namespace;/
$line $line =~ /^use $namespace;/
Truth table0%0/3
A and B
A= $lineB= $line =~ /^use $namespace;/
ABresult
0X0
100
111
574 0 $use = $n;
575 }
576 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($line and $line =~ /^define UR::Object::Type$/ || $line =~ /^(App|UR)::Object::(Type|Class)->(define|create)\($/ || $line =~ /^class\s*$class_name\b/) { } 0 0 0
Condition0%0/9
$line and $line =~ /^define UR::Object::Type$/ || $line =~ /^(App|UR)::Object::(Type|Class)->(define|create)\($/ || $line =~ /^class\s*$class_name\b/
!ll&&!rl&&r
000
$line =~ /^define UR::Object::Type$/ or $line =~ /^(App|UR)::Object::(Type|Class)->(define|create)\($/ or $line =~ /^class\s*$class_name\b/
l!l&&r!l&&!r
000
$line =~ /^define UR::Object::Type$/ or $line =~ /^(App|UR)::Object::(Type|Class)->(define|create)\($/
l!l&&r!l&&!r
000
MC/DC0%0/4
$line and $line =~ /^define UR::Object::Type$/ || $line =~ /^(App|UR)::Object::(Type|Class)->(define|create)\($/ || $line =~ /^class\s*$class_name\b/
$line $line =~ /^define UR::Object::Type$/ $line =~ /^(App|UR)::Object::(Type|Class)->(define|create)\($/ $line =~ /^class\s*$class_name\b/
Truth table0%0/5
A and B or C or D
A= $lineB= $line =~ /^define UR::Object::Type$/C= $line =~ /^(App|UR)::Object::(Type|Class)->(define|create)\($/D= $line =~ /^class\s*$class_name\b/
ABCDresult
0XXX0
10000
11XX1
101X1
10011
577 $line and (
578 $line =~ /^define UR::Object::Type$/
579 or $line =~ /^(App|UR)::Object::(Type|Class)->(define|create)\($/
580 or $line =~ /^class\s*$class_name\b/
581 )
582 ) {
583 0 $begin = $n;
584 0 $state = 'during';
585 }
586 else {
587  
588 }
589 }
590 elsif ($state eq 'during') {
591 0 my $old_meta_src .= $line; # FIXME this dosen't appear anywhere else??
592 0 ▶ if ($line =~ /^(\)|\}|);\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^(\)|\}|);\s*$/) 0 0 0
593 0 $end = $n;
594 0 $state = 'after';
595 }
596 }
597 }
598  
599 # cache
600 0 $self->{module_header_positions} = [$begin,$end,$use];
601  
602 # return
603 0 return ($begin,$end,$use);
604 }
605  
606 0 ▶sub module_header_source_lines {
Subroutine module_header_source_lines: not called CC=2 SCAR=17.9
Pod undocumented
607 0 my $self = shift;
608 0 my ($begin,$end,$use) = $self->module_header_positions;
609 0 my @src = $self->module_source_lines;
610 0 ▶ return unless $end;
Branch: 1 branch
BranchTrueFalseTotal
unless $end 0 0 0
611 0 @src[$begin..$end];
612 }
613  
614 0 ▶sub module_header_source {
Subroutine module_header_source: not called CC=1 SCAR=6.9
Pod undocumented
615 0 join("",shift->module_header_source_lines);
616 }
617  
618 0 ▶sub rewrite_module_header {
Subroutine rewrite_module_header: not called CC=22 SCAR=60.6
Pod undocumented
619 266 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
620 266 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
621  
622 0 my $self = shift;
623 0 my $package = $self->class_name;
624  
625 # generate new class metadata
626 0 my $new_meta_src = $self->resolve_module_header_source;
627 0 ▶ unless ($new_meta_src) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($new_meta_src) 0 0 0
628 0 die "Failed to generate source code for $package!";
629 }
630  
631 0 my ($begin,$end,$use) = $self->module_header_positions;
632  
633 0 my $namespace = $self->namespace;
634 0 ▶ $namespace = 'UR' if $namespace eq $self->class_name;
Branch: 1 branch
BranchTrueFalseTotal
if $namespace eq $self->class_name 0 0 0
635  
636 0 ▶ unless ($namespace) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($namespace) 0 0 0
637 0 ($namespace) = ($package =~ /^(.*?)::/);
638 }
639 0 ▶ $new_meta_src = "use $namespace;\n" . $new_meta_src unless $use;
Branch: 1 branch
BranchTrueFalseTotal
unless $use 0 0 0
640  
641 # determine the path to the module
642 # this may not exist
643 0 my $module_file_path = $self->module_path;
644  
645 # temp safety hack
646 0 ▶ if ($module_file_path =~ "/gsc/scripts/lib") {
Branch: 1 branch
BranchTrueFalseTotal
if ($module_file_path =~ m[/gsc/scripts/lib]) 0 0 0
647 0 Carp::confess("attempt to write directly to the app server!");
648 }
649  
650 # determine the new source for the module
651 0 my @module_src;
652 my $old_file_data;
653 0 ▶ if (-e $module_file_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $module_file_path) { } 0 0 0
654 # rewrite the existing module
655  
656 # find the old positions of the module header
657 0 @module_src = $self->module_source_lines;
658  
659 # cleanup legacy cruft
660 0 ▶ unless ($namespace eq 'UR') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($namespace eq "UR") 0 0 0
661 0 ▶ @module_src = map { ($_ =~ m/^use UR;/?"":$_) } @module_src;
Branch: 1 branch
BranchTrueFalseTotal
$_ =~ /^use UR;/ ? : 0 0 0
662 }
663  
664 0 ▶ if (!grep {$_ =~ m/^use warnings;/} @module_src) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ =~ /^use warnings;/;} @module_src) 0 0 0
665 0 $new_meta_src = "use warnings;\n" . $new_meta_src;
666 }
667  
668 0 ▶ if (!grep {$_ =~ m/^use strict;/} @module_src) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ =~ /^use strict;/;} @module_src) 0 0 0
669 0 $new_meta_src = "use strict;\n" . $new_meta_src;
670 }
671  
672 # If $begin and $end are undef, then module_header_positions() didn't find any
673 # old code and we're inserting all brand new stuff. Look for the package declaration
674 # and insert after that.
675 0 my $len;
676 0 ▶ if (defined $begin || defined $end) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $begin or defined $end) { } 0 0 0
Condition0%0/3
defined $begin or defined $end
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $begin or defined $end
defined $begin defined $end
Truth table0%0/3
A or B
A= defined $beginB= defined $end
ABresult
1X1
011
000
677 0 $len = $end-$begin+1;
678 } else {
679 # is there a more fool-proof way to find it?
680 0 ▶ for ($begin = 0; $begin < $#module_src; ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($begin < $#module_src) 0 0 0
681 0 ▶ last if ($module_src[$begin++] =~ m/package\s+$package;/);
Branch: 1 branch
BranchTrueFalseTotal
if $module_src[$begin++] =~ /package\s+$package;/ 0 0 0
682 }
683 0 $len = 0;
684 }
685  
686 # replace the old lines with the new source
687 # note that the inserted "row" is multi-line, but joins nicely below...
688 0 splice(@module_src,$begin,$len,$new_meta_src);
689  
690 0 my $f = IO::File->new($module_file_path);
691 0 $old_file_data = join('',$f->getlines);
692 0 $f->close();
693 }
694 else {
695 # write new module source
696  
697 # put =cut marks around it if this is a special metadata class
698 # the definition at the top is non-functional for bootstrapping reasons
699 0 ▶ if ($meta_classes{$package}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($UR::Object::Type::meta_classes{$package}) 0 0 0
700 0 $new_meta_src = "=cut\n\n$new_meta_src\n\n=cut\n\n";
701 0 $self->warning_message("Meta package $package");
702 }
703  
704 0 @module_src = join("\n",
705 "package " . $self->class_name . ";",
706 "",
707 "use strict;",
708 "use warnings;",
709 "",
710 $new_meta_src,
711 "1;",
712 ""
713 );
714 }
715  
716 0 ▶ $ENV{'HOST'} ||= '';
Condition0%0/2
$ENV{"HOST"} ||= ""
l!l
00
MC/DC0%0/1
$ENV{"HOST"} ||= ""
$ENV{"HOST"}
Truth table0%0/2
A
A= $ENV{"HOST"}
Aresult
11
00
717 0 my $temp = "$module_file_path.$$.$ENV{HOST}";
718 0 my $temp_dir = $module_file_path;
719 0 $temp_dir =~ s/\/[^\/]+$//;
720 0 ▶ unless (-d $temp_dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $temp_dir) 0 0 0
721 0 print "mkdir -p $temp_dir\n";
722 0 system "mkdir -p $temp_dir";
723 }
724 0 my $out = IO::File->new(">$temp");
725 0 ▶ unless ($out) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($out) 0 0 0
726 0 die "Failed to create temp file $temp!";
727 }
728 0 for (@module_src) { $out->print($_) };
729 0 $out->close;
730  
731 0 my $rv = system qq(perl -e 'eval `cat $temp`' 2>/dev/null 1>/dev/null);
732 0 $rv /= 255;
733 0 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) { } 0 0 0
734 0 die "Module is not compilable with new source!";
735 }
736 else {
737 0 ▶ unless (rename $temp, $module_file_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (rename $temp, $module_file_path) 0 0 0
738 0 die "Error renaming $temp to $module_file_path!";
739 }
740 }
741  
742 0 UR::Context::Transaction->log_change($self, ref($self), $self->id, 'rewrite_module_header', Data::Dumper::Dumper{path => $module_file_path, data => $old_file_data});
743  
744 0 return 1;
745 }
746  
747  
748 0 ▶sub pprint_arrayref {
Subroutine pprint_arrayref: not called CC=1 SCAR=6.9
Pod undocumented
749 0 my $arrayref = shift;
750 # Useqq(1) causes newlines to be escaped so the only newlines are those
751 # injected by Indent(1). Useqq(1) also quotes string values so we can
752 # strip the whitespace around the newlines.
753 0 my $value_string = Data::Dumper->new([$arrayref])->Terse(1)->Indent(1)->Useqq(1)->Dump;
754 0 $value_string =~ s/\s*\n\s*/ /g;
755 0 $value_string =~ s/\s*$//;
756 0 return $value_string;
757 }
758  
759  
760 0 ▶sub pprint_section {
Subroutine pprint_section: not called CC=1 SCAR=6.9
Pod undocumented
761 0 my ($section, $section_src) = @_;
762 0 my $indent_section = ' ' x 4;
763 0 return "$indent_section$section => [\n$section_src$indent_section],\n";
764 }
765  
766 {
767 266 my $indent_name = ' ' x 8;
768 266 my $indent_key = $indent_name . ' ' x 4;
769 266 my $max_width = 78;
770 0 ▶ sub pprint_subsection {
Subroutine pprint_subsection: not called CC=3 SCAR=24.8
Pod undocumented
771 0 my ($name, @fields) = @_;
772  
773 0 foreach ( @fields ) { s/^\s+// }
774 0 my $section_src = _pprint_subsection_one_line($name, @fields);
775 0 ▶ if (length($section_src) > $max_width) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $section_src > $max_width) 0 0 0
776 0 $section_src = _pprint_subsection_multi_line($name, @fields);
777 }
778 0 return $section_src;
779 }
780  
781 0 ▶ sub _pprint_subsection_one_line {
Subroutine _pprint_subsection_one_line: not called CC=1 SCAR=6.9
782 0 my $name = shift;
783  
784 0 return $indent_name . $name . ' => { '
785 . join(', ', @_)
786 . " },\n";
787 }
788  
789 0 ▶ sub _pprint_subsection_multi_line {
Subroutine _pprint_subsection_multi_line: not called CC=1 SCAR=6.9
790 0 my $name = shift;
791  
792 0 return $indent_name . $name . " => {\n"
793 . $indent_key . join(",\n$indent_key", @_) . ",\n"
794 . $indent_name . "},\n";
795 }
796 }
797  
798 266 ▶sub _quoted_value {
Subroutine _quoted_value: not called CC=3 SCAR=23.0
799 0 my $value = shift;
800 0 my ($qo, $qc) = ('', '');
801 0 ▶ if (!looks_like_number($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&looks_like_number($value)) 0 0 0
802 0 ▶ if ($value =~ /'/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /'/) { } 0 0 0
803 0 ($qo, $qc) = ('q(', ')');
804 } else {
805 0 ($qo, $qc) = ("'", "'");
806 }
807 }
808 0 return "$qo$value$qc";
809 }
810  
811 0 ▶sub _idx {
Subroutine _idx: not called CC=2 SCAR=17.9
812 0 my $e = shift;
813 0 my @expected_order = qw(
814 is
815 is_optional
816 );
817 0 ▶ my $e_idx = first_index { $_ eq $e } @expected_order;
Subroutine __ANON__: not called CC=1 SCAR=6.9
818 0 ▶ if ($e_idx == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e_idx == -1) 0 0 0
819 0 $e_idx = scalar(@expected_order);
820 }
821 0 return $e_idx;
822 }
823  
824 0 ▶sub _key_sorter {
Subroutine _key_sorter: not called CC=2 SCAR=17.9
825 0 my ($a_idx, $b_idx) = (_idx($a), _idx($b));
826 0 my $cmp;
827 0 ▶ if ($a_idx == $b_idx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a_idx == $b_idx) { } 0 0 0
828 0 $cmp = $a cmp $b;
829 } else {
830 0 $cmp = $a_idx <=> $b_idx;
831 }
832 0 return $cmp;
833 }
834  
835 0 ▶sub _sort_keys {
Subroutine _sort_keys: not called CC=1 SCAR=6.9
836 0 sort _key_sorter @_;
837 }
838  
839 0 ▶sub _exclude_items {
Subroutine _exclude_items: not called CC=1 SCAR=6.9
840 0 my ($list, $exclude) = @_;
841 return grep {
842 0 my $l = $_;
843 !grep {
844 0 my $e = $_;
845 0 $e eq $l;
846 } @$exclude;
847 } @$list;
848 }
849  
850 0 ▶sub _section_lines {
Subroutine _section_lines: not called CC=1 SCAR=6.9
851 0 my ($struct, @keys) = @_;
852 0 @keys = _sort_keys(@keys);
853 my @lines = map {
854 0 my $value = _quoted_value($struct->{$_});
855 0 sprintf('%s => %s', $_, $value);
856 } @keys;
857 0 return @lines;
858 }
859  
860  
861 266 1;
862  
863 =pod
864  
865 =head1 NAME
866  
867 UR::Object::Type::ModuleWriter - Helper module for UR::Object::Type responsible for writing Perl modules
868  
869 =head1 DESCRIPTION
870  
871 Subroutines within this module actually live in the UR::Object::Type
872 namespace; this module is just a convienent place to collect them. The
873 Module Writer is used by the class updater system (L<(UR::Namespace::Command::Update::Classes>
874 and 'ur update classes) to add, remove and alter the Perl modules behind
875 the classes within a Namespace.
876  
877 =head1 METHODS
878  
879 =over 4
880  
881 =item resolve_module_header_source
882  
883 $classobj->resolve_module_header_source();
884  
885 Returns a string that represents a fully-formed class definition the the
886 given class metaobject $classobj.
887  
888 =item resolve_class_description_perl
889  
890 $classobj->resolve_class_description_perl()
891  
892 Used by resolve_module_header_source(). This method inspects all the
893 applicable properties of the class metaobject and builds up a string that
894 gets inserted between the {...} of the class definition string.
895  
896 =item rewrite_module_header
897  
898 $classobj->rewrite_module_header();
899  
900 This method rewrites an existing Perl module file in place for the class
901 metaobject, or creates a new file if one does not already exist.
902  
903 =item module_base_name
904  
905 Returns the pathname of the class's module relative to the top level directory
906 of that class's Namespace.
907  
908 =item module_path
909  
910 Returns the fully qualified pathname of the class's module.
911  
912 =item module_source_lines
913  
914 Returns the text of the class's Perl module as a list of strings.
915  
916 =item module_source
917  
918 Returns the text of the class's Perl module as a single string.
919  
920 =item module_header_positions
921  
922 Returns a 3-element list ($begin, $end, $use) where $begin is the line number
923 where the class header begins. $end is the line number where it ends. $use
924 is the line number where the module declares that it use's a Namespace.
925  
926 =item module_header_source_lines
927  
928 Returns the text of the class's Perl module source where the class definition
929 is as a list of strings.
930  
931 =item module_header_source
932  
933 Returns the text of the class's Perl module source where the class definition
934 is as a single string.
935  
936 =back
937  
938 =head1 SEE ALSO
939  
940 UR::Object::Type, UR::Object::Type::Initializer
941  
942 =cut
943  
944  
« blib/lib/UR/DataSource/File.pm ↑ Summary blib/lib/UR/DataSource/Filesystem.pm »