Last change
on this file since 103 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
|
Rev | Line | |
---|
[2] | 1 | package Bookmark; |
---|
| 2 | |
---|
[25] | 3 | use Moose; |
---|
[66] | 4 | use HTTP::Date qw{time2isoz}; |
---|
[2] | 5 | |
---|
[44] | 6 | has id => ( is => 'ro' ); |
---|
[25] | 7 | has uri => ( is => 'rw' ); |
---|
| 8 | has title => ( is => 'rw' ); |
---|
[44] | 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 | ); |
---|
[25] | 16 | has tags => ( is => 'rw' ); |
---|
| 17 | has bookmark_uri => ( is => 'rw' ); |
---|
[66] | 18 | has exists => ( is => 'ro' ); |
---|
[75] | 19 | has collection => ( is => 'ro' ); |
---|
[2] | 20 | |
---|
[66] | 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 | |
---|
[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 | |
---|
[91] | 34 | sub 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] | 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 | |
---|
[2] | 53 | # module return |
---|
| 54 | 1; |
---|
Note: See
TracBrowser
for help on using the repository browser.