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