Yancy/Backend/Memory.pm

filter: Statementstmt 98.5% 209 / 212 Branchbran 90.0% 90 / 100 Conditioncond 86.3% 82 / 95 MC/DCmcdc 65.3% 32 / 49 Subroutinesub 100% 20 / 20 Podpod 11.1% 1 / 9 totaltotal 89.4% 434 / 485 CC 108 SCAR 24.9
CC
108
Cov
94%
list
18.1
create
17.0
set
14.0
CRAP 12.0
« Yancy/Plugin/Form/Bootstrap4.pm ↑ Summary Yancy/Model/Schema.pm »
1 package Yancy::Backend::Memory;
2 23 our $VERSION = '1.087';
3 # ABSTRACT: A backend entirely in memory
4  
5 #pod =head1 DESCRIPTION
6 #pod
7 #pod An in-memory "database" backend for Yancy. Uses L<Yancy::Util/match> to implement
8 #pod basic searching for (</list>).
9 #pod
10 #pod =cut
11  
12 # XXX: TODO Remove references to Local::Test
13  
14 23 ▶use Mojo::Base '-base';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 23 ▶use List::Util qw( max );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 23 ▶use Mojo::JSON qw( true false from_json to_json encode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 23 ▶use Mojo::File qw( path );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 23 ▶use Storable qw( dclone );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 23 ▶use Role::Tiny qw( with );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 23 with 'Yancy::Backend::Role::Sync';
21 23 ▶use Yancy::Util qw( match is_type order_by is_format );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 23 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 23 our %DATA;
25  
26 sub new {
27 84 ▶ my ( $class, $url, $schema ) = @_;
Subroutine new: called CC=5 SCAR=18.1
Pod documented
28 84 ▶ if ( $url ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url) 83 1 84
29 83 my ( $path ) = $url =~ m{^[^:]+://[^/]+(?:/(.+))?$};
30 83 ▶ if ( $path ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path) 0 83 83
31 0 ▶ %DATA = %{ from_json( path( ( $ENV{MOJO_HOME} || () ), $path )->slurp ) };
Condition0%0/3
$ENV{'MOJO_HOME'} || ()
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{'MOJO_HOME'} || ()
$ENV{'MOJO_HOME'} ()
Truth table0%0/3
A || B
A= $ENV{'MOJO_HOME'}B= ()
ABresult
1X1
011
000
32 }
33 }
34 84 ▶ $schema //= \%Local::Test::SCHEMA;
Condition100%2/2
$schema //= \%Local::Test::SCHEMA
l!l
786
MC/DC100%1/1
$schema //= \%Local::Test::SCHEMA
$schema
Truth table100%2/2
A
A= $schema
Aresult
11
00
35 84 return bless { init_arg => $url, schema => $schema }, $class;
36 }
37  
38 sub schema {
39 3177 ▶ my ( $self, $schema ) = @_;
Subroutine schema: called CC=2 SCAR=6.9
Pod undocumented
40 3177 ▶ if ( $schema ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema) 51 3126 3177
41 51 $self->{schema} = $schema;
42 51 return;
43 }
44 3126 $self->{schema};
45 }
46 sub collections;
47 23 *collections = *schema;
48  
49 sub create {
50 113 ▶ my ( $self, $schema_name, $params ) = @_;
Subroutine create: called CC=17 SCAR=28.3
Pod undocumented
51 113 $params = { %$params };
52 113 my $props = $self->schema->{ $schema_name }{properties};
53 $params->{ $_ } = $props->{ $_ }{default} // undef
54 113 ▶ for grep !exists $params->{ $_ },
Condition100%2/2
$props->{$_}{"default"} // undef
l!l
149171
MC/DC100%1/1
$props->{$_}{"default"} // undef
$props->{$_}{"default"}
Truth table100%2/2
A
A= $props->{$_}{"default"}
Aresult
11
00
55 keys %$props;
56 113 $params = $self->_normalize( $schema_name, $params ); # makes a copy
57  
58 113 ▶ my $id_field = $self->schema->{ $schema_name }{ 'x-id-field' } || 'id';
Condition100%2/2
$self->schema->{$schema_name}{'x-id-field'} || 'id'
l!l
6845
MC/DC100%1/1
$self->schema->{$schema_name}{'x-id-field'} || 'id'
$self->schema->{$schema_name}{'x-id-field'}
Truth table100%2/2
A
A= $self->schema->{$schema_name}{'x-id-field'}
Aresult
11
00
59 113 ▶ my @id_fields = ref $id_field eq 'ARRAY' ? @$id_field : ( $id_field );
Branch: 1 branch
BranchTrueFalseTotal
ref $id_field eq 'ARRAY' ? : 5 108 113
60  
61 # Fill in any auto-increment data...
62 113 for my $id_field ( @id_fields ) {
63 # We haven't provided a value for an integer ID, assume it's autoinc
64 118 ▶ if ( !$params->{ $id_field } and $self->schema->{ $schema_name }{properties}{ $id_field }{type} eq 'integer' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $params->{$id_field} and $self->schema->{$schema_name}{'properties'}{$id_field}{'type'} eq 'integer') { } 55 63 118
elsif ($params->{$id_field} and $id_field ne 'id' and exists $self->schema->{$schema_name}{'properties'}{'id'}) { } 46 23 69
Condition66%2/3
not $params->{$id_field} and $self->schema->{$schema_name}{"properties"}{$id_field}{"type"} eq "integer"
!ll&&!rl&&r
63055
MC/DC50%1/2
not $params->{$id_field} and $self->schema->{$schema_name}{"properties"}{$id_field}{"type"} eq "integer"
not $params->{$id_field} $self->schema->{$schema_name}{"properties"}{$id_field}{"type"} eq "integer"
Truth table66%2/3
not(A) and B
A= not $params->{$id_field}B= $self->schema->{$schema_name}{"properties"}{$id_field}{"type"} eq "integer"
ABresult
0X0
100
111
65 55 my @existing_ids = keys %{ $DATA{ $schema_name } };
66 55 ▶ $params->{ $id_field} = ( max( @existing_ids ) // 0 ) + 1;
Condition100%2/2
&max(@existing_ids) // 0
l!l
4015
MC/DC100%1/1
&max(@existing_ids) // 0
&max(@existing_ids)
Truth table100%2/2
A
A= &max(@existing_ids)
Aresult
11
00
67 }
68 # We have provided another ID, make 'id' another autoinc
69 ▶ elsif ( $params->{ $id_field }
Condition83%5/6
$params->{$id_field} and $id_field ne "id" and exists $self->schema->{$schema_name}{"properties"}{"id"}
!ll&&!rl&&r
61146
$params->{$id_field} and $id_field ne "id"
!ll&&!rl&&r
0657
MC/DC66%2/3
$params->{$id_field} and $id_field ne "id" and exists $self->schema->{$schema_name}{"properties"}{"id"}
$params->{$id_field} $id_field ne "id" exists $self->schema->{$schema_name}{"properties"}{"id"}
Truth table75%3/4
A and B and C
A= $params->{$id_field}B= $id_field ne "id"C= exists $self->schema->{$schema_name}{"properties"}{"id"}
ABCresult
0XX0
10X0
1100
1111
70 && $id_field ne 'id'
71 && exists $self->schema->{ $schema_name }{properties}{id}
72 ) {
73 46 my @existing_ids = map { $_->{ id } } values %{ $DATA{ $schema_name } };
74 46 ▶ $params->{id} = ( max( @existing_ids ) // 0 ) + 1;
Condition100%2/2
&max(@existing_ids) // 0
l!l
2917
MC/DC100%1/1
&max(@existing_ids) // 0
&max(@existing_ids)
Truth table100%2/2
A
A= &max(@existing_ids)
Aresult
11
00
75 }
76 }
77  
78 113 ▶ my $store = $DATA{ $schema_name } //= {};
Condition100%2/2
$Yancy::Backend::Memory::DATA{$schema_name} //= {}
l!l
1112
MC/DC100%1/1
$Yancy::Backend::Memory::DATA{$schema_name} //= {}
$Yancy::Backend::Memory::DATA{$schema_name}
Truth table100%2/2
A
A= $Yancy::Backend::Memory::DATA{$schema_name}
Aresult
11
00
79 113 for my $i ( 0 .. $#id_fields-1 ) {
80 5 ▶ $store = $store->{ $params->{ $id_fields[$i] } } //= {};
Condition100%2/2
$$store{$$params{$id_fields[$i]}} //= {}
l!l
14
MC/DC100%1/1
$$store{$$params{$id_fields[$i]}} //= {}
$$store{$$params{$id_fields[$i]}}
Truth table100%2/2
A
A= $$store{$$params{$id_fields[$i]}}
Aresult
11
00
81 }
82 113 $store->{ $params->{ $id_fields[-1] } } = $params;
83  
84 113 ▶ return @id_fields > 1 ? { map {; $_ => $params->{ $_ } } @id_fields } : $params->{ $id_field };
Branch: 1 branch
BranchTrueFalseTotal
@id_fields > 1 ? : 5 108 113
85 }
86  
87 sub get {
88 325 ▶ my ( $self, $schema_name, $id, %opt ) = @_;
Subroutine get: called CC=12 SCAR=24.9
Pod undocumented
89 325 my $schema = $self->schema->{ $schema_name };
90 325 ▶ my $real_coll = ( $schema->{'x-view'} || {} )->{schema} // $schema_name;
Condition80%4/5
+($schema->{"x-view"} || {})->{"schema"} // $schema_name
l!l&&r!l&&!r
23230
$schema->{'x-view'} || {}
l!l
2323
MC/DC0%0/2
+($schema->{"x-view"} || {})->{"schema"} // $schema_name
$schema->{'x-view'} $schema_name
Truth table66%2/3
A // B
A= $schema->{'x-view'}B= $schema_name
ABresult
1X1
011
000
91  
92 325 ▶ my $id_field = $self->schema->{ $schema_name }{ 'x-id-field' } || 'id';
Condition100%2/2
$self->schema->{$schema_name}{'x-id-field'} || 'id'
l!l
163162
MC/DC100%1/1
$self->schema->{$schema_name}{'x-id-field'} || 'id'
$self->schema->{$schema_name}{'x-id-field'}
Truth table100%2/2
A
A= $self->schema->{$schema_name}{'x-id-field'}
Aresult
11
00
93 325 ▶ my @ids = ref $id_field eq 'ARRAY' ? map { $id->{ $_ } } @$id_field : ( $id );
Branch: 1 branch
BranchTrueFalseTotal
ref $id_field eq 'ARRAY' ? : 13 312 325
94 324 ▶ die "Missing composite ID parts" if @ids > 1 && ( !ref $id || keys %$id < @ids );
Branch: 1 branch
BranchTrueFalseTotal
if @ids > 1 and !ref($id) || keys %$id < @ids 1 323 324
Condition83%5/6
@ids > 1 and !ref($id) || keys %$id < @ids
!ll&&!rl&&r
312111
not ref $id or keys %$id < @ids
l!l&&r!l&&!r
0111
MC/DC66%2/3
@ids > 1 and !ref($id) || keys %$id < @ids
@ids > 1 not ref $id keys %$id < @ids
Truth table75%3/4
A and not(B) or C
A= @ids > 1B= not ref $idC= keys %$id < @ids
ABCresult
0XX0
1000
11X1
1011
95  
96 323 my $item = $DATA{ $real_coll };
97 323 for my $id ( @ids ) {
98 334 ▶ return undef if !defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 3 331 334
99 331 ▶ $item = $item->{ $id } // return undef;
Condition100%2/2
$item->{$id} // (return undef)
l!l
29041
MC/DC100%1/1
$item->{$id} // (return undef)
$item->{$id}
Truth table100%2/2
A
A= $item->{$id}
Aresult
11
00
100 }
101  
102 279 $item = $self->_viewise( $schema_name, $item );
103 279 ▶ if ( my $join = $opt{join} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $join = $opt{"join"}) 3 276 279
104 3 $item = $self->_join( $schema_name, $item, $join );
105 }
106  
107 279 return $item;
108 }
109  
110 sub _join {
111 19 ▶ my ( $self, $schema_name, $item, $join, $where ) = @_;
Subroutine _join: called CC=13 SCAR=25.7
112 19 $item = { %$item };
113 19 my $schema = $self->schema->{ $schema_name };
114 19 ▶ my $id_field = $self->schema->{ $schema_name }{ 'x-id-field' } || 'id';
Condition50%1/2
$self->schema->{$schema_name}{'x-id-field'} || 'id'
l!l
190
MC/DC0%0/1
$self->schema->{$schema_name}{'x-id-field'} || 'id'
$self->schema->{$schema_name}{'x-id-field'}
Truth table50%1/2
A
A= $self->schema->{$schema_name}{'x-id-field'}
Aresult
11
00
115 19 ▶ my @joins = ref $join eq 'ARRAY' ? @$join : ( $join );
Branch: 1 branch
BranchTrueFalseTotal
ref $join eq 'ARRAY' ? : 7 12 19
116 19 for my $join ( @joins ) {
117 26 ▶ if ( my $join_prop = $schema->{ properties }{ $join } ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $join_prop = $schema->{'properties'}{$join}) { } 21 5 26
elsif (my $join_schema = $self->schema->{$join}) { } 5 0 5
118 21 ▶ my $join_id = $item->{ $join } || next;
Condition100%2/2
$item->{$join} || (next)
l!l
174
MC/DC100%1/1
$item->{$join} || (next)
$item->{$join}
Truth table100%2/2
A
A= $item->{$join}
Aresult
11
00
119 17 my $join_schema_name = $join_prop->{'x-foreign-key'};
120 17 $item->{ $join } = $self->get( $join_schema_name, $join_id );
121 17 for my $key ( grep /^${join}\./, keys %$where ) {
122 7 my ( $k ) = $key =~ /^${join}\.(.+)$/;
123 7 ▶ if ( !match( { $k => $where->{ $key } }, $item->{ $join } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (match({$k, $where->{$key}}, $item->{$join})) 2 5 7
124 # Inner match fails, so this row is not in the
125 # results
126 2 return;
127 }
128 }
129 }
130 elsif ( my $join_schema = $self->schema->{ $join } ) {
131 5 my $join_schema_name = $join;
132 5 ▶ my ( $join_id_field ) = grep { ( $join_schema->{properties}{$_}{'x-foreign-key'}//'' ) eq $schema_name } keys %{ $join_schema->{properties} };
Condition100%2/2
$join_schema->{"properties"}{$_}{"x-foreign-key"} // ""
l!l
1010
MC/DC100%1/1
$join_schema->{"properties"}{$_}{"x-foreign-key"} // ""
$join_schema->{"properties"}{$_}{"x-foreign-key"}
Truth table100%2/2
A
A= $join_schema->{"properties"}{$_}{"x-foreign-key"}
Aresult
11
00
133 5 ▶ my $join_where = ref $id_field eq 'ARRAY' ? { map { $_ => $item->{ $_ } } @$join_id_field } : { $join_id_field => $item->{$join_id_field} };
Branch: 1 branch
BranchTrueFalseTotal
ref $id_field eq 'ARRAY' ? : 0 5 5
134 5 my $min_items = 0;
135 5 for my $key ( grep /^${join}\./, keys %$where ) {
136 2 my ( $k ) = $key =~ /^${join}\.(.+)$/;
137 2 $join_where->{ $k } = $where->{ $key };
138 2 $min_items = 1;
139 }
140 5 my $res = $self->list( $join_schema_name, $join_where );
141 5 ▶ return if $res->{total} < $min_items;
Branch: 1 branch
BranchTrueFalseTotal
if $res->{"total"} < $min_items 1 4 5
142 4 $item->{ $join } = $res->{items};
143 }
144 }
145 16 return $item;
146 }
147  
148 sub _viewise {
149 625 ▶ my ( $self, $schema_name, $item, $join ) = @_;
Subroutine _viewise: called CC=8 SCAR=20.8
150 625 $item = dclone $item;
151 625 my $schema = $self->schema->{ $schema_name };
152 625 ▶ my $real_coll = ( $schema->{'x-view'} || {} )->{schema} // $schema_name;
Condition80%4/5
+($schema->{"x-view"} || {})->{"schema"} // $schema_name
l!l&&r!l&&!r
36220
$schema->{'x-view'} || {}
l!l
3622
MC/DC0%0/2
+($schema->{"x-view"} || {})->{"schema"} // $schema_name
$schema->{'x-view'} $schema_name
Truth table66%2/3
A // B
A= $schema->{'x-view'}B= $schema_name
ABresult
1X1
011
000
153 my %props = %{
154 625 $schema->{properties} || $self->schema->{ $real_coll }{properties}
155 625 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
$schema->{"properties"} or $self->schema->{$real_coll}{"properties"} 1 624 625
156 625 ▶ if ( $join ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($join) 12 613 625
157 12 ▶ $props{ $_ } = 1 for @{ ref $join eq 'ARRAY' ? $join : [ $join ] };
Branch: 1 branch
BranchTrueFalseTotal
ref $join eq 'ARRAY' ? : 4 8 12
158 }
159 625 delete $item->{$_} for grep !$props{ $_ }, keys %$item;
160 625 $item;
161 }
162  
163 sub list {
164 327 ▶ my ( $self, $schema_name, $params, @opt ) = @_;
Subroutine list: called CC=18 SCAR=29.0
Pod undocumented
165 327 ▶ my $opt = @opt % 2 == 0 ? {@opt} : $opt[0];
Branch: 1 branch
BranchTrueFalseTotal
@opt % 2 == 0 ? : 152 175 327
166 327 my $schema = $self->schema->{ $schema_name };
167 327 ▶ die "list attempted on non-existent schema '$schema_name'" unless $schema;
Branch: 1 branch
BranchTrueFalseTotal
unless $schema 0 327 327
168 327 ▶ $params ||= {};
Condition100%2/2
$params ||= {}
l!l
208119
MC/DC100%1/1
$params ||= {}
$params
Truth table100%2/2
A
A= $params
Aresult
11
00
169  
170 327 ▶ my $id_field = $self->schema->{ $schema_name }{ 'x-id-field' } || 'id';
Condition100%2/2
$self->schema->{$schema_name}{'x-id-field'} || 'id'
l!l
172155
MC/DC100%1/1
$self->schema->{$schema_name}{'x-id-field'} || 'id'
$self->schema->{$schema_name}{'x-id-field'}
Truth table100%2/2
A
A= $self->schema->{$schema_name}{'x-id-field'}
Aresult
11
00
171 327 ▶ my @id_fields = ref $id_field eq 'ARRAY' ? @$id_field : ( $id_field );
Branch: 1 branch
BranchTrueFalseTotal
ref $id_field eq 'ARRAY' ? : 8 319 327
172  
173 327 ▶ my $real_coll = ( $schema->{'x-view'} || {} )->{schema} // $schema_name;
Condition80%4/5
+($schema->{"x-view"} || {})->{"schema"} // $schema_name
l!l&&r!l&&!r
13260
$schema->{'x-view'} || {}
l!l
1326
MC/DC0%0/2
+($schema->{"x-view"} || {})->{"schema"} // $schema_name
$schema->{'x-view'} $schema_name
Truth table66%2/3
A // B
A= $schema->{'x-view'}B= $schema_name
ABresult
1X1
011
000
174 my $props = $schema->{properties}
175 327 ▶ || $self->schema->{ $real_coll }{properties};
Condition33%1/3
$schema->{'properties'} || $self->schema->{$real_coll}{'properties'}
l!l&&r!l&&!r
32700
MC/DC0%0/2
$schema->{'properties'} || $self->schema->{$real_coll}{'properties'}
$schema->{'properties'} $self->schema->{$real_coll}{'properties'}
Truth table33%1/3
A || B
A= $schema->{'properties'}B= $self->schema->{$real_coll}{'properties'}
ABresult
1X1
011
000
176 327 my @rows = values %{ $DATA{ $real_coll } };
177 327 for my $id_field ( 1..$#id_fields ) {
178 8 @rows = map values %$_, @rows;
179 }
180 327 ▶ if ( $opt->{join} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"join"}) 6 321 327
181 6 @rows = map $self->_join( $schema_name, $_, $opt->{join}, $params ), @rows;
182 }
183 # Join queries have been resolved
184 327 ▶ for my $p ( ref $params eq 'ARRAY' ? @$params : ( $params ) ) {
Branch: 1 branch
BranchTrueFalseTotal
ref $params eq 'ARRAY' ? : 1 326 327
185 328 for my $key ( grep /\./, keys %$p ) {
186 4 delete $p->{ $key };
187 4 my ( $j ) = split /\./, $key;
188 4 $p->{ $j } = { '!=' => undef };
189 }
190 }
191 my $matched_rows = order_by(
192 $opt->{order_by} // \@id_fields,
193 327 ▶ [ grep { match( $params, $_ ) } @rows ],
Condition100%2/2
$opt->{"order_by"} // \@id_fields
l!l
41286
MC/DC100%1/1
$opt->{"order_by"} // \@id_fields
$opt->{"order_by"}
Truth table100%2/2
A
A= $opt->{"order_by"}
Aresult
11
00
194 );
195 327 ▶ my $first = $opt->{offset} // 0;
Condition100%2/2
$opt->{"offset"} // 0
l!l
77250
MC/DC100%1/1
$opt->{"offset"} // 0
$opt->{"offset"}
Truth table100%2/2
A
A= $opt->{"offset"}
Aresult
11
00
196 327 ▶ my $last = $opt->{limit} ? $opt->{limit} + $first - 1 : $#$matched_rows;
Branch: 1 branch
BranchTrueFalseTotal
$opt->{'limit'} ? : 130 197 327
197 327 ▶ if ( $last > $#$matched_rows ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last > $matched_rows->$#*) 57 270 327
198 57 $last = $#$matched_rows;
199 }
200 327 my @items = map $self->_viewise( $schema_name, $_, $opt->{join} ), @$matched_rows[ $first .. $last ];
201 327 my $retval = {
202 items => \@items,
203 total => scalar @$matched_rows,
204 };
205 #; use Data::Dumper;
206 #; say Dumper $retval;
207 327 return $retval;
208 }
209  
210 sub set {
211 58 ▶ my ( $self, $schema_name, $id, $params ) = @_;
Subroutine set: called CC=14 SCAR=26.4
Pod undocumented
212 58 ▶ my $id_field = $self->schema->{ $schema_name }{ 'x-id-field' } || 'id';
Condition100%2/2
$self->schema->{$schema_name}{'x-id-field'} || 'id'
l!l
2929
MC/DC100%1/1
$self->schema->{$schema_name}{'x-id-field'} || 'id'
$self->schema->{$schema_name}{'x-id-field'}
Truth table100%2/2
A
A= $self->schema->{$schema_name}{'x-id-field'}
Aresult
11
00
213 58 ▶ my @id_fields = ref $id_field eq 'ARRAY' ? @$id_field : ( $id_field );
Branch: 1 branch
BranchTrueFalseTotal
ref $id_field eq 'ARRAY' ? : 4 54 58
214 58 ▶ die "Missing composite ID parts" if @id_fields > 1 && ( !ref $id || keys %$id < @id_fields );
Branch: 1 branch
BranchTrueFalseTotal
if @id_fields > 1 and !ref($id) || keys %$id < @id_fields 2 56 58
Condition100%6/6
@id_fields > 1 and !ref($id) || keys %$id < @id_fields
!ll&&!rl&&r
5431
not ref $id or keys %$id < @id_fields
l!l&&r!l&&!r
112
MC/DC66%2/3
@id_fields > 1 and !ref($id) || keys %$id < @id_fields
@id_fields > 1 not ref $id keys %$id < @id_fields
Truth table75%3/4
A and not(B) or C
A= @id_fields > 1B= not ref $idC= keys %$id < @id_fields
ABCresult
0XX0
1000
11X1
1011
215  
216 # Fill in any missing params from the ID
217 56 for my $id_field ( @id_fields ) {
218 58 ▶ my $id_part = ref $id eq 'HASH' ? $id->{ $id_field } : $id;
Branch: 1 branch
BranchTrueFalseTotal
ref $id eq 'HASH' ? : 4 54 58
219 58 ▶ if ( !$params->{ $id_field } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($params->{$id_field}) 34 24 58
220 34 $params->{ $id_field } = $id_part;
221 }
222 }
223  
224 56 $params = $self->_normalize( $schema_name, $params );
225  
226 56 my $store = $DATA{ $schema_name };
227 56 for my $i ( 0..$#id_fields-1 ) {
228 2 my $id_field = $id_fields[ $i ];
229 2 ▶ my $id_part = ref $id eq 'HASH' ? $id->{ $id_field } : $id;
Branch: 1 branch
BranchTrueFalseTotal
ref $id eq 'HASH' ? : 2 0 2
230 2 ▶ return 0 if !$store->{ $id_part };
Branch: 1 branch
BranchTrueFalseTotal
unless $store->{$id_part} 0 2 2
231 # Update the item's ID if it changes
232 2 my $item = delete $store->{ $id_part };
233 2 $store->{ $params->{ $id_field } } = $item;
234 2 $store = $item;
235 }
236 56 ▶ my $id_part = ref $id eq 'HASH' ? $id->{ $id_fields[-1] } : $id;
Branch: 1 branch
BranchTrueFalseTotal
ref $id eq 'HASH' ? : 2 54 56
237 56 ▶ return 0 if !$store->{ $id_part };
Branch: 1 branch
BranchTrueFalseTotal
unless $store->{$id_part} 10 46 56
238 $store->{ $params->{ $id_fields[-1] } } = {
239 46 %{ delete $store->{ $id_part } },
240 %$params,
241 };
242  
243 46 return 1;
244 }
245  
246 sub delete {
247 94 ▶ my ( $self, $schema_name, $id ) = @_;
Subroutine delete: called CC=12 SCAR=24.9
Pod undocumented
248 94 ▶ return 0 if !$id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 94 94
249 94 ▶ my $id_field = $self->schema->{ $schema_name }{ 'x-id-field' } || 'id';
Condition100%2/2
$self->schema->{$schema_name}{'x-id-field'} || 'id'
l!l
5044
MC/DC100%1/1
$self->schema->{$schema_name}{'x-id-field'} || 'id'
$self->schema->{$schema_name}{'x-id-field'}
Truth table100%2/2
A
A= $self->schema->{$schema_name}{'x-id-field'}
Aresult
11
00
250 94 ▶ my @id_fields = ref $id_field eq 'ARRAY' ? @$id_field : ( $id_field );
Branch: 1 branch
BranchTrueFalseTotal
ref $id_field eq 'ARRAY' ? : 4 90 94
251 94 ▶ die "Missing composite ID parts" if @id_fields > 1 && ( !ref $id || keys %$id < @id_fields );
Branch: 1 branch
BranchTrueFalseTotal
if @id_fields > 1 and !ref($id) || keys %$id < @id_fields 2 92 94
Condition100%6/6
@id_fields > 1 and !ref($id) || keys %$id < @id_fields
!ll&&!rl&&r
9031
not ref $id or keys %$id < @id_fields
l!l&&r!l&&!r
112
MC/DC66%2/3
@id_fields > 1 and !ref($id) || keys %$id < @id_fields
@id_fields > 1 not ref $id keys %$id < @id_fields
Truth table75%3/4
A and not(B) or C
A= @id_fields > 1B= not ref $idC= keys %$id < @id_fields
ABCresult
0XX0
1000
11X1
1011
252 92 my $store = $DATA{ $schema_name };
253 92 for my $i ( 0..$#id_fields-1 ) {
254 2 my $id_field = $id_fields[ $i ];
255 2 ▶ my $id_part = ref $id eq 'HASH' ? $id->{ $id_field } : $id;
Branch: 1 branch
BranchTrueFalseTotal
ref $id eq 'HASH' ? : 2 0 2
256 2 ▶ $store = $store->{ $id_part } // return 0;
Condition50%1/2
$store->{$id_part} // (return 0)
l!l
20
MC/DC0%0/1
$store->{$id_part} // (return 0)
$store->{$id_part}
Truth table50%1/2
A
A= $store->{$id_part}
Aresult
11
00
257 }
258 92 ▶ my $id_part = ref $id eq 'HASH' ? $id->{ $id_fields[-1] } : $id;
Branch: 1 branch
BranchTrueFalseTotal
ref $id eq 'HASH' ? : 2 90 92
259 92 ▶ return 0 if !$store->{ $id_part };
Branch: 1 branch
BranchTrueFalseTotal
unless $store->{$id_part} 22 70 92
260 70 return !!delete $store->{ $id_part };
261 }
262  
263 sub _normalize {
264 169 ▶ my ( $self, $schema_name, $data ) = @_;
Subroutine _normalize: called CC=10 SCAR=23.0
265 169 ▶ return undef if !$data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 169 169
266 169 my $schema = $self->schema->{ $schema_name }{ properties };
267 169 my %replace;
268 169 for my $key ( keys %$data ) {
269 1035 ▶ next if !defined $data->{ $key }; # leave nulls alone
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data->{$key} 216 819 1035
270 819 my ( $type, $format ) = @{ $schema->{ $key } }{qw( type format )};
271 819 ▶ if ( is_type( $type, 'boolean' ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_type($type, 'boolean')) { } 56 763 819
elsif (is_type($type, 'string') and is_format($format, 'date-time')) { } 70 800 870
272 # Boolean: true (1, "true"), false (0, "false")
273 $replace{ $key }
274 56 ▶ = $data->{ $key } && $data->{ $key } !~ /^false$/i
Branch: 1 branch
BranchTrueFalseTotal
$data->{$key} && !($data->{$key} =~ /^false$/iu) ? : 24 32 56
Condition100%3/3
$data->{$key} and not $data->{$key} =~ /^false$/iu
!ll&&!rl&&r
30224
MC/DC100%2/2
$data->{$key} and not $data->{$key} =~ /^false$/iu
$data->{$key} not $data->{$key} =~ /^false$/iu
Truth table100%3/3
A and not(B)
A= $data->{$key}B= not $data->{$key} =~ /^false$/iu
ABresult
0X0
100
111
275 ? 1 : 0;
276 }
277 ▶ elsif ( is_type( $type, 'string' ) && is_format( $format, 'date-time' ) ) {
Condition100%3/3
is_type($type, "string") and is_format($format, "date-time")
!ll&&!rl&&r
10758670
MC/DC100%2/2
is_type($type, "string") and is_format($format, "date-time")
is_type($type, "string") is_format($format, "date-time")
Truth table100%3/3
A and B
A= is_type($type, "string")B= is_format($format, "date-time")
ABresult
0X0
100
111
278 70 ▶ if ( $data->{ $key } eq 'now' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{$key} eq "now") 57 13 70
279 57 $replace{ $key } = Time::Piece->new->datetime;
280 }
281 }
282 }
283 169 +{ %$data, %replace };
284 }
285  
286 # Some databases can know other formats
287 23 my %db_formats = map { $_ => 1 } qw( date time date-time binary );
288  
289 sub read_schema {
290 104 ▶ my ( $self, @table_names ) = @_;
Subroutine read_schema: called CC=7 SCAR=19.5
Pod undocumented
291 104 ▶ my $schema = %Local::Test::SCHEMA ? \%Local::Test::SCHEMA : $self->schema;
Branch: 1 branch
BranchTrueFalseTotal
%Local::Test::SCHEMA ? : 104 0 104
292 104 my $cloned = dclone $schema;
293 104 delete @$cloned{@Local::Test::SCHEMA_ADDED_COLLS}; # ones not in the "database" at all
294 # zap all things that DB can't know about
295 104 for my $c ( values %$cloned ) {
296 463 delete $c->{'x-list-columns'};
297 463 for my $p ( values %{ $c->{properties} } ) {
298 2890 delete @$p{ qw(description pattern title) };
299 2890 ▶ if ( $p->{format} && !$db_formats{ $p->{format} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{"format"} and not $db_formats{$p->{"format"}}) 399 2491 2890
Condition100%3/3
$p->{"format"} and not $db_formats{$p->{"format"}}
!ll&&!rl&&r
2272219399
MC/DC100%2/2
$p->{"format"} and not $db_formats{$p->{"format"}}
$p->{"format"} not $db_formats{$p->{"format"}}
Truth table100%3/3
A and not(B)
A= $p->{"format"}B= not $db_formats{$p->{"format"}}
ABresult
0X0
100
111
300 399 delete $p->{format};
301 }
302 }
303 }
304 104 ▶ return @table_names ? @$cloned{ @table_names } : $cloned;
Branch: 1 branch
BranchTrueFalseTotal
@table_names ? : 7 97 104
305 }
306  
307 1 ▶sub supports { grep { $_[1] eq $_ } 'complex-type' }
Subroutine supports: called CC=1 SCAR=0.0
Pod undocumented
308  
309 23 1;
310  
311 __END__
« Yancy/Plugin/Form/Bootstrap4.pm ↑ Summary Yancy/Model/Schema.pm »