File Coverage

blib/lib/Config/MVP/Reader/Hash.pm
Criterion Covered Total %
statement 19 19 100.0
branch 3 4 75.0
condition 2 3 66.6
subroutine 3 3 100.0
pod 1 1 100.0
total 28 30 93.3


line stmt bran cond sub pod time code
1             package Config::MVP::Reader::Hash 2.200013;
2             # ABSTRACT: a reader that tries to cope with a plain old hashref
3              
4 2     2   6709 use Moose;
  2         4  
  2         13  
5             extends 'Config::MVP::Reader';
6              
7             #pod =head1 SYNOPSIS
8             #pod
9             #pod my $sequence = Config::MVP::Reader::Hash->new->read_config( \%config );
10             #pod
11             #pod =head1 DESCRIPTION
12             #pod
13             #pod In some ways, this is the L<Config::MVP::Reader> of last resort. Given a
14             #pod hashref, it attempts to interpret it as a Config::MVP::Sequence. Because
15             #pod hashes are generally unordered, order can't be relied upon unless the hash tied
16             #pod to have order (presumably with L<Tie::IxHash>). The hash keys are assumed to
17             #pod be section names and will be used as the section package moniker unless a
18             #pod L<__package> entry is found.
19             #pod
20             #pod =cut
21              
22             sub read_into_assembler {
23 3     3 1 8 my ($self, $location, $assembler) = @_;
24              
25 3 50       10 confess "no hash given to $self" unless my $hash = $location;
26              
27 3         11 for my $name (keys %$hash) {
28 6         117 my $payload = { %{ $hash->{ $name } } };
  6         19  
29 6   66     23 my $package = delete($payload->{__package}) || $name;
30              
31 6         27 $assembler->begin_section($package, $name);
32              
33 6         106 for my $key (%$payload) {
34 16         33 my $val = $payload->{ $key };
35 16 100       35 my @values = ref $val ? @$val : $val;
36 16         51 $assembler->add_value($key => $_) for @values;
37             }
38              
39 6         21 $assembler->end_section;
40             }
41              
42 3         259 return $assembler->sequence;
43             }
44              
45 2     2   11298 no Moose;
  2         6  
  2         10  
46             __PACKAGE__->meta->make_immutable;
47             1;
48              
49             __END__
50              
51             =pod
52              
53             =encoding UTF-8
54              
55             =head1 NAME
56              
57             Config::MVP::Reader::Hash - a reader that tries to cope with a plain old hashref
58              
59             =head1 VERSION
60              
61             version 2.200013
62              
63             =head1 SYNOPSIS
64              
65             my $sequence = Config::MVP::Reader::Hash->new->read_config( \%config );
66              
67             =head1 DESCRIPTION
68              
69             In some ways, this is the L<Config::MVP::Reader> of last resort. Given a
70             hashref, it attempts to interpret it as a Config::MVP::Sequence. Because
71             hashes are generally unordered, order can't be relied upon unless the hash tied
72             to have order (presumably with L<Tie::IxHash>). The hash keys are assumed to
73             be section names and will be used as the section package moniker unless a
74             L<__package> entry is found.
75              
76             =head1 PERL VERSION
77              
78             This module should work on any version of perl still receiving updates from
79             the Perl 5 Porters. This means it should work on any version of perl released
80             in the last two to three years. (That is, if the most recently released
81             version is v5.40, then this module should work on both v5.40 and v5.38.)
82              
83             Although it may work on older versions of perl, no guarantee is made that the
84             minimum required version will not be increased. The version may be increased
85             for any reason, and there is no promise that patches will be accepted to lower
86             the minimum required perl.
87              
88             =head1 AUTHOR
89              
90             Ricardo Signes <cpan@semiotic.systems>
91              
92             =head1 COPYRIGHT AND LICENSE
93              
94             This software is copyright (c) 2022 by Ricardo Signes.
95              
96             This is free software; you can redistribute it and/or modify it under
97             the same terms as the Perl 5 programming language system itself.
98              
99             =cut