Yancy/Model.pm

filter: Statementstmt 92.5% 112 / 121 Branchbran 75.0% 27 / 36 Conditioncond 76.4% 13 / 17 MC/DCmcdc 50.0% 5 / 10 Subroutinesub 89.4% 17 / 19 Podpod 100% 6 / 6 totaltotal 86.1% 180 / 209 CC 32 SCAR 18.0
CC
32
Cov
87%
find_class
10.7
_merge_schema
10.0
schema
6.2
CRAP 6.0
« Mojolicious/Plugin/Yancy.pm ↑ Summary Yancy/Plugin/Auth/Basic.pm »
1 package Yancy::Model;
2 22 our $VERSION = '1.087';
3 # ABSTRACT: Model layer for Yancy apps
4  
5 #pod =head1 SYNOPSIS
6 #pod
7 #pod # XXX: Allow using backend strings
8 #pod my $model = Yancy::Model->new( backend => $backend );
9 #pod
10 #pod my $schema = $model->schema( 'foo' );
11 #pod
12 #pod my $id = $schema->create( $data );
13 #pod my $count = $schema->delete( $id );
14 #pod my $count = $schema->delete( $where );
15 #pod my $count = $schema->set( $id, $data );
16 #pod my $count = $schema->set( $where, $data );
17 #pod
18 #pod my $item = $schema->get( $id );
19 #pod my ( $items, $total ) = $schema->list( $where, $opts );
20 #pod for my $item ( @$items ) {
21 #pod }
22 #pod
23 #pod my $success = $row->set( $data );
24 #pod my $success = $row->delete();
25 #pod my $data = $row->to_hash;
26 #pod
27 #pod =head1 DESCRIPTION
28 #pod
29 #pod B<NOTE>: This module is experimental and its API may change before
30 #pod Yancy v2!
31 #pod
32 #pod L<Yancy::Model> is a framework for your business logic. L<Yancy::Model>
33 #pod contains a number of schemas, L<Yancy::Model::Schema> objects. Each
34 #pod schema contains a number of items, L<Yancy::Model::Item> objects.
35 #pod
36 #pod For information on how to extend this module to add your own schema
37 #pod and item methods, see L<Yancy::Guides::Model>.
38 #pod
39 #pod =head1 SEE ALSO
40 #pod
41 #pod L<Yancy::Guides::Model>
42 #pod
43 #pod =cut
44  
45 22 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 22 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
47 22 ▶use Mojo::Util qw( camelize );
Subroutine BEGIN: called CC=1 SCAR=0.0
48 22 ▶use Mojo::Loader qw( load_class );
Subroutine BEGIN: called CC=1 SCAR=0.0
49 22 ▶use Mojo::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 22 ▶use Yancy::Util qw( derp );
Subroutine BEGIN: called CC=1 SCAR=0.0
51 22 ▶use Storable qw( dclone );
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 #pod =attr backend
54 #pod
55 #pod A L<Yancy::Backend> object.
56 #pod
57 #pod =cut
58  
59 0 ▶has backend => sub { die "backend is required" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
60  
61 #pod =attr namespaces
62 #pod
63 #pod An array of namespaces to find Schema and Item classes. Defaults to C<[ 'Yancy::Model' ]>.
64 #pod
65 #pod =cut
66  
67 101 ▶has namespaces => sub { [qw( Yancy::Model )] };
Subroutine __ANON__: called CC=1 SCAR=0.0
68  
69 #pod =attr log
70 #pod
71 #pod A L<Mojo::Log> object to log messages to.
72 #pod
73 #pod =cut
74  
75 0 ▶has log => sub { Mojo::Log->new };
Subroutine __ANON__: not called CC=1 SCAR=1.2
76  
77 103 ▶has _schema => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
78 7 ▶has _config_schema => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
79 22 has _auto_read => 1;
80  
81 #pod =method new
82 #pod
83 #pod Create a new model with the given backend. In addition to any L</ATTRIBUTES>, these
84 #pod options may be given:
85 #pod
86 #pod =over
87 #pod
88 #pod =item schema
89 #pod
90 #pod A JSON schema configuration. By default, the information from
91 #pod L</read_schema> will be merged with this information. See
92 #pod L<Yancy::Guides::Schema/Documenting Your Schema> for more information.
93 #pod
94 #pod =item read_schema
95 #pod
96 #pod Read the backend database information to build the schema information.
97 #pod Enabled by default. Set to a false value to disable.
98 #pod
99 #pod =back
100 #pod
101 #pod =cut
102  
103 sub new {
104 106 ▶ my ( $class, @args ) = @_;
Subroutine new: called CC=6 SCAR=18.0
Pod documented
105 106 ▶ my %args = @args == 1 ? %{ $args[0] } : @args;
Branch: 1 branch
BranchTrueFalseTotal
@args == 1 ? : 0 106 106
106 106 ▶ my $conf = $args{_config_schema} = delete $args{schema} if $args{schema};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"schema"} 99 7 106
107 106 ▶ my $read = exists $args{read_schema} ? delete $args{read_schema} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'read_schema'} ? : 104 2 106
108 106 $args{ _auto_read } = $read;
109 106 my $self = $class->SUPER::new(\%args);
110 106 ▶ if ( $read ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($read) { } 92 14 106
elsif (my(@names) = grep({delete $conf->{$_}{'read_schema'};} keys %$conf)) { } 1 13 14
111 92 $self->read_schema;
112 }
113 54 elsif ( my @names = grep { delete $conf->{$_}{read_schema} } keys %$conf ) {
114 1 $self->read_schema( @names );
115 }
116 106 return $self;
117 }
118  
119 #pod =method find_class
120 #pod
121 #pod Find a class of the given type for an object of the given name. The name is run
122 #pod through L<Mojo::Util/camelize> before lookups.
123 #pod
124 #pod unshift @{ $model->namespaces }, 'MyApp';
125 #pod # MyApp::Schema::User
126 #pod $class = $model->find_class( Schema => 'user' );
127 #pod # MyApp::Item::UserProfile
128 #pod $class = $model->find_class( Item => 'user_profile' );
129 #pod
130 #pod If a specific class cannot be found, a generic class for the type is found instead.
131 #pod
132 #pod # MyApp::Schema
133 #pod $class = $model->find_class( Schema => 'not_found' );
134 #pod # MyApp::Item
135 #pod $class = $model->find_class( Item => 'not_found' );
136 #pod
137 #pod =cut
138  
139 sub find_class {
140 516 ▶ my ( $self, $type, $name ) = @_;
Subroutine find_class: called CC=7 SCAR=23.7
Pod documented
141 # First, a specific class for this named type.
142 516 for my $namespace ( @{ $self->namespaces } ) {
143 516 my $class = "${namespace}::${type}::" . camelize( $name );
144 516 ▶ if ( my $e = load_class $class ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = load_class($class)) 516 0 516
145 516 ▶ if ( ref $e ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $e) 0 516 516
146 0 die "Could not load $class: $e";
147 }
148 # Not found, try the next one
149 516 next;
150 }
151 # No error, so this is the class we want
152 0 return $class;
153 }
154  
155 # Finally, try to find a generic type class
156 516 for my $namespace ( @{ $self->namespaces } ) {
157 516 my $class = "${namespace}::${type}";
158 516 ▶ if ( my $e = load_class $class ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = load_class($class)) 0 516 516
159 0 ▶ if ( ref $e ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $e) 0 0 0
160 0 die "Could not load $class: $e";
161 }
162 # Not found, try the next one
163 0 next;
164 }
165 # No error, so this is the class we want
166 516 return $class;
167 }
168  
169 0 die "Could not find class for type $type or name $name";
170 }
171  
172 #pod =method read_schema
173 #pod
174 #pod Read the schema from the L</backend> and prepare schema objects using L</find_class>
175 #pod to find the correct classes.
176 #pod
177 #pod =cut
178  
179 sub read_schema {
180 93 ▶ my ( $self, @names ) = @_;
Subroutine read_schema: called CC=6 SCAR=17.9
Pod documented
181 93 my $conf_schema = $self->_config_schema;
182 93 my $read_schema;
183 93 ▶ if ( @names ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@names) { } 1 92 93
184 1 $read_schema = { map { $_ => $self->backend->read_schema( $_ ) } @names };
185 }
186 else {
187 92 $read_schema = $self->backend->read_schema( @names );
188 92 my %all_schemas = map { $_ => 1 } keys %$conf_schema, keys %$read_schema;
189 92 @names = keys %all_schemas;
190 }
191  
192 # Make all concrete schemas first, then any views.
193 # XXX: x-view is deprecated. Remove the sort in v2.
194 93 @names = sort { !!$conf_schema->{$a}{'x-view'} <=> !!$conf_schema->{$b}{'x-view'} } @names;
195  
196 93 for my $name ( @names ) {
197 # ; use Data::Dumper;
198 # ; say "Creating schema $name";
199 # ; say "Has view " . Dumper $conf_schema->{$name}{'x-view'} if $conf_schema->{$name}{'x-view'};
200 560 ▶ my $full_schema = _merge_schema( $conf_schema->{ $name } // {}, $read_schema->{ $name } // {} );
Condition75%3/4
$conf_schema->{$name} // {}
l!l
5600
$read_schema->{$name} // {}
l!l
419141
MC/DC0%0/1
$conf_schema->{$name} // {}
$conf_schema->{$name}
MC/DC100%1/1
$read_schema->{$name} // {}
$read_schema->{$name}
Truth table50%1/2
A
A= $conf_schema->{$name}
Aresult
11
00
Truth table100%2/2
A
A= $read_schema->{$name}
Aresult
11
00
201 560 ▶ if ( $full_schema->{'x-ignore'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($full_schema->{"x-ignore"}) 94 466 560
202 # Remember we're ignoring this schema
203 94 $conf_schema->{ $name }{ 'x-ignore' } = 1;
204 94 next;
205 }
206 466 $self->schema( $name, $full_schema );
207 }
208 93 return $self;
209 }
210  
211 #pod =method schema
212 #pod
213 #pod Get or set a schema object.
214 #pod
215 #pod $model = $model->schema( user => MyApp::Model::User->new );
216 #pod $schema = $model->schema( 'user' );
217 #pod
218 #pod =cut
219  
220 sub schema {
221 1977 ▶ my ( $self, $name, $data ) = @_;
Subroutine schema: called CC=6 SCAR=18.2
Pod documented
222 1977 ▶ if ( !$data ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data) 1486 491 1977
223 1486 ▶ if ( my $schema = $self->_schema->{ $name } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $schema = $self->_schema->{$name}) 1461 25 1486
224 1461 return $schema;
225 }
226 # Create a default schema
227 25 my $conf = $self->_config_schema->{ $name };
228 25 ▶ die "Schema $name is ignored" if $conf->{'x-ignore'};
Branch: 1 branch
BranchTrueFalseTotal
if $conf->{"x-ignore"} 0 25 25
229 25 $self->schema( $name, $conf );
230 23 return $self->_schema->{ $name };
231 }
232 491 ▶ if ( !blessed $data || !$data->isa( 'Yancy::Model::Schema' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($data) and $data->isa("Yancy::Model::Schema")) 491 0 491
Condition33%1/3
builtin::blessed($data) and $data->isa("Yancy::Model::Schema")
!ll&&!rl&&r
49100
MC/DC0%0/2
builtin::blessed($data) and $data->isa("Yancy::Model::Schema")
builtin::blessed($data) $data->isa("Yancy::Model::Schema")
Truth table33%1/3
A and B
A= builtin::blessed($data)B= $data->isa("Yancy::Model::Schema")
ABresult
0X0
100
111
233 491 my $class = $self->find_class( Schema => $name );
234 491 $data = $class->new( model => $self, name => $name, json_schema => $data );
235 }
236 489 $self->_schema->{$name} = $data;
237 489 return $self;
238 }
239  
240 #pod =method json_schema
241 #pod
242 #pod Get the JSON Schema for every attached schema.
243 #pod
244 #pod =cut
245  
246 sub json_schema {
247 51 ▶ my ( $self, @names ) = @_;
Subroutine json_schema: called CC=2 SCAR=7.0
Pod documented
248 51 ▶ if ( !@names ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@names) 51 0 51
249 51 @names = $self->schema_names;
250 }
251 return {
252 51 map { $_ => $self->schema( $_ )->json_schema } @names
253 };
254 }
255  
256 #pod =method schema_names
257 #pod
258 #pod Get a list of all the schema names.
259 #pod
260 #pod =cut
261  
262 sub schema_names {
263 144 ▶ my ( $self ) = @_;
Subroutine schema_names: called CC=1 SCAR=0.0
Pod documented
264 144 my $conf = $self->_config_schema;
265 144 my %all_schemas = map { $_ => 1 } grep !$conf->{$_}{'x-ignore'}, keys %$conf, keys %{ $self->_schema };
266 144 return keys %all_schemas;
267 }
268  
269 # _merge_schema( $keep, $merge );
270 #
271 # Merge the two schemas, returning the result.
272 sub _merge_schema {
273 560 ▶ my ( $left, $right ) = @_;
Subroutine _merge_schema: called CC=10 SCAR=23.0
274 # ; use Data::Dumper;
275 # ; say "Split schema " . Dumper( $left ) . Dumper( $right );
276 560 my $keep = dclone $left;
277 560 ▶ if ( my $right_props = $right->{properties} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $right_props = $right->{"properties"}) 419 141 560
278 419 ▶ my $keep_props = $keep->{properties} ||= {};
Condition100%2/2
$keep->{"properties"} ||= {}
l!l
34871
MC/DC100%1/1
$keep->{"properties"} ||= {}
$keep->{"properties"}
Truth table100%2/2
A
A= $keep->{"properties"}
Aresult
11
00
279 419 for my $p ( keys %{ $right_props } ) {
280 2618 ▶ my $keep_prop = $keep_props->{ $p } ||= {};
Condition100%2/2
$keep_props->{$p} ||= {}
l!l
2224394
MC/DC100%1/1
$keep_props->{$p} ||= {}
$keep_props->{$p}
Truth table100%2/2
A
A= $keep_props->{$p}
Aresult
11
00
281 2618 my $right_prop = $right_props->{ $p };
282 2618 for my $k ( keys %$right_prop ) {
283 6742 ▶ $keep_prop->{ $k } ||= $right_prop->{ $k };
Condition100%3/3
$keep_prop->{$k} ||= $right_prop->{$k}
l!l&&r!l&&!r
5413964365
MC/DC100%2/2
$keep_prop->{$k} ||= $right_prop->{$k}
$keep_prop->{$k} $right_prop->{$k}
Truth table100%3/3
A ||= B
A= $keep_prop->{$k}B= $right_prop->{$k}
ABresult
1X1
011
000
284 }
285 }
286 }
287 560 for my $k ( keys %$right ) {
288 1667 ▶ next if $k eq 'properties';
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "properties" 419 1248 1667
289 1248 ▶ $keep->{ $k } ||= $right->{ $k };
Condition66%2/3
$keep->{$k} ||= $right->{$k}
l!l&&r!l&&!r
10372110
MC/DC0%0/2
$keep->{$k} ||= $right->{$k}
$keep->{$k} $right->{$k}
Truth table66%2/3
A ||= B
A= $keep->{$k}B= $right->{$k}
ABresult
1X1
011
000
290 }
291 # ; use Data::Dumper;
292 # ; say "Merged schema " . Dumper $keep;
293 560 return $keep;
294 }
295  
296 22 1;
297  
298 __END__
« Mojolicious/Plugin/Yancy.pm ↑ Summary Yancy/Plugin/Auth/Basic.pm »