|
Last change
on this file since 105 was
91,
checked in by peter, 10 years ago
|
|
change TO_JSON() method name to to_hashref() to emphasize its multiple uses
|
|
File size:
1.3 KB
|
| Line | |
|---|
| 1 | package Bookmark; |
|---|
| 2 | |
|---|
| 3 | use Moose; |
|---|
| 4 | use HTTP::Date qw{time2isoz}; |
|---|
| 5 | |
|---|
| 6 | has id => ( is => 'ro' ); |
|---|
| 7 | has uri => ( is => 'rw' ); |
|---|
| 8 | has title => ( is => 'rw' ); |
|---|
| 9 | has ctime => ( is => 'ro' ); |
|---|
| 10 | has mtime => ( |
|---|
| 11 | is => 'ro', |
|---|
| 12 | # mtime defaults to ctime |
|---|
| 13 | default => sub { $_[0]->ctime }, |
|---|
| 14 | lazy => 1, |
|---|
| 15 | ); |
|---|
| 16 | has tags => ( is => 'rw' ); |
|---|
| 17 | has bookmark_uri => ( is => 'rw' ); |
|---|
| 18 | has exists => ( is => 'ro' ); |
|---|
| 19 | has collection => ( is => 'ro' ); |
|---|
| 20 | |
|---|
| 21 | sub created { scalar localtime $_[0]->ctime } |
|---|
| 22 | sub updated { scalar localtime $_[0]->mtime } |
|---|
| 23 | sub created_iso { (my $iso = time2isoz($_[0]->ctime)) =~ s/\D//g; return $iso; } |
|---|
| 24 | sub updated_iso { (my $iso = time2isoz($_[0]->ctime)) =~ s/\D//g; return $iso; } |
|---|
| 25 | |
|---|
| 26 | sub 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 | |
|---|
| 34 | sub 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 | |
|---|
| 47 | sub 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 |
|---|
| 54 | 1; |
|---|
Note: See
TracBrowser
for help on using the repository browser.