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

Last change on this file since 108 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
RevLine 
[2]1package Bookmark;
2
[25]3use Moose;
[66]4use HTTP::Date qw{time2isoz};
[2]5
[44]6has id    => ( is => 'ro' );
[25]7has uri   => ( is => 'rw' );
8has title => ( is => 'rw' );
[44]9has ctime => ( is => 'ro' );
10has mtime => (
11    is => 'ro',
12    # mtime defaults to ctime
13    default => sub { $_[0]->ctime },
14    lazy => 1,
15);
[25]16has tags  => ( is => 'rw' );
17has bookmark_uri => ( is => 'rw' );
[66]18has exists => ( is => 'ro' );
[75]19has collection => ( is => 'ro' );
[2]20
[66]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
[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
[91]34sub to_hashref {
[25]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
[75]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
[2]53# module return
541;
Note: See TracBrowser for help on using the repository browser.