| 1 | package MooseX::Role::REST::Consumer::Response; | ||||||||||
| 2 | 1 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 1 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | use overload | ||||||||||
| 5 | 0 | ▶ | 'bool' => sub { shift->is_success }, | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.6
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 6 | 0 | ▶ | '""' => sub { shift->_stringify() }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.2
| |||||||||||
| 7 | |||||||||||
| 8 | 1 | has 'is_success' => ( isa => 'Bool', is => 'ro', required => 1 ); | |||||||||
| 9 | 1 | has 'error_message' => ( isa => 'Str', is => 'ro', required => 0 ); | |||||||||
| 10 | 1 | has 'data' => ( is => 'ro', required => 0 ); | |||||||||
| 11 | 1 | has 'request' => ( is => 'ro' ); | |||||||||
| 12 | 1 | has 'response' => ( is => 'ro' ); | |||||||||
| 13 | |||||||||||
| 14 | 0 | ▶ | sub success { | ||||||||
|
Subroutine
success: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 15 | 0 | my $class = shift; | |||||||||
| 16 | 0 | return $class->new(is_success => 1, @_); | |||||||||
| 17 | } | ||||||||||
| 18 | |||||||||||
| 19 | 0 | ▶ | sub failure { | ||||||||
|
Subroutine
failure: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 20 | 0 | my $class = shift; | |||||||||
| 21 | 0 | return $class->new(is_success => 0, @_); | |||||||||
| 22 | } | ||||||||||
| 23 | |||||||||||
| 24 | 0 | ▶ | sub is_failure { | ||||||||
|
Subroutine
is_failure: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 25 | 0 | my $self = shift; | |||||||||
| 26 | 0 | ▶ | return $self->is_success ? 0 : 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | } | ||||||||||
| 28 | |||||||||||
| 29 | 0 | ▶ | sub _stringify { | ||||||||
|
Subroutine
_stringify: not called CC=2 SCAR=17.9
| |||||||||||
| 30 | 0 | my $self = shift; | |||||||||
| 31 | |||||||||||
| 32 | 0 | ▶ | if (defined $self->error_message) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | 0 | return $self->error_message; | |||||||||
| 34 | } | ||||||||||
| 35 | 0 | return ''; | |||||||||
| 36 | } | ||||||||||
| 37 | |||||||||||
| 38 | 1 | __PACKAGE__->meta->make_immutable(); | |||||||||
| 39 | |||||||||||
| 40 | =cut | ||||||||||
| 41 | |||||||||||
| 42 | =head1 NAME | ||||||||||
| 43 | |||||||||||
| 44 | MooseX::Role::REST::Consumer::Response - A simple utility to provide object-oriented return values with messaging | ||||||||||
| 45 | |||||||||||
| 46 | =head1 SYNOPSIS | ||||||||||
| 47 | |||||||||||
| 48 | use MooseX::Role::REST::Consumer::Response | ||||||||||
| 49 | |||||||||||
| 50 | sub do_something { | ||||||||||
| 51 | .... | ||||||||||
| 52 | |||||||||||
| 53 | return MooseX::Role::REST::Consumer::Response->success( | ||||||||||
| 54 | message => "Everything worked", | ||||||||||
| 55 | ); | ||||||||||
| 56 | } | ||||||||||
| 57 | |||||||||||
| 58 | my $response = do_something(); | ||||||||||
| 59 | print "Response: $response\n"; | ||||||||||
| 60 | if (! $response) { | ||||||||||
| 61 | ... | ||||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | =head1 DESCRIPTION | ||||||||||
| 65 | |||||||||||
| 66 | The B<Reponse> object in this class can be used in boolean or string context and should DWIM. | ||||||||||
| 67 | |||||||||||
| 68 | =head1 CLASS METHODS | ||||||||||
| 69 | |||||||||||
| 70 | =head2 new (%param) | ||||||||||
| 71 | |||||||||||
| 72 | =over 4 | ||||||||||
| 73 | |||||||||||
| 74 | =item B<is_success> - Boolean of if success or not | ||||||||||
| 75 | |||||||||||
| 76 | =item B<message> - Optional text message | ||||||||||
| 77 | |||||||||||
| 78 | =back | ||||||||||
| 79 | |||||||||||
| 80 | Returns blessed object | ||||||||||
| 81 | |||||||||||
| 82 | =head2 success (%param) | ||||||||||
| 83 | |||||||||||
| 84 | Takes same parameters as B<new> but implies ( is_success => 1 ); | ||||||||||
| 85 | |||||||||||
| 86 | =head2 failure (%param) | ||||||||||
| 87 | |||||||||||
| 88 | Similar to B<success> but implies ( is_success => 0 ); | ||||||||||
| 89 | |||||||||||
| 90 | =head1 OBJECT METHODS | ||||||||||
| 91 | |||||||||||
| 92 | First, some basic L<Moose> attribute accessors: | ||||||||||
| 93 | |||||||||||
| 94 | =over 4 | ||||||||||
| 95 | |||||||||||
| 96 | =item B<is_success> | ||||||||||
| 97 | |||||||||||
| 98 | =item B<error_message> | ||||||||||
| 99 | |||||||||||
| 100 | =back | ||||||||||
| 101 | |||||||||||
| 102 | |||||||||||
| 103 | =head2 is_failure | ||||||||||
| 104 | |||||||||||
| 105 | Returns boolean | ||||||||||
| 106 | |||||||||||
| 107 | =cut | ||||||||||
| 108 | |||||||||||
| 109 | |||||||||||