| 1 | package Reply::Plugin::DataDump; | ||
| 2 | our $AUTHORITY = 'cpan:DOY'; | ||
| 3 | $Reply::Plugin::DataDump::VERSION = '0.42'; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | # ABSTRACT: format results using Data::Dump | ||
| 7 | |||
| 8 | use base 'Reply::Plugin'; | ||
| 9 | |||
| 10 | use Data::Dump 'dumpf'; | ||
| 11 | use overload (); | ||
| 12 | |||
| 13 | |||
| 14 | sub new { | ||
| 15 | my $class = shift; | ||
| 16 | my %opts = @_; | ||
| 17 | $opts{respect_stringification} = 1 | ||
| 18 | unless defined $opts{respect_stringification}; | ||
| 19 | |||
| 20 | my $self = $class->SUPER::new(@_); | ||
| 21 | $self->{filter} = sub { | ||
| 22 | my ($ctx, $ref) = @_; | ||
| 23 | return unless $ctx->is_blessed; | ||
| 24 | my $stringify = overload::Method($ref, '""'); | ||
| 25 | return unless $stringify; | ||
| 26 | return { | ||
| 27 | dump => $stringify->($ref), | ||
| 28 | }; | ||
| 29 | } if $opts{respect_stringification}; | ||
| 30 | |||
| 31 | return $self; | ||
| 32 | } | ||
| 33 | |||
| 34 | sub mangle_result { | ||
| 35 | my $self = shift; | ||
| 36 | my (@result) = @_; | ||
| 37 | return @result ? dumpf(@result, $self->{filter}) : (); | ||
| 38 | } | ||
| 39 | |||
| 40 | 1; | ||
| 41 | |||
| 42 | __END__ |