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

Annotation of todotxt/Text-Todo-REST-API/lib/Text/Todo/REST/API/Response.pm, Revision 1.4

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

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