[BACK]Return to API.pm CVS log [TXT][DIR] Up to [local] / todotxt / Text-Todo-REST-API / lib / Text / Todo / REST

Diff for /todotxt/Text-Todo-REST-API/lib/Text/Todo/REST/API.pm between version 1.7 and 1.8

version 1.7, 2010/01/18 13:47:53 version 1.8, 2010/01/19 03:18:34
Line 1 
Line 1 
 package Text::Todo::REST::API;  package Text::Todo::REST::API;
   
 # $AFresh1: API.pm,v 1.6 2010/01/18 03:51:40 andrew Exp $  # $AFresh1: API.pm,v 1.7 2010/01/18 13:47:53 andrew Exp $
   
 use warnings;  use warnings;
 use strict;  use strict;
Line 10 
Line 10 
 use Text::Todo;  use Text::Todo;
   
 use Class::Std::Utils;  use Class::Std::Utils;
   use Digest::MD5 qw/ md5_hex /;
   
 use Module::Pluggable  use Module::Pluggable
     require     => 1,      instantiate => 'new',
     search_path => __PACKAGE__ . '::Representations',      search_path => __PACKAGE__ . '::Representations',
     sub_name    => 'representations';      sub_name    => 'representations';
 use Digest::MD5 qw/ md5_hex /;  
   
 use version; our $VERSION = qv('0.0.1');  use version; our $VERSION = qv('0.0.1');
   
Line 33 
Line 34 
         my %suffix_of,          my %suffix_of,
         my %file_regex_of,          my %file_regex_of,
   
           my %format_of,
         my %user_of,          my %user_of,
         my %list_of,          my %list_of,
         my %action_of,          my %action_of,
Line 44 
Line 46 
     sub new {      sub new {
         my ( $class, $options ) = @_;          my ( $class, $options ) = @_;
   
         my $format = $options->{default_format};          my $self = bless anon_scalar(), $class;
           my $ident = ident($self);
   
           $format_of{$ident} = $options->{default_format};
         if ( $options->{format} ) {          if ( $options->{format} ) {
             $format = $options->{format};              $format_of{$ident} = $options->{format};
         }          }
         elsif ($options->{path_info}          elsif ($options->{path_info}
             && $options->{path_info} =~ s/\.(\w+)$//xms )              && $options->{path_info} =~ s/\.(\w+)$//xms )
         {          {
             $format = $1;              $format_of{$ident} = $1;
         }          }
   
         my $self = bless anon_scalar(), $class;          $suffix_of{$ident} = $options->{suffix} || '.txt';
         my $ident = ident($self);  
   
         if ( ref $self eq __PACKAGE__ && $format ) {  
             my $found_handler = 0;  
         REP: foreach my $rep ( $self->representations ) {  
                 if ( $rep->_handles($format) ) {  
                     $self          = $rep->new($options);  
                     $found_handler = 1;  
                     last REP;  
                 }  
             }  
             if ( !$found_handler ) {  
                 croak("Unable to find handler for [$format]\n");  
             }  
         }  
   
         $suffix_of{$ident}  = $options->{suffix} || '.txt';  
   
         $file_regex_of{$ident} = $options->{file_regex} || qr{          $file_regex_of{$ident} = $options->{file_regex} || qr{
             .*              .*
             todo              todo
Line 83 
Line 71 
   
         $options->{path_info} ||= q{};          $options->{path_info} ||= q{};
         $options->{path_info} =~ s{^/}{}xms;          $options->{path_info} =~ s{^/}{}xms;
         (   $list_of{$ident}, $action_of{$ident}, @{ $args_of{$ident} },          ( $list_of{$ident}, $action_of{$ident}, @{ $args_of{$ident} }, )
         ) = split '/', $options->{path_info};              = split '/', $options->{path_info};
   
         if ( $list_of{$ident} ) {          if ( $list_of{$ident} ) {
             $action_of{$ident} ||= 'list';              $action_of{$ident} ||= 'list';
Line 93 
Line 81 
             $action_of{$ident} = 'files';              $action_of{$ident} = 'files';
         }          }
   
         eval { $todo_of{$ident} = Text::Todo->new(          eval {
             {   todo_dir  => $options->{todo_dir},              $todo_of{$ident} = Text::Todo->new(
                 todo_file => $options->{todo_file},                  {   todo_dir  => $options->{todo_dir},
             }                      todo_file => $options->{todo_file},
         ) };                  }
               );
           };
         if ($@) {          if ($@) {
             $self->fail('Unable to create Text::Todo object' . $@);              $self->fail( 'Unable to create Text::Todo object' . $@ );
         }          }
   
         $todo_of{$ident}->load('todo_file')          $todo_of{$ident}->load('todo_file')
Line 120 
Line 110 
   
     sub content_type {return}      sub content_type {return}
   
     sub Dump {      sub _handle_representation {
         my ($self) = @_;          my ( $self, $type, @args ) = @_;
         return $self->fail( 'Unable to Dump [' . $self->_action . ']' );  
           my $method = join q{_}, $type, $self->_action;
   
           foreach my $class ( $self->representations ) {
               if (   $class->can_format( $self->_format )
                   && $class->can( $method ) )
               {
                   return $class->$method($self->_format, @args);
               }
           }
   
           return $self->fail(
               'Unable to ' . $type . ' [' . $self->_action . ']' );
     }      }
   
     sub Load {      sub Dump {
         my ($self) = @_;          my ( $self, @args ) = @_;
         return $self->fail( 'Unable to Load [' . $self->_action . ']' );          return $self->_handle_representation( 'dump', @args );
     }      }
   
     sub _handle_action {      sub _handle_action {
Line 247 
Line 249 
     sub _user       { my ($self) = @_; return $user_of{ ident $self}; }      sub _user       { my ($self) = @_; return $user_of{ ident $self}; }
     sub _list       { my ($self) = @_; return $list_of{ ident $self}; }      sub _list       { my ($self) = @_; return $list_of{ ident $self}; }
     sub _action     { my ($self) = @_; return $action_of{ ident $self}; }      sub _action     { my ($self) = @_; return $action_of{ ident $self}; }
       sub _format     { my ($self) = @_; return $format_of{ ident $self}; }
     sub _args       { my ($self) = @_; return $args_of{ ident $self}; }      sub _args       { my ($self) = @_; return $args_of{ ident $self}; }
   
     sub DESTROY {      sub DESTROY {

Legend:
Removed from v.1.7  
changed lines
  Added in v.1.8

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>