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

Annotation of todotxt/Text-Todo/lib/Text/Todo.pm, Revision 1.4

1.1       andrew      1: package Text::Todo;
                      2:
1.4     ! andrew      3: # $RedRiver: Todo.pm,v 1.3 2010/01/06 19:54:56 andrew Exp $
1.1       andrew      4:
                      5: use warnings;
                      6: use strict;
                      7: use Carp;
                      8:
1.2       andrew      9: use Class::Std::Utils;
                     10: use Text::Todo::Entry;
                     11:
1.1       andrew     12: use version; our $VERSION = qv('0.0.1');
                     13:
1.2       andrew     14: {
                     15:
                     16:     my %file_of;
                     17:     my %list_of;
1.1       andrew     18:
1.2       andrew     19:     sub new {
                     20:         my ( $class, $file ) = @_;
1.1       andrew     21:
1.2       andrew     22:         my $self = bless anon_scalar(), $class;
                     23:         my $ident = ident($self);
                     24:
1.3       andrew     25:         if ($file) { $self->load($file); }
1.2       andrew     26:
                     27:         return $self;
                     28:     }
                     29:
1.3       andrew     30:     sub file {
1.2       andrew     31:         my ( $self, $file ) = @_;
                     32:         my $ident = ident($self);
                     33:
                     34:         if ($file) {
                     35:             $file_of{$ident} = $file;
                     36:         }
                     37:
1.3       andrew     38:         return $file_of{$ident};
                     39:     }
                     40:
                     41:     sub load {
                     42:         my ( $self, $file ) = @_;
                     43:         my $ident = ident($self);
                     44:
                     45:         $file = $self->file($file) || croak 'load requires a filename';
1.2       andrew     46:
                     47:         my @list;
                     48:         open my $fh, '<', $file or croak "Couldn't open [$file]: $!";
                     49:         while (<$fh>) {
                     50:             s/\r?\n$//xms;
                     51:             push @list, Text::Todo::Entry->new($_);
                     52:         }
                     53:         close $fh or croak "Couldn't close [$file]: $!";
                     54:         $list_of{$ident} = \@list;
                     55:
                     56:         return 1;
                     57:     }
                     58:
                     59:     sub save {
                     60:         my ( $self, $file ) = @_;
                     61:         my $ident = ident($self);
                     62:
1.3       andrew     63:         $file = $self->file($file) || croak 'save requires a filename';
1.2       andrew     64:
                     65:         open my $fh, '>', $file or croak "Couldn't open [$file]: $!";
                     66:         foreach my $e ( @{ $list_of{$ident} } ) {
1.3       andrew     67:             print {$fh} $e->text . "\n"
                     68:                 or croak "Couldn't print to [$file]: $!";
1.2       andrew     69:         }
                     70:         close $fh or croak "Couldn't close [$file]: $!";
                     71:
                     72:         return 1;
                     73:     }
                     74:
                     75:     sub list {
1.3       andrew     76:         my ($self) = @_;
1.2       andrew     77:         my $ident = ident($self);
                     78:         return if !$list_of{$ident};
                     79:
                     80:         return $list_of{$ident};
                     81:
                     82:         #my $id = 1;
                     83:         #my @l;
                     84:         #foreach my $e ( @{ $list_of{$ident} } ) {
                     85:         #    push @l, $e; #{ %{$e}, id => $id };
                     86:         #    $id++;
                     87:         #}
                     88:         #
                     89:         #my @list = sort { $a->priority cmp $b->priority }
                     90:         #    grep { defined $_->priority } @l;
                     91:         #
                     92:         #push @list, grep { !defined $_->priority } @l;
                     93:         #
                     94:         #return \@list;
                     95:     }
1.1       andrew     96:
1.3       andrew     97:     sub add {
                     98:         my ( $self, $entry ) = @_;
                     99:         my $ident = ident($self);
                    100:
                    101:         if ( ref $entry ) {
                    102:             if ( ref $entry ne 'Text::Todo::Entry' ) {
                    103:                 croak(    'entry is a '
                    104:                         . ref($entry)
                    105:                         . ' not a Text::Todo::Entry!' );
                    106:             }
                    107:         }
                    108:         else {
                    109:             $entry = Text::Todo::Entry->new($entry);
                    110:         }
                    111:
                    112:         push @{ $list_of{$ident} }, $entry;
                    113:
                    114:         return $entry;
                    115:     }
1.2       andrew    116: }
1.1       andrew    117:
1.2       andrew    118: 1;    # Magic true value required at end of module
1.1       andrew    119: __END__
                    120:
                    121: =head1 NAME
                    122:
1.4     ! andrew    123: Text::Todo - Perl interface to todo_txt files
1.1       andrew    124:
                    125:
                    126: =head1 SYNOPSIS
                    127:
                    128:     use Text::Todo;
                    129:
                    130: =head1 DESCRIPTION
                    131:
1.4     ! andrew    132: For more information see L<http://todotxt.com>
1.1       andrew    133:
                    134: =head1 INTERFACE
                    135:
1.2       andrew    136: =head2 new
                    137:
                    138: =head2 load
                    139:
                    140: =head2 save
                    141:
1.3       andrew    142: =head2 file
                    143:
1.2       andrew    144: =head2 list
1.3       andrew    145:
                    146: =head2 add
1.1       andrew    147:
                    148: =head1 DIAGNOSTICS
                    149:
                    150: =for author to fill in:
                    151:     List every single error and warning message that the module can
                    152:     generate (even the ones that will "never happen"), with a full
                    153:     explanation of each problem, one or more likely causes, and any
                    154:     suggested remedies.
                    155:
                    156: =over
                    157:
                    158: =item C<< Error message here, perhaps with %s placeholders >>
                    159:
                    160: [Description of error here]
                    161:
                    162: =item C<< Another error message here >>
                    163:
                    164: [Description of error here]
                    165:
                    166: [Et cetera, et cetera]
                    167:
                    168: =back
                    169:
                    170:
                    171: =head1 CONFIGURATION AND ENVIRONMENT
                    172:
                    173: Text::Todo requires no configuration files or environment variables.
                    174:
1.4     ! andrew    175: Someday it should be able to read and use the todo.sh config file.
        !           176:
1.1       andrew    177:
                    178: =head1 DEPENDENCIES
                    179:
                    180: =for author to fill in:
                    181:     A list of all the other modules that this module relies upon,
                    182:     including any restrictions on versions, and an indication whether
                    183:     the module is part of the standard Perl distribution, part of the
                    184:     module's distribution, or must be installed separately. ]
                    185:
                    186: None.
                    187:
                    188:
                    189: =head1 INCOMPATIBILITIES
                    190:
                    191: None reported.
                    192:
                    193:
                    194: =head1 BUGS AND LIMITATIONS
                    195:
                    196: No bugs have been reported.
                    197:
                    198: Please report any bugs or feature requests to
                    199: C<bug-text-todo@rt.cpan.org>, or through the web interface at
                    200: L<http://rt.cpan.org>.
                    201:
                    202:
                    203: =head1 AUTHOR
                    204:
                    205: Andrew Fresh  C<< <andrew@cpan.org> >>
                    206:
                    207:
                    208: =head1 LICENSE AND COPYRIGHT
                    209:
                    210: Copyright (c) 2009, Andrew Fresh C<< <andrew@cpan.org> >>. All rights reserved.
                    211:
                    212: This module is free software; you can redistribute it and/or
                    213: modify it under the same terms as Perl itself. See L<perlartistic>.
                    214:
                    215:
                    216: =head1 DISCLAIMER OF WARRANTY
                    217:
                    218: BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
                    219: FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
                    220: OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
                    221: PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
                    222: EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
                    223: WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
                    224: ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
                    225: YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
                    226: NECESSARY SERVICING, REPAIR, OR CORRECTION.
                    227:
                    228: IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
                    229: WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
                    230: REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
                    231: LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
                    232: OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
                    233: THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
                    234: RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
                    235: FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
                    236: SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
                    237: SUCH DAMAGES.

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