| 1 | package Yancy::Model; | ||||||||||||||||||||||||||||
| 2 | 2 | our $VERSION = '1.078'; | |||||||||||||||||||||||||||
| 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 | 2 | ▶ | use Mojo::Base -base; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | 2 | ▶ | use Scalar::Util qw( blessed ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 47 | 2 | ▶ | use Mojo::Util qw( camelize ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 48 | 2 | ▶ | use Mojo::Loader qw( load_class ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | #pod =attr backend | ||||||||||||||||||||||||||||
| 51 | #pod | ||||||||||||||||||||||||||||
| 52 | #pod A L<Yancy::Backend> object. | ||||||||||||||||||||||||||||
| 53 | #pod | ||||||||||||||||||||||||||||
| 54 | #pod =cut | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | has backend => sub { die "backend is required" }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | #pod =attr namespaces | ||||||||||||||||||||||||||||
| 59 | #pod | ||||||||||||||||||||||||||||
| 60 | #pod An array of namespaces to find Schema and Item classes. Defaults to C<[ 'Yancy::Model' ]>. | ||||||||||||||||||||||||||||
| 61 | #pod | ||||||||||||||||||||||||||||
| 62 | #pod =cut | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 1 | ▶ | has namespaces => sub { [qw( Yancy::Model )] }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 1 | ▶ | has _schema => sub { {} }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | #pod =method find_class | ||||||||||||||||||||||||||||
| 69 | #pod | ||||||||||||||||||||||||||||
| 70 | #pod Find a class of the given type for an object of the given name. The name is run | ||||||||||||||||||||||||||||
| 71 | #pod through L<Mojo::Util/camelize> before lookups. | ||||||||||||||||||||||||||||
| 72 | #pod | ||||||||||||||||||||||||||||
| 73 | #pod unshift @{ $model->namespaces }, 'MyApp'; | ||||||||||||||||||||||||||||
| 74 | #pod # MyApp::Schema::User | ||||||||||||||||||||||||||||
| 75 | #pod $class = $model->find_class( Schema => 'user' ); | ||||||||||||||||||||||||||||
| 76 | #pod # MyApp::Item::UserProfile | ||||||||||||||||||||||||||||
| 77 | #pod $class = $model->find_class( Item => 'user_profile' ); | ||||||||||||||||||||||||||||
| 78 | #pod | ||||||||||||||||||||||||||||
| 79 | #pod If a specific class cannot be found, a generic class for the type is found instead. | ||||||||||||||||||||||||||||
| 80 | #pod | ||||||||||||||||||||||||||||
| 81 | #pod # MyApp::Schema | ||||||||||||||||||||||||||||
| 82 | #pod $class = $model->find_class( Schema => 'not_found' ); | ||||||||||||||||||||||||||||
| 83 | #pod # MyApp::Item | ||||||||||||||||||||||||||||
| 84 | #pod $class = $model->find_class( Item => 'not_found' ); | ||||||||||||||||||||||||||||
| 85 | #pod | ||||||||||||||||||||||||||||
| 86 | #pod =cut | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | sub find_class { | ||||||||||||||||||||||||||||
| 89 | 5 | ▶ | my ( $self, $type, $name ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
find_class: called CC=7 SCAR=23.7
Pod
documented
| |||||||||||||||||||||||||||||
| 90 | # First, a specific class for this named type. | ||||||||||||||||||||||||||||
| 91 | 5 | for my $namespace ( @{ $self->namespaces } ) { | |||||||||||||||||||||||||||
| 92 | 5 | my $class = "${namespace}::${type}::" . camelize( $name ); | |||||||||||||||||||||||||||
| 93 | 5 | ▶ | if ( my $e = load_class $class ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 5 | ▶ | if ( ref $e ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 0 | die "Could not load $class: $e"; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | # Not found, try the next one | ||||||||||||||||||||||||||||
| 98 | 5 | next; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | # No error, so this is the class we want | ||||||||||||||||||||||||||||
| 101 | 0 | return $class; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | # Finally, try to find a generic type class | ||||||||||||||||||||||||||||
| 105 | 5 | for my $namespace ( @{ $self->namespaces } ) { | |||||||||||||||||||||||||||
| 106 | 5 | my $class = "${namespace}::${type}"; | |||||||||||||||||||||||||||
| 107 | 5 | ▶ | if ( my $e = load_class $class ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | if ( ref $e ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 0 | die "Could not load $class: $e"; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | # Not found, try the next one | ||||||||||||||||||||||||||||
| 112 | 0 | next; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | # No error, so this is the class we want | ||||||||||||||||||||||||||||
| 115 | 5 | return $class; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | die "Could not find class for type $type or name $name"; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | #pod =method read_schema | ||||||||||||||||||||||||||||
| 122 | #pod | ||||||||||||||||||||||||||||
| 123 | #pod Read the schema from the L</backend> and prepare schema objects using L</find_class> | ||||||||||||||||||||||||||||
| 124 | #pod to find the correct classes. | ||||||||||||||||||||||||||||
| 125 | #pod | ||||||||||||||||||||||||||||
| 126 | #pod =cut | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | sub read_schema { | ||||||||||||||||||||||||||||
| 129 | 1 | ▶ | my ( $self ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
read_schema: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 130 | 1 | my $schema = $self->backend->read_schema; | |||||||||||||||||||||||||||
| 131 | 1 | for my $name ( keys %$schema ) { | |||||||||||||||||||||||||||
| 132 | 4 | $self->schema( $name, $schema->{ $name } ); | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | 1 | return $self; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | #pod =method schema | ||||||||||||||||||||||||||||
| 138 | #pod | ||||||||||||||||||||||||||||
| 139 | #pod Get or set a schema object. | ||||||||||||||||||||||||||||
| 140 | #pod | ||||||||||||||||||||||||||||
| 141 | #pod $model = $model->schema( user => MyApp::Model::User->new ); | ||||||||||||||||||||||||||||
| 142 | #pod $schema = $model->schema( 'user' ); | ||||||||||||||||||||||||||||
| 143 | #pod | ||||||||||||||||||||||||||||
| 144 | #pod =cut | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | # XXX: Preload namespaces' Schema:: and Item:: classes? | ||||||||||||||||||||||||||||
| 147 | sub schema { | ||||||||||||||||||||||||||||
| 148 | 20 | ▶ | my ( $self, $name, $data ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
schema: called CC=5 SCAR=17.4
Pod
documented
| |||||||||||||||||||||||||||||
| 149 | 20 | ▶ | if ( !$data ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | 16 | ▶ | if ( my $schema = $self->_schema->{ $name } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 16 | return $schema; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | # Create a default schema | ||||||||||||||||||||||||||||
| 154 | 0 | $self->schema( $name, {} ); | |||||||||||||||||||||||||||
| 155 | 0 | return $self->_schema->{$name}; | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | 4 | ▶ | if ( !blessed $data || !$data->isa( 'Yancy::Model::Schema' ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($data) and $data->isa("Yancy::Model::Schema")
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")
| |||||||||||||||||||||||||||||
| 158 | 4 | my $class = $self->find_class( Schema => $name ); | |||||||||||||||||||||||||||
| 159 | 4 | $data = $class->new( model => $self, name => $name, schema => $data ); | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | 4 | $self->_schema->{$name} = $data; | |||||||||||||||||||||||||||
| 162 | 4 | return $self; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 2 | 1; | |||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | __END__ | ||||||||||||||||||||||||||||