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

Annotation of todotxt/Text-Todo-REST-API/lib/Text/Todo/REST/API/Actions/GET.pm, Revision 1.5

1.1       andrew      1: package Text::Todo::REST::API::Actions::GET;
                      2:
1.5     ! andrew      3: # $AFresh1: GET.pm,v 1.4 2010/02/14 06:14:57 andrew Exp $
1.1       andrew      4:
                      5: use warnings;
                      6: use strict;
                      7: use Carp;
                      8:
                      9: use Class::Std::Utils;
                     10: use Digest::MD5 qw/ md5_hex /;
                     11:
                     12: use version; our $VERSION = qv('0.0.1');
                     13:
                     14: {
                     15:     my @attr_refs = \();
                     16:
                     17:     sub new {
                     18:         my ( $class, $options ) = @_;
                     19:
                     20:         my $self = bless anon_scalar(), $class;
                     21:         my $ident = ident($self);
                     22:
                     23:         return $self;
                     24:     }
                     25:
                     26:     sub get_entry_done {
                     27:         my ( $self, $todo, $key ) = @_;
                     28:         my $entry = $self->get_entry( $todo, $key );
                     29:
                     30:         return $todo->list->[ $entry->{line} - 1 ]->done;
                     31:     }
                     32:
                     33:     sub get_entry {
1.4       andrew     34:         my ( $self, $todo, $options ) = @_;
                     35:         my $entry = $self->_which_key( $options, 'entry' );
1.1       andrew     36:
                     37:         my $list = $self->get_list($todo);
                     38:
1.3       andrew     39:         if ( $entry =~ /^[[:xdigit:]]{32}$/xms ) {
                     40:             my $search = lc $entry;
1.1       andrew     41:
                     42:             foreach my $e ( @{$list} ) {
                     43:                 if ( $search eq $e->{md5} ) {
                     44:                     return $e;
                     45:                 }
                     46:             }
                     47:         }
1.3       andrew     48:         elsif ( $entry =~ /^\d+$/xms ) {
                     49:             return $list->[ $entry - 1 ];
1.1       andrew     50:         }
                     51:
1.3       andrew     52:         return $self->_fail("Unable to find entry [$entry]!");
1.1       andrew     53:     }
                     54:
                     55:     sub get_list {
                     56:         my ( $self, $todo ) = @_;
                     57:
                     58:         my $line = 1;
                     59:         my @list = map ( {
                     60:                 line => $line++,
                     61:                 md5  => md5_hex( $_->text ),
                     62:                 text => $_->text,
                     63:             },
                     64:             $todo->list );
                     65:         return \@list;
                     66:     }
                     67:
                     68:     sub get_files {
                     69:         my ( $self, $todo, $options ) = @_;
                     70:         my $dir = $todo->file('todo_dir');
                     71:
                     72:         if ( !$dir ) {
1.2       andrew     73:             return $self->_fail('Unable to find todo_dir');
1.1       andrew     74:         }
                     75:
                     76:         my $file_regex = $options->{file_regex};
                     77:
                     78:         opendir my $dh, $dir or croak "Couldn't opendir: $!";
                     79:         my @files = grep {m/$file_regex/xms} readdir $dh;
                     80:         closedir $dh;
                     81:
                     82:         return \@files;
                     83:     }
                     84:
                     85:     sub get_tags {
1.4       andrew     86:         my ( $self, $todo, $options ) = @_;
                     87:         my $tag = $self->_which_key( $options, 'tags' );
                     88:         if ($tag) {
                     89:             return [ $todo->listtag($tag) ];
                     90:         }
                     91:         else {
                     92:             return $todo->known_tags;
                     93:         }
1.3       andrew     94:     }
                     95:
                     96:     sub _which_key {
1.4       andrew     97:         my ( $self, $key, $type ) = @_;
                     98:
                     99:         return if !defined $key;
1.3       andrew    100:
1.4       andrew    101:         if ( ref $key eq 'ARRAY' ) {
1.3       andrew    102:             $key = $key->[0];
                    103:         }
                    104:         elsif ( ref $key eq 'HASH' ) {
1.5     ! andrew    105:             $key = $key->{$type};
1.3       andrew    106:         }
                    107:
                    108:         return $key;
1.1       andrew    109:     }
                    110:
                    111:     sub _fail {
                    112:         my ( $self, @message ) = @_;
                    113:         croak(@message);
                    114:     }
                    115:
                    116:     sub DESTROY {
                    117:         my ($self) = @_;
                    118:         my $ident = ident $self;
                    119:         foreach my $attr_ref (@attr_refs) {
                    120:             delete $attr_ref->{$ident};
                    121:         }
                    122:     }
                    123: }
                    124: 1;    # Magic true value required at end of module
                    125: __END__
                    126:
                    127: =head1 NAME
                    128:
                    129: Text::Todo::REST::API - [One line description of module's purpose here]
                    130:
                    131:
                    132: =head1 VERSION
                    133:
                    134: This document describes Text::Todo::REST::API version 0.0.1
                    135:
                    136:
                    137: =head1 SYNOPSIS
                    138:
                    139:     use Text::Todo::REST::API;
                    140:
                    141: =for author to fill in:
                    142:     Brief code example(s) here showing commonest usage(s).
                    143:     This section will be as far as many users bother reading
                    144:     so make it as educational and exeplary as possible.
                    145:
                    146:
                    147: =head1 DESCRIPTION
                    148:
                    149: =for author to fill in:
                    150:     Write a full description of the module and its features here.
                    151:     Use subsections (=head2, =head3) as appropriate.
                    152:
                    153:
                    154: =head1 INTERFACE
                    155:
                    156: =for author to fill in:
                    157:     Write a separate section listing the public components of the modules
                    158:     interface. These normally consist of either subroutines that may be
                    159:     exported, or methods that may be called on objects belonging to the
                    160:     classes provided by the module.
                    161:
                    162:
                    163: =head1 DIAGNOSTICS
                    164:
                    165: =for author to fill in:
                    166:     List every single error and warning message that the module can
                    167:     generate (even the ones that will "never happen"), with a full
                    168:     explanation of each problem, one or more likely causes, and any
                    169:     suggested remedies.
                    170:
                    171: =over
                    172:
                    173: =item C<< Error message here, perhaps with %s placeholders >>
                    174:
                    175: [Description of error here]
                    176:
                    177: =item C<< Another error message here >>
                    178:
                    179: [Description of error here]
                    180:
                    181: [Et cetera, et cetera]
                    182:
                    183: =back
                    184:
                    185:
                    186: =head1 CONFIGURATION AND ENVIRONMENT
                    187:
                    188: =for author to fill in:
                    189:     A full explanation of any configuration system(s) used by the
                    190:     module, including the names and locations of any configuration
                    191:     files, and the meaning of any environment variables or properties
                    192:     that can be set. These descriptions must also include details of any
                    193:     configuration language used.
                    194:
                    195: Text::Todo::REST::API requires no configuration files or environment variables.
                    196:
                    197:
                    198: =head1 DEPENDENCIES
                    199:
                    200: =for author to fill in:
                    201:     A list of all the other modules that this module relies upon,
                    202:     including any restrictions on versions, and an indication whether
                    203:     the module is part of the standard Perl distribution, part of the
                    204:     module's distribution, or must be installed separately. ]
                    205:
                    206: None.
                    207:
                    208:
                    209: =head1 INCOMPATIBILITIES
                    210:
                    211: =for author to fill in:
                    212:     A list of any modules that this module cannot be used in conjunction
                    213:     with. This may be due to name conflicts in the interface, or
                    214:     competition for system or program resources, or due to internal
                    215:     limitations of Perl (for example, many modules that use source code
                    216:     filters are mutually incompatible).
                    217:
                    218: None reported.
                    219:
                    220:
                    221: =head1 BUGS AND LIMITATIONS
                    222:
                    223: =for author to fill in:
                    224:     A list of known problems with the module, together with some
                    225:     indication Whether they are likely to be fixed in an upcoming
                    226:     release. Also a list of restrictions on the features the module
                    227:     does provide: data types that cannot be handled, performance issues
                    228:     and the circumstances in which they may arise, practical
                    229:     limitations on the size of data sets, special cases that are not
                    230:     (yet) handled, etc.
                    231:
                    232: No bugs have been reported.
                    233:
                    234: Please report any bugs or feature requests to
                    235: C<bug-text-todo-rest-api@rt.cpan.org>, or through the web interface at
                    236: L<http://rt.cpan.org>.
                    237:
                    238:
                    239: =head1 AUTHOR
                    240:
                    241: Andrew Fresh  C<< <andrew@cpan.org> >>
                    242:
                    243:
                    244: =head1 LICENSE AND COPYRIGHT
                    245:
                    246: Copyright (c) 2010, Andrew Fresh C<< <andrew@cpan.org> >>. All rights reserved.
                    247:
                    248: This module is free software; you can redistribute it and/or
                    249: modify it under the same terms as Perl itself. See L<perlartistic>.
                    250:
                    251:
                    252: =head1 DISCLAIMER OF WARRANTY
                    253:
                    254: BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
                    255: FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
                    256: OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
                    257: PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
                    258: EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
                    259: WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
                    260: ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
                    261: YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
                    262: NECESSARY SERVICING, REPAIR, OR CORRECTION.
                    263:
                    264: IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
                    265: WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
                    266: REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
                    267: LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
                    268: OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
                    269: THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
                    270: RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
                    271: FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
                    272: SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
                    273: SUCH DAMAGES.

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