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

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

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

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