DataThing/Type.pm

filter: Statementstmt 81.5% 106 / 130 Branchbran 59.5% 25 / 42 Conditioncond 14.2% 2 / 14 MC/DCmcdc 11.1% 1 / 9 Subroutinesub 74.2% 26 / 35 Podpod 0.0% 0 / 13 totaltotal 65.8% 160 / 243 CC 29 SCAR 29.5
CC
29
Cov
72%
compare
110.0
property_type
7.0
for_each_property
4.0
CRAP 19.0
↑ Summary DataThing.pm »
1  
2 =head1 NAME
3  
4 Util::DataThing::Type - Represents the type of a property in L<Util::DataThing>
5  
6 =cut
7  
8 package Util::DataThing::Type;
9  
10 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Carp qw(croak confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use overload "<=>" => \&_compare, '""' => sub { $_[0]->{display_name} };
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
14 4 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use Sub::Name;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use MRO::Compat;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 4 my %primitives;
19 my %primitive_coerce;
20 4 my %objects;
21 4 my %object_properties;
22  
23 4 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.0
24  
25 4 %primitives = ();
26 %primitive_coerce = (
27 11 ▶ string => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine _coerce_string: called CC=1 SCAR=0.0
28 11 return "".$_[0];
29 },
30 0 ▶ integer => sub {
Subroutine __ANON__: not called CC=1 SCAR=1.2
Subroutine _coerce_integer: called CC=1 SCAR=1.2
31 0 return int($_[0])+0;
32 },
33 0 ▶ float => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
34 0 return $_[0]+0;
35 },
36 6 ▶ boolean => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
37 6 ▶ return $_[0] ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 4 2 6
38 },
39 0 ▶ any => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
40 0 return $_[0];
41 },
42 4 );
43 4 %objects = ();
44 4 %object_properties = ();
45  
46  
47 4 foreach my $type_name (qw(string integer float boolean any)) {
48 20 my $obj = bless {}, __PACKAGE__;
49 20 $obj->{display_name} = '('.$type_name.')';
50 20 my $coerce = $primitive_coerce{$type_name};
51 20 Sub::Name::subname("_coerce_".$type_name, $coerce);
52 20 $obj->{coerce_in} = $coerce;
53 20 $obj->{coerce_out} = $coerce;
54  
55 20 $primitives{$type_name} = $obj;
56  
57 # We also create a convenient constant in Util::DataThing
58 # so that subclasses can write, for example, STRING instead
59 # of Util::DataThing::Type->primitive('string').
60 20 my $sub_name = 'Util::DataThing::'.uc($type_name);
61 {
62 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
63 20 ▶ *{$sub_name} = sub { $obj };
Subroutine __ANON__: called CC=1 SCAR=0.0
64 }
65 }
66 }
67  
68 0 ▶sub primitive {
Subroutine primitive: not called CC=1 SCAR=6.9
Pod undocumented
69 0 my ($class, $type_name) = @_;
70  
71 0 return $primitives{$type_name} or croak "There is no primitive type called $type_name";
72 }
73  
74 15 ▶sub object {
Subroutine object: called CC=2 SCAR=7.1
Pod undocumented
75 15 my ($class, $object_class) = @_;
76  
77 15 ▶ return $objects{$object_class} if defined($objects{$object_class});
Branch: 1 branch
BranchTrueFalseTotal
if defined $objects{$object_class} 10 5 15
78  
79 5 my $self = bless {}, __PACKAGE__;
80 5 $self->{display_name} = $object_class;
81 5 $self->{object_class} = $object_class;
82 2 ▶ $self->{coerce_in} = sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
83 2 my ($value) = @_;
84  
85 2 my $class = $self->object_class;
86 2 ▶ confess("Only $class objects can be assigned to this field.") unless UNIVERSAL::isa($value, $class);
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($value, $class) 0 2 2
87  
88 2 return $value->{data};
89 5 };
90 0 ▶ $self->{coerce_out} = sub {
Subroutine __ANON__: not called CC=1 SCAR=4.1
91 0 my ($value) = @_;
92  
93 0 my $class = $self->object_class;
94 0 my $obj = { data => $value };
95 0 return bless $obj, $class;
96 5 };
97  
98 5 Sub::Name::subname("_coerce_in_object", $self->{coerce_in});
99 5 Sub::Name::subname("_coerce_out_object", $self->{coerce_out});
100  
101 # Ensure that we only end up with one instance in memory for each
102 # class at any time, but also that we don't end up with
103 # a giant cache of unused types.
104 5 $objects{$object_class} = $self;
105 5 Scalar::Util::weaken($objects{$object_class});
106  
107 5 return $self;
108 }
109  
110 14 ▶sub is_object {
Subroutine is_object: called CC=2 SCAR=7.6
Pod undocumented
111 14 ▶ return defined($_[0]->{object_class}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]{'object_class'} ? : 14 0 14
112 }
113  
114 23 ▶sub object_class {
Subroutine object_class: called CC=1 SCAR=0.0
Pod undocumented
115 23 return $_[0]->{object_class};
116 }
117  
118 1 ▶sub for_each_property {
Subroutine for_each_property: called CC=4 SCAR=13.9
Pod undocumented
119 1 my ($self, $code) = @_;
120  
121 1 ▶ return unless $self->is_object;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_object 0 1 1
122  
123 1 my $object_class = $self->object_class;
124  
125 1 my $all_classes = mro::get_linear_isa($object_class);
126  
127 1 ▶ for (my $i = scalar(@$all_classes) - 1; $i >= 0; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 3 1 4
128 3 my $class = $all_classes->[$i];
129 3 my $properties = $object_properties{$class};
130 3 ▶ next unless $properties;
Branch: 1 branch
BranchTrueFalseTotal
unless $properties 1 2 3
131  
132 2 map { $code->($_, $properties->{$_}) } keys %$properties;
133 }
134  
135 }
136  
137 1 ▶sub properties {
Subroutine properties: called CC=2 SCAR=7.0
Pod undocumented
138 1 my ($self) = @_;
139  
140 1 ▶ return {} unless $self->is_object;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_object 0 1 1
141  
142 1 my $ret = {};
143 2 ▶ $self->for_each_property(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
144 2 $ret->{$_[0]} = $_[1];
145 1 });
146 1 return $ret;
147 }
148  
149 12 ▶sub property_type {
Subroutine property_type: called CC=7 SCAR=19.5
Pod undocumented
150 12 my ($self, $property_name) = @_;
151  
152 12 ▶ return undef unless $self->is_object;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_object 0 12 12
153  
154 12 my $want_array = wantarray;
155  
156 12 my $object_class = $self->object_class;
157  
158 12 my $all_classes = mro::get_linear_isa($object_class);
159  
160 12 ▶ for (my $i = 0; $i < scalar(@$all_classes); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$all_classes) 25 10 35
161 25 my $class = $all_classes->[$i];
162 25 my $properties = $object_properties{$class};
163 25 ▶ next unless $properties;
Branch: 1 branch
BranchTrueFalseTotal
unless $properties 16 9 25
164  
165 9 ▶ if ($want_array) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want_array) { } 7 2 9
166 7 ▶ return $properties->{$property_name}, $class if defined($properties->{$property_name});
Branch: 1 branch
BranchTrueFalseTotal
if defined $properties->{$property_name} 1 6 7
167 }
168 else {
169 2 ▶ return $properties->{$property_name} if defined($properties->{$property_name});
Branch: 1 branch
BranchTrueFalseTotal
if defined $properties->{$property_name} 1 1 2
170 }
171 }
172  
173 # If we fall out here then no parent class has the
174 # property we're looking for.
175 10 return undef;
176 }
177  
178 # Arrays and maps are not yet supported
179 0 ▶sub is_array {
Subroutine is_array: not called CC=1 SCAR=6.9
Pod undocumented
180 0 return 0;
181 }
182 0 ▶sub is_map {
Subroutine is_map: not called CC=1 SCAR=6.9
Pod undocumented
183 0 return 0;
184 }
185 0 ▶sub inner_type {
Subroutine inner_type: not called CC=1 SCAR=6.9
Pod undocumented
186 0 return undef;
187 }
188  
189 10 ▶sub coerce_in {
Subroutine coerce_in: called CC=2 SCAR=8.1
Pod undocumented
190 10 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) { } 0 10 10
191 0 return $_[0]->{coerce_in}->($_[1]);
192 }
193 else {
194 10 return $_[0]->{coerce_in};
195 }
196 }
197  
198 10 ▶sub coerce_out {
Subroutine coerce_out: called CC=2 SCAR=8.1
Pod undocumented
199 10 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) { } 0 10 10
200 0 return $_[0]->{coerce_out}->($_[1]);
201 }
202 else {
203 10 return $_[0]->{coerce_out};
204 }
205 }
206  
207 0 ▶sub compare {
Subroutine compare: not called CC=10 SCAR=47.0
Pod undocumented
208 0 my ($a, $b, $reversed) = @_;
209  
210 # Fast path for our singleton primitive types
211 0 ▶ return 0 if $a == $b;
Branch: 1 branch
BranchTrueFalseTotal
if $a == $b 0 0 0
212  
213 0 ▶ return -1 unless $a->isa('Util::ObjectThing::Type') && $b->isa('Util::ObjectThing::Type');
Branch: 1 branch
BranchTrueFalseTotal
unless $a->isa("Util::ObjectThing::Type") and $b->isa("Util::ObjectThing::Type") 0 0 0
Condition0%0/3
$a->isa("Util::ObjectThing::Type") and $b->isa("Util::ObjectThing::Type")
!ll&&!rl&&r
000
MC/DC0%0/2
$a->isa("Util::ObjectThing::Type") and $b->isa("Util::ObjectThing::Type")
$a->isa("Util::ObjectThing::Type") $b->isa("Util::ObjectThing::Type")
Truth table0%0/3
A and B
A= $a->isa("Util::ObjectThing::Type")B= $b->isa("Util::ObjectThing::Type")
ABresult
0X0
100
111
214  
215 0 ▶ if ($a->is_object && $b->is_object) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a->is_object and $b->is_object) 0 0 0
Condition0%0/3
$a->is_object and $b->is_object
!ll&&!rl&&r
000
MC/DC0%0/2
$a->is_object and $b->is_object
$a->is_object $b->is_object
Truth table0%0/3
A and B
A= $a->is_objectB= $b->is_object
ABresult
0X0
100
111
216 0 return $a->object_class cmp $b->object_class;
217 }
218  
219 0 ▶ if ($a->is_array && $b->is_array) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a->is_array and $b->is_array) 0 0 0
Condition0%0/3
$a->is_array and $b->is_array
!ll&&!rl&&r
000
MC/DC0%0/2
$a->is_array and $b->is_array
$a->is_array $b->is_array
Truth table0%0/3
A and B
A= $a->is_arrayB= $b->is_array
ABresult
0X0
100
111
220 0 return $a->inner_type <=> $b->inner_type;
221 }
222  
223 0 ▶ if ($a->is_map && $b->is_map) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a->is_map and $b->is_map) 0 0 0
Condition0%0/3
$a->is_map and $b->is_map
!ll&&!rl&&r
000
MC/DC0%0/2
$a->is_map and $b->is_map
$a->is_map $b->is_map
Truth table0%0/3
A and B
A= $a->is_mapB= $b->is_map
ABresult
0X0
100
111
224 0 return $a->inner_type <=> $b->inner_type;
225 }
226  
227 0 return -1;
228  
229 }
230  
231 10 ▶sub _register_object_property {
Subroutine _register_object_property: called CC=2 SCAR=6.9
232 10 my ($class, $object_class, $property_name, $type) = @_;
233  
234 10 ▶ $object_properties{$object_class} ||= {};
Condition100%2/2
$object_properties{$object_class} ||= {}
l!l
55
MC/DC100%1/1
$object_properties{$object_class} ||= {}
$object_properties{$object_class}
Truth table100%2/2
A
A= $object_properties{$object_class}
Aresult
11
00
235 10 $object_properties{$object_class}{$property_name} = $type;
236 }
237  
238 4 1;
↑ Summary DataThing.pm »