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