File Coverage

blib/lib/Data/Decode/Chain.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1              
2             package Data::Decode::Chain;
3 2     2   6494 use Moose;
  0            
  0            
4             use MooseX::AttributeHelpers;
5             use namespace::clean -except => qw(meta);
6             use Data::Decode::Exception;
7             use Data::Decode::Types;
8              
9             has decoders => (
10             metaclass => 'Collection::Array',
11             is => 'ro',
12             isa => 'Data::Decode::DecoderList',
13             required => 1,
14             coerce => 1,
15             provides => {
16             elements => 'all_decoders',
17             }
18             );
19              
20             sub decode {
21             my ($self, $decoder, $string, $hints) = @_;
22              
23             my $ret;
24             foreach my $decoder ($self->all_decoders) {
25             $ret = eval {
26             $decoder->decode($decoder, $string, $hints);
27             };
28             my $e;
29             if ($e = Data::Decode::Exception::Deferred->caught() ) {
30             # Decoding was deffered, we don't do anything about this
31             # error, and simply let the next decoder attempt to handle
32             # this particular set of inputs.
33             next;
34             } elsif ( $e = Exception::Class->caught() ) {
35             # This is a generic error, just propagate it
36             eval { $e->isa('Data::Decode::Exception') } ?
37             $e->rethrow : die $e;
38             }
39             last;
40             }
41              
42             return $ret;
43             }
44              
45             1;
46              
47             __END__
48              
49             =head1 NAME
50              
51             Data::Decode::Chain - Chain Multiple Decoders
52              
53             =head1 SYNOPSIS
54              
55             Data::Decode->new(
56             strategy => Data::Decode::Chain->new(
57             decoders => [
58             Data::Decode::Whatever->new,
59             Data::Decode::SomethingElse->new
60             ]
61             )
62             );
63              
64             =head1 METHODS
65              
66             =head2 new
67              
68             =head2 decode
69              
70             =cut