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.088';
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 103 ▶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 105 ▶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 108 ▶sub new {
Subroutine new: called CC=6 SCAR=18.0
Pod documented
104 108 my ( $class, @args ) = @_;
105 0 ▶ my %args = @args == 1 ? %{ $args[0] } : @args;
Branch: 1 branch
BranchTrueFalseTotal
@args == 1 ? : 0 108 108
106 108 ▶ my $conf = $args{_config_schema} = delete $args{schema} if $args{schema};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"schema"} 101 7 108
107 108 ▶ my $read = exists $args{read_schema} ? delete $args{read_schema} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'read_schema'} ? : 106 2 108
108 108 $args{ _auto_read } = $read;
109 108 my $self = $class->SUPER::new(\%args);
110 108 ▶ if ( $read ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($read) { } 94 14 108
elsif (my(@names) = grep({delete $conf->{$_}{'read_schema'};} keys %$conf)) { } 1 13 14
111 94 $self->read_schema;
112 }
113 54 elsif ( my @names = grep { delete $conf->{$_}{read_schema} } keys %$conf ) {
114 1 $self->read_schema( @names );
115 }
116 108 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 601 ▶sub find_class {
Subroutine find_class: called CC=7 SCAR=23.7
Pod documented
140 601 my ( $self, $type, $name ) = @_;
141 # First, a specific class for this named type.
142 601 for my $namespace ( @{ $self->namespaces } ) {
143 601 my $class = "${namespace}::${type}::" . camelize( $name );
144 601 ▶ if ( my $e = load_class $class ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = load_class($class)) 601 0 601
145 601 ▶ if ( ref $e ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $e) 0 601 601
146 0 die "Could not load $class: $e";
147 }
148 # Not found, try the next one
149 601 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 601 for my $namespace ( @{ $self->namespaces } ) {
157 601 my $class = "${namespace}::${type}";
158 601 ▶ if ( my $e = load_class $class ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = load_class($class)) 0 601 601
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 601 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 95 ▶sub read_schema {
Subroutine read_schema: called CC=6 SCAR=17.9
Pod documented
180 95 my ( $self, @names ) = @_;
181 95 my $conf_schema = $self->_config_schema;
182 95 my $read_schema;
183 95 ▶ if ( @names ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@names) { } 1 94 95
184 1 $read_schema = { map { $_ => $self->backend->read_schema( $_ ) } @names };
185 }
186 else {
187 94 $read_schema = $self->backend->read_schema( @names );
188 94 my %all_schemas = map { $_ => 1 } keys %$conf_schema, keys %$read_schema;
189 94 @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 95 @names = sort { !!$conf_schema->{$a}{'x-view'} <=> !!$conf_schema->{$b}{'x-view'} } @names;
195  
196 95 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 646 ▶ my $full_schema = _merge_schema( $conf_schema->{ $name } // {}, $read_schema->{ $name } // {} );
Condition75%3/4
$conf_schema->{$name} // {}
l!l
6460
$read_schema->{$name} // {}
l!l
501145
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 646 ▶ if ( $full_schema->{'x-ignore'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($full_schema->{"x-ignore"}) 96 550 646
202 # Remember we're ignoring this schema
203 96 $conf_schema->{ $name }{ 'x-ignore' } = 1;
204 96 next;
205 }
206 550 $self->schema( $name, $full_schema );
207 }
208 95 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 2200 ▶sub schema {
Subroutine schema: called CC=6 SCAR=18.2
Pod documented
221 2200 my ( $self, $name, $data ) = @_;
222 2200 ▶ if ( !$data ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data) 1625 575 2200
223 1625 ▶ if ( my $schema = $self->_schema->{ $name } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $schema = $self->_schema->{$name}) 1600 25 1625
224 1600 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 575 ▶ if ( !blessed $data || !$data->isa( 'Yancy::Model::Schema' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($data) and $data->isa("Yancy::Model::Schema")) 575 0 575
Condition33%1/3
builtin::blessed($data) and $data->isa("Yancy::Model::Schema")
!ll&&!rl&&r
57500
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 575 my $class = $self->find_class( Schema => $name );
234 575 $data = $class->new( model => $self, name => $name, json_schema => $data );
235 }
236 573 $self->_schema->{$name} = $data;
237 573 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 52 ▶sub json_schema {
Subroutine json_schema: called CC=2 SCAR=7.0
Pod documented
247 52 my ( $self, @names ) = @_;
248 52 ▶ if ( !@names ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@names) 52 0 52
249 52 @names = $self->schema_names;
250 }
251 return {
252 52 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 147 ▶sub schema_names {
Subroutine schema_names: called CC=1 SCAR=0.0
Pod documented
263 147 my ( $self ) = @_;
264 147 my $conf = $self->_config_schema;
265 147 my %all_schemas = map { $_ => 1 } grep !$conf->{$_}{'x-ignore'}, keys %$conf, keys %{ $self->_schema };
266 147 return keys %all_schemas;
267 }
268  
269 # _merge_schema( $keep, $merge );
270 #
271 # Merge the two schemas, returning the result.
272 646 ▶sub _merge_schema {
Subroutine _merge_schema: called CC=10 SCAR=23.0
273 646 my ( $left, $right ) = @_;
274 # ; use Data::Dumper;
275 # ; say "Split schema " . Dumper( $left ) . Dumper( $right );
276 646 my $keep = dclone $left;
277 646 ▶ if ( my $right_props = $right->{properties} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $right_props = $right->{"properties"}) 501 145 646
278 501 ▶ my $keep_props = $keep->{properties} ||= {};
Condition100%2/2
$keep->{"properties"} ||= {}
l!l
43071
MC/DC100%1/1
$keep->{"properties"} ||= {}
$keep->{"properties"}
Truth table100%2/2
A
A= $keep->{"properties"}
Aresult
11
00
279 501 for my $p ( keys %{ $right_props } ) {
280 2878 ▶ my $keep_prop = $keep_props->{ $p } ||= {};
Condition100%2/2
$keep_props->{$p} ||= {}
l!l
2484394
MC/DC100%1/1
$keep_props->{$p} ||= {}
$keep_props->{$p}
Truth table100%2/2
A
A= $keep_props->{$p}
Aresult
11
00
281 2878 my $right_prop = $right_props->{ $p };
282 2878 for my $k ( keys %$right_prop ) {
283 7238 ▶ $keep_prop->{ $k } ||= $right_prop->{ $k };
Condition100%3/3
$keep_prop->{$k} ||= $right_prop->{$k}
l!l&&r!l&&!r
5895964379
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 646 for my $k ( keys %$right ) {
288 2136 ▶ next if $k eq 'properties';
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "properties" 501 1635 2136
289 1635 ▶ $keep->{ $k } ||= $right->{ $k };
Condition66%2/3
$keep->{$k} ||= $right->{$k}
l!l&&r!l&&!r
14242110
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 646 return $keep;
294 }
295  
296 22 1;
297  
298 __END__
« Mojolicious/Plugin/Yancy.pm ↑ Summary Yancy/Plugin/Auth/Basic.pm »