| 1 | package Yancy::Model::Schema; | ||||||||||||
| 2 | 2 | our $VERSION = '1.078'; | |||||||||||
| 3 | # ABSTRACT: Interface to a single schema | ||||||||||||
| 4 | |||||||||||||
| 5 | #pod =head1 SYNOPSIS | ||||||||||||
| 6 | #pod | ||||||||||||
| 7 | #pod my $schema = $app->model->schema( 'foo' ); | ||||||||||||
| 8 | #pod | ||||||||||||
| 9 | #pod my $id = $schema->create( $data ); | ||||||||||||
| 10 | #pod my $item = $schema->get( $id ); | ||||||||||||
| 11 | #pod my $count = $schema->delete( $id ); | ||||||||||||
| 12 | #pod my $count = $schema->delete( $where ); | ||||||||||||
| 13 | #pod my $count = $schema->set( $id, $data ); | ||||||||||||
| 14 | #pod my $count = $schema->set( $where, $data ); | ||||||||||||
| 15 | #pod | ||||||||||||
| 16 | #pod my $res = $schema->list( $where, $opts ); | ||||||||||||
| 17 | #pod for my $item ( @{ $res->{items} } ) { ... } | ||||||||||||
| 18 | #pod | ||||||||||||
| 19 | #pod =head1 DESCRIPTION | ||||||||||||
| 20 | #pod | ||||||||||||
| 21 | #pod B<NOTE>: This module is experimental and its API may change before | ||||||||||||
| 22 | #pod Yancy v2! | ||||||||||||
| 23 | #pod | ||||||||||||
| 24 | #pod For information on how to extend this module to add your own schema | ||||||||||||
| 25 | #pod and item methods, see L<Yancy::Guides::Model>. | ||||||||||||
| 26 | #pod | ||||||||||||
| 27 | #pod =head1 SEE ALSO | ||||||||||||
| 28 | #pod | ||||||||||||
| 29 | #pod L<Yancy::Guides::Model>, L<Yancy::Model> | ||||||||||||
| 30 | #pod | ||||||||||||
| 31 | #pod =cut | ||||||||||||
| 32 | |||||||||||||
| 33 | 2 | ▶ | use Mojo::Base -base; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 34 | |||||||||||||
| 35 | #pod =attr model | ||||||||||||
| 36 | #pod | ||||||||||||
| 37 | #pod The L<Yancy::Model> object that created this schema object. | ||||||||||||
| 38 | #pod | ||||||||||||
| 39 | #pod =cut | ||||||||||||
| 40 | |||||||||||||
| 41 | 0 | ▶ | has model => sub { die 'model is required' }; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||
| 42 | |||||||||||||
| 43 | #pod =attr name | ||||||||||||
| 44 | #pod | ||||||||||||
| 45 | #pod The name of the schema. | ||||||||||||
| 46 | #pod | ||||||||||||
| 47 | #pod =cut | ||||||||||||
| 48 | |||||||||||||
| 49 | 0 | ▶ | has name => sub { die 'name is required' }; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||
| 50 | |||||||||||||
| 51 | 20 | ▶ | sub _backend { shift->model->backend }; | ||||||||||
|
Subroutine
_backend: called CC=1 SCAR=0.0
| |||||||||||||
| 52 | has _item_class => sub { | ||||||||||||
| 53 | 1 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 54 | 1 | return $self->model->find_class( Item => $self->name ); | |||||||||||
| 55 | 2 | }; | |||||||||||
| 56 | |||||||||||||
| 57 | #pod =method info | ||||||||||||
| 58 | #pod | ||||||||||||
| 59 | #pod The JSON Schema for this schema. | ||||||||||||
| 60 | #pod | ||||||||||||
| 61 | #pod =cut | ||||||||||||
| 62 | |||||||||||||
| 63 | sub info { | ||||||||||||
| 64 | 2 | ▶ | my ( $self ) = @_; | ||||||||||
|
Subroutine
info: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 65 | 2 | return $self->_backend->schema->{ $self->name }; | |||||||||||
| 66 | } | ||||||||||||
| 67 | |||||||||||||
| 68 | #pod =method id_field | ||||||||||||
| 69 | #pod | ||||||||||||
| 70 | #pod The ID field for this schema. Either a single string, or an arrayref of | ||||||||||||
| 71 | #pod strings (for composite keys). | ||||||||||||
| 72 | #pod | ||||||||||||
| 73 | #pod =cut | ||||||||||||
| 74 | |||||||||||||
| 75 | sub id_field { | ||||||||||||
| 76 | 2 | ▶ | my ( $self ) = @_; | ||||||||||
|
Subroutine
id_field: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||
| 77 | 2 | ▶ | return $self->info->{'x-id-field'} // 'id'; | ||||||||||
|
Condition50%1/2
$self->info->{"x-id-field"} // "id"
MC/DC0%0/1
$self->info->{"x-id-field"} // "id"
$self->info->{"x-id-field"}
Truth table50%1/2
A
A= $self->info->{"x-id-field"}
| |||||||||||||
| 78 | } | ||||||||||||
| 79 | |||||||||||||
| 80 | #pod =method build_item | ||||||||||||
| 81 | #pod | ||||||||||||
| 82 | #pod Turn a hashref of row data into a L<Yancy::Model::Item> object using | ||||||||||||
| 83 | #pod L<Yancy::Model/find_class> to find the correct class. | ||||||||||||
| 84 | #pod | ||||||||||||
| 85 | #pod =cut | ||||||||||||
| 86 | |||||||||||||
| 87 | sub build_item { | ||||||||||||
| 88 | 11 | ▶ | my ( $self, $data ) = @_; | ||||||||||
|
Subroutine
build_item: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 89 | 11 | return $self->_item_class->new( { data => $data, schema => $self } ); | |||||||||||
| 90 | } | ||||||||||||
| 91 | |||||||||||||
| 92 | #pod =method get | ||||||||||||
| 93 | #pod | ||||||||||||
| 94 | #pod Get an item by its ID. Returns a L<Yancy::Model::Item> object. | ||||||||||||
| 95 | #pod | ||||||||||||
| 96 | #pod =cut | ||||||||||||
| 97 | |||||||||||||
| 98 | sub get { | ||||||||||||
| 99 | 8 | ▶ | my ( $self, $id, %opt ) = @_; | ||||||||||
|
Subroutine
get: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 100 | 8 | ▶ | return $self->build_item( $self->_backend->get( $self->name, $id, %opt ) // return undef ); | ||||||||||
|
Condition100%2/2
$self->_backend->get($self->name, $id, %opt) // (return undef)
MC/DC100%1/1
$self->_backend->get($self->name, $id, %opt) // (return undef)
$self->_backend->get($self->name, $id, %opt)
Truth table100%2/2
A
A= $self->_backend->get($self->name, $id, %opt)
| |||||||||||||
| 101 | } | ||||||||||||
| 102 | |||||||||||||
| 103 | #pod =method list | ||||||||||||
| 104 | #pod | ||||||||||||
| 105 | #pod List items. Returns a hash reference with C<items> and C<total> keys. The C<items> is | ||||||||||||
| 106 | #pod an array ref of L<Yancy::Model::Item> objects. C<total> is the total number of items | ||||||||||||
| 107 | #pod that would be returned without any C<offset> or C<limit> options. | ||||||||||||
| 108 | #pod | ||||||||||||
| 109 | #pod =cut | ||||||||||||
| 110 | |||||||||||||
| 111 | sub list { | ||||||||||||
| 112 | 2 | ▶ | my ( $self, $where, $opt ) = @_; | ||||||||||
|
Subroutine
list: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 113 | 2 | my $res = $self->_backend->list( $self->name, $where, $opt ); | |||||||||||
| 114 | 2 | return { items => [ map { $self->build_item( $_ ) } @{ $res->{items} } ], total => $res->{total} }; | |||||||||||
| 115 | } | ||||||||||||
| 116 | |||||||||||||
| 117 | #pod =method create | ||||||||||||
| 118 | #pod | ||||||||||||
| 119 | #pod Create a new item. Returns the ID of the created item. | ||||||||||||
| 120 | #pod | ||||||||||||
| 121 | #pod =cut | ||||||||||||
| 122 | |||||||||||||
| 123 | sub create { | ||||||||||||
| 124 | 5 | ▶ | my ( $self, $data ) = @_; | ||||||||||
|
Subroutine
create: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 125 | 5 | return $self->_backend->create( $self->name, $data ); | |||||||||||
| 126 | } | ||||||||||||
| 127 | |||||||||||||
| 128 | #pod =method set | ||||||||||||
| 129 | #pod | ||||||||||||
| 130 | #pod Set the given fields in an item. See also L<Yancy::Model::Item/set>. | ||||||||||||
| 131 | #pod | ||||||||||||
| 132 | #pod =cut | ||||||||||||
| 133 | |||||||||||||
| 134 | sub set { | ||||||||||||
| 135 | 1 | ▶ | my ( $self, $id, $data ) = @_; | ||||||||||
|
Subroutine
set: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 136 | # XXX: Use get() to get the item instance first? Then they could | ||||||||||||
| 137 | # override set() to do things... | ||||||||||||
| 138 | 1 | return $self->_backend->set( $self->name, $id, $data ); | |||||||||||
| 139 | } | ||||||||||||
| 140 | |||||||||||||
| 141 | #pod =method delete | ||||||||||||
| 142 | #pod | ||||||||||||
| 143 | #pod Delete an item. See also L<Yancy::Model::Item/delete>. | ||||||||||||
| 144 | #pod | ||||||||||||
| 145 | #pod =cut | ||||||||||||
| 146 | |||||||||||||
| 147 | sub delete { | ||||||||||||
| 148 | 2 | ▶ | my ( $self, $id ) = @_; | ||||||||||
|
Subroutine
delete: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 149 | # XXX: Use get() to get the item instance first? Then they could | ||||||||||||
| 150 | # override delete() to do things... | ||||||||||||
| 151 | 2 | return $self->_backend->delete( $self->name, $id ); | |||||||||||
| 152 | } | ||||||||||||
| 153 | |||||||||||||
| 154 | 2 | 1; | |||||||||||
| 155 | |||||||||||||
| 156 | __END__ | ||||||||||||