/
Entry.pm
181 lines (142 loc) · 3.37 KB
/
Entry.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package Net::Google::DataAPI::Role::Entry;
use Any::Moose '::Role';
use Carp;
use XML::Atom;
use XML::Atom::Entry;
has container => (
isa => 'Maybe[Net::Google::DataAPI::Role::Entry]',
is => 'ro',
);
has service => (
does => 'Net::Google::DataAPI::Role::Service',
is => 'ro',
required => 1,
lazy_build => 1,
weak_ref => 1,
handles => ['ns'],
);
sub _build_service { shift->container->service };
my %rel2label = (
edit => 'editurl',
self => 'selfurl',
);
for my $label (values %rel2label) {
has $label => (
isa => 'Str',
is => 'ro',
lazy_build => 1,
);
__PACKAGE__->meta->add_method(
"_build_$label" => sub {
my $self = shift;
for my $link ($self->atom->link) {
my $rel = $rel2label{$link->rel} or next;
return $link->href if $rel eq $label;
}
}
);
}
has atom => (
isa => 'XML::Atom::Entry',
is => 'rw',
trigger => sub {
my ($self, $arg) = @_;
my $id = $self->atom->get($self->atom->ns, 'id');
croak "can't set different id!" if $self->id && $self->id ne $id;
$self->from_atom;
},
handles => ['elem', 'author'],
);
has id => (
isa => 'Str',
is => 'ro',
lazy_build => 1,
);
sub _build_id {
my $self = shift;
$self->atom->get($self->atom->ns, 'id') || '';
}
has title => (
isa => 'Str',
is => 'rw',
trigger => sub {$_[0]->update},
lazy_build => 1,
);
sub _build_title {
my $self = shift;
$self->atom ? $self->atom->title : 'untitled';
}
has etag => (
isa => 'Str',
is => 'rw',
lazy_build => 1,
);
sub _build_etag {
my $self = shift;
$self->atom or return '';
$self->elem->getAttributeNS($self->ns('gd')->{uri}, 'etag');
}
sub from_atom {
my ($self) = @_;
for my $attr ($self->meta->get_all_attributes) {
$attr->name eq 'service' and next;
$attr->clear_value($self) if $attr->{lazy};
}
}
sub to_atom {
my ($self) = @_;
my $entry = XML::Atom::Entry->new;
$entry->title($self->title);
return $entry;
}
sub sync {
my ($self) = @_;
my $entry = $self->service->get_entry($self->selfurl);
$self->atom($entry);
}
sub update {
my ($self) = @_;
$self->etag or return;
my $atom = $self->service->put(
{
self => $self,
entry => $self->to_atom,
}
);
$self->container->sync if $self->container;
$self->atom($atom);
}
sub delete {
my $self = shift;
my $res = $self->service->delete({self => $self});
$self->container->sync if $self->container;
return $res->is_success;
}
no Any::Moose '::Role';
1;
__END__
=pod
=head1 NAME
Net::Google::DataAPI::Role::Entry - represents entry of Google Data API
=head1 SYNOPSIS
package MyEntry;
use Any::Moose;
use Net::Google::DataAPI;
with 'Net::Google::DataAPI::Role::Entry';
entry_has some_data => (
is => 'ro',
isa => 'Str',
tagname => 'somedata',
ns => 'gd',
);
1;
=head1 DESCRIPTION
Net::Google::DataAPI::Role::Entry provides base functionalities for the entry of Google Data API.
=head1 AUTHOR
Nobuo Danjou E<lt>nobuo.danjou@gmail.comE<gt>
=head1 SEE ALSO
L<Net::Google::DataAPI>
=head1 LICENSE
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut