line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Yancy::Model::Item; |
2
|
|
|
|
|
|
|
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: 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
|
|
8104
|
use Mojo::Base -base; |
|
11
|
|
|
|
|
29
|
|
|
11
|
|
|
|
|
119
|
|
28
|
|
|
|
|
|
|
use overload |
29
|
11
|
|
|
|
|
130
|
'%{}' => \&_hashref, |
30
|
11
|
|
|
11
|
|
2680
|
fallback => 1; |
|
11
|
|
|
|
|
29
|
|
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub _hashref { |
33
|
4478
|
|
|
4478
|
|
54339
|
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
|
100
|
|
|
|
12211
|
if ( !$self->isa( scalar caller ) ) { |
37
|
2225
|
|
|
|
|
3534
|
return $self->{data}; |
38
|
|
|
|
|
|
|
} |
39
|
2253
|
|
|
|
|
8691
|
return $self; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub TO_JSON { |
43
|
6
|
|
|
6
|
0
|
943
|
my ( $self ) = @_; |
44
|
6
|
|
|
|
|
15
|
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
|
4
|
my ( $self ) = @_; |
79
|
2
|
|
|
|
|
6
|
my $id_field = $self->schema->id_field; |
80
|
2
|
|
|
|
|
35
|
my $data = $self->data; |
81
|
2
|
50
|
|
|
|
6
|
if ( ref $id_field eq 'ARRAY' ) { |
82
|
0
|
|
|
|
|
0
|
return { map { $_ => $data->{$_} } @$id_field }; |
|
0
|
|
|
|
|
0
|
|
83
|
|
|
|
|
|
|
} |
84
|
2
|
|
|
|
|
8
|
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
|
24
|
my ( $self, $data ) = @_; |
95
|
1
|
50
|
|
|
|
3
|
if ( $self->schema->set( $self->id, $data ) ) { |
96
|
1
|
|
|
|
|
6
|
$self->data->{ $_ } = $data->{ $_ } for keys %$data; |
97
|
1
|
|
|
|
|
4
|
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
|
20
|
my ( $self ) = @_; |
110
|
1
|
|
|
|
|
3
|
return $self->schema->delete( $self->id ); |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
1; |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
__END__ |