line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Yancy::Model::Item; |
2
|
|
|
|
|
|
|
our $VERSION = '1.087'; |
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: 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. |
20
|
|
|
|
|
|
|
#pod |
21
|
|
|
|
|
|
|
#pod =head1 SEE ALSO |
22
|
|
|
|
|
|
|
#pod |
23
|
|
|
|
|
|
|
#pod L |
24
|
|
|
|
|
|
|
#pod |
25
|
|
|
|
|
|
|
#pod =cut |
26
|
|
|
|
|
|
|
|
27
|
11
|
|
|
11
|
|
7819
|
use Mojo::Base -base; |
|
11
|
|
|
|
|
30
|
|
|
11
|
|
|
|
|
87
|
|
28
|
|
|
|
|
|
|
use overload |
29
|
11
|
|
|
|
|
131
|
'%{}' => \&_hashref, |
30
|
11
|
|
|
11
|
|
2480
|
fallback => 1; |
|
11
|
|
|
|
|
31
|
|
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub _hashref { |
33
|
4122
|
|
|
4122
|
|
62269
|
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
|
4122
|
100
|
|
|
|
11232
|
if ( !$self->isa( scalar caller ) ) { |
37
|
2047
|
|
|
|
|
3309
|
return $self->{data}; |
38
|
|
|
|
|
|
|
} |
39
|
2075
|
|
|
|
|
7966
|
return $self; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub TO_JSON { |
43
|
6
|
|
|
6
|
0
|
972
|
my ( $self ) = @_; |
44
|
6
|
|
|
|
|
16
|
return $self->{data}; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
#pod =attr schema |
48
|
|
|
|
|
|
|
#pod |
49
|
|
|
|
|
|
|
#pod The L object containing this item. |
50
|
|
|
|
|
|
|
#pod |
51
|
|
|
|
|
|
|
#pod =cut |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
has schema => sub { die 'schema is required' }; |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
#pod =attr data |
56
|
|
|
|
|
|
|
#pod |
57
|
|
|
|
|
|
|
#pod The row data for this item. |
58
|
|
|
|
|
|
|
#pod |
59
|
|
|
|
|
|
|
#pod =cut |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
has data => sub { die 'data is required' }; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
#pod =method model |
64
|
|
|
|
|
|
|
#pod |
65
|
|
|
|
|
|
|
#pod The L object that contains this item. |
66
|
|
|
|
|
|
|
#pod |
67
|
|
|
|
|
|
|
#pod =cut |
68
|
|
|
|
|
|
|
|
69
|
0
|
|
|
0
|
1
|
0
|
sub model { shift->schema->model } |
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
|
|
|
|
|
|
|
sub id { |
78
|
2
|
|
|
2
|
1
|
6
|
my ( $self ) = @_; |
79
|
2
|
|
|
|
|
5
|
my $id_field = $self->schema->id_field; |
80
|
2
|
|
|
|
|
22
|
my $data = $self->data; |
81
|
2
|
50
|
|
|
|
23
|
if ( ref $id_field eq 'ARRAY' ) { |
82
|
0
|
|
|
|
|
0
|
return { map { $_ => $data->{$_} } @$id_field }; |
|
0
|
|
|
|
|
0
|
|
83
|
|
|
|
|
|
|
} |
84
|
2
|
|
|
|
|
12
|
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
|
|
|
|
|
|
|
sub set { |
94
|
1
|
|
|
1
|
1
|
36
|
my ( $self, $data ) = @_; |
95
|
1
|
50
|
|
|
|
6
|
if ( $self->schema->set( $self->id, $data ) ) { |
96
|
1
|
|
|
|
|
6
|
$self->data->{ $_ } = $data->{ $_ } for keys %$data; |
97
|
1
|
|
|
|
|
6
|
return 1; |
98
|
|
|
|
|
|
|
} |
99
|
0
|
|
|
|
|
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
|
|
|
|
|
|
|
sub delete { |
109
|
1
|
|
|
1
|
1
|
32
|
my ( $self ) = @_; |
110
|
1
|
|
|
|
|
5
|
return $self->schema->delete( $self->id ); |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
1; |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
__END__ |