Changeset 25 in bookmarks


Ignore:
Timestamp:
05/23/13 00:37:57 (11 years ago)
Author:
peter
Message:
  • Bookmark uses Moose
    • the Bookmarks base_uri should be passed to the constructor to set the bookmark_uri attribute of the Bookmark
    • added a TO_JSON method to serialize the URIs to strings for JSON
  • use JSON::convert_blessed to trigger calling Bookmark::TO_JSON
  • added a private _sth_tags_from_uri attribute to Bookmarks
  • Bookmarks::get_tags can be called with a uri parameter to fetch a list of tags for that resource
  • Bookmarks::get_bookmark() returns a Bookmark object
  • Bookmarks::get_resources() returns an array of Bookmark objects
Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Bookmark.pm

    r2 r25  
    11package Bookmark; 
    22 
    3 use Class::Accessor qw{antlers}; 
     3use Moose; 
    44 
    5 has id    => ( is => 'ro' ); 
    6 has uri   => ( is => 'ro' ); 
    7 has ctime => ( is => 'ro' ); 
    8 has mtime => ( is => 'ro' ); 
     5has id    => ( is => 'rw' ); 
     6has uri   => ( is => 'rw' ); 
     7has title => ( is => 'rw' ); 
     8has ctime => ( is => 'rw' ); 
     9has mtime => ( is => 'rw' ); 
     10has tags  => ( is => 'rw' ); 
     11has bookmark_uri => ( is => 'rw' ); 
     12 
     13sub BUILD { 
     14    my $self = shift; 
     15    my $args = shift; 
     16    if ($args->{base_uri}) { 
     17        $self->bookmark_uri(URI->new_abs($self->id, $args->{base_uri})); 
     18    } 
     19} 
     20 
     21sub TO_JSON { 
     22    my $self = shift; 
     23    return { 
     24        id    => $self->id, 
     25        uri   => $self->uri, 
     26        title => $self->title, 
     27        ctime => $self->ctime, 
     28        mtime => $self->mtime, 
     29        tags  => $self->tags, 
     30        ($self->bookmark_uri ? (bookmark_uri => $self->bookmark_uri->canonical->as_string) : ()), 
     31    }; 
     32} 
    933 
    1034# module return 
  • trunk/BookmarkApp.pm

    r24 r25  
    9090        ); 
    9191        return decode_utf8( 
    92             encode_json({ 
     92            JSON->new->utf8->convert_blessed->encode({ 
    9393                bookmarks => \@resources, 
    9494            }) 
  • trunk/Bookmarks.pm

    r24 r25  
    88has dbh      => ( is => 'rw' ); 
    99has base_uri => ( is => 'ro', isa => 'URI' ); 
     10 
     11has _sth_tags_from_uri => ( 
     12    is       => 'ro', 
     13    init_arg => undef, 
     14    lazy     => 1, 
     15    default  => sub { $_[0]->dbh->prepare('select tag from tags where uri = ? order by tag'); }, 
     16); 
    1017 
    1118sub BUILD { 
     
    2936    my $self = shift; 
    3037    my $params = shift; 
     38 
     39    # look for bookmark by id or uri 
    3140    my $sth; 
    3241    if ($params->{id}) { 
     
    4049    } 
    4150    my $bookmark = $sth->fetchrow_hashref; 
    42     if ($bookmark) { 
    43         my $sth_tag = $self->dbh->prepare('select tag from tags where uri = ? order by tag'); 
    44         $sth_tag->execute($bookmark->{uri}); 
    45         $bookmark->{tags} = [ map { $$_[0] } @{ $sth_tag->fetchall_arrayref } ]; 
    46         if ($self->base_uri) { 
    47             $bookmark->{bookmark_uri} = URI->new_abs($bookmark->{id}, $self->base_uri); 
    48         } 
    49     } 
    50     return $bookmark; 
     51    return unless $bookmark; 
     52 
     53    return Bookmark->new({ 
     54        %$bookmark, 
     55        tags     => [ $self->get_tags({ uri => $bookmark->{uri} }) ], 
     56        base_uri => $self->base_uri, 
     57    }); 
    5158} 
    5259 
     
    8592    $sth_resource->execute(@bind); 
    8693 
    87     my $sth_tag = $self->dbh->prepare('select tag from tags where uri = ? order by tag'); 
    8894    my @resources; 
    8995    while (my $resource = $sth_resource->fetchrow_hashref) { 
    90         $sth_tag->execute($resource->{uri}); 
    91         $resource->{tags} = [ map { $$_[0] } @{ $sth_tag->fetchall_arrayref } ]; 
    92         if ($self->base_uri) { 
    93             $resource->{bookmark_uri} = URI->new_abs($resource->{id}, $self->base_uri); 
    94         } 
    95         push @resources, $resource; 
     96        push @resources, Bookmark->new({ 
     97            %$resource, 
     98            tags     => [ $self->get_tags({ uri => $resource->{uri} }) ], 
     99            base_uri => $self->base_uri, 
     100        }); 
    96101    } 
    97102    return @resources; 
     
    101106    my $self = shift; 
    102107    my $params = shift; 
    103     my $tag = $params->{selected}; 
    104     my $sth_all_tags = $self->dbh->prepare('select tag, count(tag) as count, tag = ? as selected from tags group by tag order by tag'); 
    105     $sth_all_tags->execute($tag); 
    106     my $all_tags = $sth_all_tags->fetchall_arrayref({}); 
    107     return @{ $all_tags }; 
     108    if (my $uri = $params->{uri}) { 
     109        # get the tags for a particular URI 
     110        $self->_sth_tags_from_uri->execute($uri); 
     111        return map { $$_[0] } @{ $self->_sth_tags_from_uri->fetchall_arrayref }; 
     112    } else { 
     113        # return all tags 
     114        my $tag = $params->{selected}; 
     115        my $sth_all_tags = $self->dbh->prepare('select tag, count(tag) as count, tag = ? as selected from tags group by tag order by tag'); 
     116        $sth_all_tags->execute($tag); 
     117        my $all_tags = $sth_all_tags->fetchall_arrayref({}); 
     118        return @{ $all_tags }; 
     119    } 
    108120} 
    109121 
Note: See TracChangeset for help on using the changeset viewer.