[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.7

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

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