source: bookmarks/trunk/lib/Bookmark.pm @ 128

Last change on this file since 128 was 91, checked in by peter, 9 years ago

change TO_JSON() method name to to_hashref() to emphasize its multiple uses

File size: 1.3 KB
Line 
1package Bookmark;
2
3use Moose;
4use HTTP::Date qw{time2isoz};
5
6has id    => ( is => 'ro' );
7has uri   => ( is => 'rw' );
8has title => ( is => 'rw' );
9has ctime => ( is => 'ro' );
10has mtime => (
11    is => 'ro',
12    # mtime defaults to ctime
13    default => sub { $_[0]->ctime },
14    lazy => 1,
15);
16has tags  => ( is => 'rw' );
17has bookmark_uri => ( is => 'rw' );
18has exists => ( is => 'ro' );
19has collection => ( is => 'ro' );
20
21sub created { scalar localtime $_[0]->ctime }
22sub updated { scalar localtime $_[0]->mtime }
23sub created_iso { (my $iso = time2isoz($_[0]->ctime)) =~ s/\D//g; return $iso; }
24sub updated_iso { (my $iso = time2isoz($_[0]->ctime)) =~ s/\D//g; return $iso; }
25
26sub BUILD {
27    my $self = shift;
28    my $args = shift;
29    if ($args->{base_uri}) {
30        $self->bookmark_uri(URI->new_abs($self->id, $args->{base_uri}));
31    }
32}
33
34sub to_hashref {
35    my $self = shift;
36    return {
37        id    => $self->id,
38        uri   => $self->uri,
39        title => $self->title,
40        ctime => $self->ctime,
41        mtime => $self->mtime,
42        tags  => $self->tags,
43        ($self->bookmark_uri ? (bookmark_uri => $self->bookmark_uri->canonical->as_string) : ()),
44    };
45}
46
47sub update {
48    my $self = shift;
49    die "Not part of a collection; nowhere to write this bookmark" unless $self->collection;
50    return $self->collection->update($self);
51}
52
53# module return
541;
Note: See TracBrowser for help on using the repository browser.