| 1 | package Yancy::Model::Item; | ||||||||||
| 2 | 2 | our $VERSION = '1.078'; | |||||||||
| 3 | # ABSTRACT: Interface to a single item | ||||||||||
| 4 | |||||||||||
| 5 | #pod =head1 SYNOPSIS | ||||||||||
| 6 | #pod | ||||||||||
| 7 | #pod my $schema = $model->schema( 'foo' ); | ||||||||||
| 8 | #pod my $item = $schema->get( $id ); | ||||||||||
| 9 | #pod | ||||||||||
| 10 | #pod $item->set( $data ); | ||||||||||
| 11 | #pod $item->delete; | ||||||||||
| 12 | #pod | ||||||||||
| 13 | #pod =head1 DESCRIPTION | ||||||||||
| 14 | #pod | ||||||||||
| 15 | #pod B<NOTE>: This module is experimental and it's API may change before | ||||||||||
| 16 | #pod Yancy v2! | ||||||||||
| 17 | #pod | ||||||||||
| 18 | #pod For information on how to extend this module to add your own schema | ||||||||||
| 19 | #pod and item methods, see L<Yancy::Guides::Model>. | ||||||||||
| 20 | #pod | ||||||||||
| 21 | #pod =head1 SEE ALSO | ||||||||||
| 22 | #pod | ||||||||||
| 23 | #pod L<Yancy::Guides::Model> | ||||||||||
| 24 | #pod | ||||||||||
| 25 | #pod =cut | ||||||||||
| 26 | |||||||||||
| 27 | 2 | ▶ | use Mojo::Base -base; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | use overload | ||||||||||
| 29 | 2 | '%{}' => \&_hashref, | |||||||||
| 30 | 2 | ▶ | fallback => 1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 31 | |||||||||||
| 32 | sub _hashref { | ||||||||||
| 33 | 24 | ▶ | my ( $self ) = @_; | ||||||||
|
Subroutine
_hashref: called CC=2 SCAR=6.9
| |||||||||||
| 34 | # If we're not being called by ourselves or one of our superclasses, | ||||||||||
| 35 | # we want to pretend we're a hashref of plain data. | ||||||||||
| 36 | 24 | ▶ | if ( !$self->isa( scalar caller ) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | 1 | return $self->{data}; | |||||||||
| 38 | } | ||||||||||
| 39 | 23 | return $self; | |||||||||
| 40 | } | ||||||||||
| 41 | |||||||||||
| 42 | #pod =attr schema | ||||||||||
| 43 | #pod | ||||||||||
| 44 | #pod The L<Yancy::Model::Schema> object containing this item. | ||||||||||
| 45 | #pod | ||||||||||
| 46 | #pod =cut | ||||||||||
| 47 | |||||||||||
| 48 | 0 | ▶ | has schema => sub { die 'schema is required' }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||
| 49 | |||||||||||
| 50 | #pod =attr data | ||||||||||
| 51 | #pod | ||||||||||
| 52 | #pod The row data for this item. | ||||||||||
| 53 | #pod | ||||||||||
| 54 | #pod =cut | ||||||||||
| 55 | |||||||||||
| 56 | 0 | ▶ | has data => sub { die 'data is required' }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||
| 57 | |||||||||||
| 58 | #pod =method model | ||||||||||
| 59 | #pod | ||||||||||
| 60 | #pod The L<Yancy::Model> object that contains this item. | ||||||||||
| 61 | #pod | ||||||||||
| 62 | #pod =cut | ||||||||||
| 63 | |||||||||||
| 64 | 0 | ▶ | sub model { shift->schema->model } | ||||||||
|
Subroutine
model: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 65 | |||||||||||
| 66 | #pod =method id | ||||||||||
| 67 | #pod | ||||||||||
| 68 | #pod The ID field for the item. Returns a string or a hash-reference (for composite keys). | ||||||||||
| 69 | #pod | ||||||||||
| 70 | #pod =cut | ||||||||||
| 71 | |||||||||||
| 72 | sub id { | ||||||||||
| 73 | 2 | ▶ | my ( $self ) = @_; | ||||||||
|
Subroutine
id: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||
| 74 | 2 | my $id_field = $self->schema->id_field; | |||||||||
| 75 | 2 | my $data = $self->data; | |||||||||
| 76 | 2 | ▶ | if ( ref $id_field eq 'ARRAY' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | 0 | return { map { $_ => $data->{$_} } @$id_field }; | |||||||||
| 78 | } | ||||||||||
| 79 | 2 | return $data->{ $id_field }; | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | #pod =method set | ||||||||||
| 83 | #pod | ||||||||||
| 84 | #pod Set data in this item. Returns true if successful. | ||||||||||
| 85 | #pod | ||||||||||
| 86 | #pod =cut | ||||||||||
| 87 | |||||||||||
| 88 | sub set { | ||||||||||
| 89 | 1 | ▶ | my ( $self, $data ) = @_; | ||||||||
|
Subroutine
set: called CC=3 SCAR=11.7
Pod
documented
| |||||||||||
| 90 | 1 | ▶ | if ( $self->schema->set( $self->id, $data ) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 91 | 1 | $self->data->{ $_ } = $data->{ $_ } for keys %$data; | |||||||||
| 92 | 1 | return 1; | |||||||||
| 93 | } | ||||||||||
| 94 | 0 | return 0; | |||||||||
| 95 | } | ||||||||||
| 96 | |||||||||||
| 97 | #pod =method delete | ||||||||||
| 98 | #pod | ||||||||||
| 99 | #pod Delete this item from the database. Returns true if successful. | ||||||||||
| 100 | #pod | ||||||||||
| 101 | #pod =cut | ||||||||||
| 102 | |||||||||||
| 103 | sub delete { | ||||||||||
| 104 | 1 | ▶ | my ( $self ) = @_; | ||||||||
|
Subroutine
delete: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 105 | 1 | return $self->schema->delete( $self->id ); | |||||||||
| 106 | } | ||||||||||
| 107 | |||||||||||
| 108 | 2 | 1; | |||||||||
| 109 | |||||||||||
| 110 | __END__ | ||||||||||