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