Yancy/Model/Item.pm

filter: Statementstmt 81.8% 27 / 33 Branchbran 66.6% 4 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 70.0% 7 / 10 Podpod 80.0% 4 / 5 totaltotal 77.7% 42 / 54 CC 5 SCAR 6.3
CC
5
Cov
79%
set
3.2
id
2.1
_hashref
2.0
CRAP 1.9
« Yancy/Plugin/Form.pm ↑ Summary Yancy/Backend/Mysql.pm »
1 package Yancy::Model::Item;
2 12 our $VERSION = '1.088';
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 12 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 use overload
29 12 ▶ '%{}' => \&_hashref,
Subroutine BEGIN: called CC=1 SCAR=0.0
30 12 fallback => 1;
31  
32 4478 ▶sub _hashref {
Subroutine _hashref: called CC=2 SCAR=6.9
33 4478 my ( $self ) = @_;
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 4478 ▶ if ( !$self->isa( scalar caller ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->isa(scalar caller)) 2225 2253 4478
37 2225 return $self->{data};
38 }
39 2253 return $self;
40 }
41  
42 6 ▶sub TO_JSON {
Subroutine TO_JSON: called CC=1 SCAR=0.0
Pod undocumented
43 6 my ( $self ) = @_;
44 6 return $self->{data};
45 }
46  
47 #pod =attr schema
48 #pod
49 #pod The L<Yancy::Model::Schema> object containing this item.
50 #pod
51 #pod =cut
52  
53 0 ▶has schema => sub { die 'schema is required' };
Subroutine __ANON__: not called CC=1 SCAR=1.2
54  
55 #pod =attr data
56 #pod
57 #pod The row data for this item.
58 #pod
59 #pod =cut
60  
61 0 ▶has data => sub { die 'data is required' };
Subroutine __ANON__: not called CC=1 SCAR=1.2
62  
63 #pod =method model
64 #pod
65 #pod The L<Yancy::Model> object that contains this item.
66 #pod
67 #pod =cut
68  
69 0 ▶sub model { shift->schema->model }
Subroutine model: not called CC=1 SCAR=6.9
Pod documented
70  
71 #pod =method id
72 #pod
73 #pod The ID field for the item. Returns a string or a hash-reference (for composite keys).
74 #pod
75 #pod =cut
76  
77 2 ▶sub id {
Subroutine id: called CC=2 SCAR=7.6
Pod documented
78 2 my ( $self ) = @_;
79 2 my $id_field = $self->schema->id_field;
80 2 my $data = $self->data;
81 2 ▶ if ( ref $id_field eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $id_field eq "ARRAY") 0 2 2
82 0 return { map { $_ => $data->{$_} } @$id_field };
83 }
84 2 return $data->{ $id_field };
85 }
86  
87 #pod =method set
88 #pod
89 #pod Set data in this item. Returns true if successful.
90 #pod
91 #pod =cut
92  
93 1 ▶sub set {
Subroutine set: called CC=3 SCAR=11.7
Pod documented
94 1 my ( $self, $data ) = @_;
95 1 ▶ if ( $self->schema->set( $self->id, $data ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->schema->set($self->id, $data)) 1 0 1
96 1 $self->data->{ $_ } = $data->{ $_ } for keys %$data;
97 1 return 1;
98 }
99 0 return 0;
100 }
101  
102 #pod =method delete
103 #pod
104 #pod Delete this item from the database. Returns true if successful.
105 #pod
106 #pod =cut
107  
108 1 ▶sub delete {
Subroutine delete: called CC=1 SCAR=0.0
Pod documented
109 1 my ( $self ) = @_;
110 1 return $self->schema->delete( $self->id );
111 }
112  
113 12 1;
114  
115 __END__
« Yancy/Plugin/Form.pm ↑ Summary Yancy/Backend/Mysql.pm »