InputMonster.pm

filter: Statementstmt 100% 55 / 55 Branchbran 76.4% 26 / 34 Conditioncond 50.0% 11 / 22 MC/DCmcdc 7.6% 1 / 13 Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 76.5% 101 / 132 CC 30 SCAR 26.5
CC
30
Cov
83%
_assert_field_spec_ok
18.7
consume
16.0
new
6.4
CRAP 14.2
↑ Summary InputMonster/Util.pm »
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Data::InputMonster 0.011;
4 # ABSTRACT: consume data from multiple sources, best first; om nom nom!
5  
6 #pod =head1 DESCRIPTION
7 #pod
8 #pod This module lets you describe a bunch of input fields you expect. For each
9 #pod field, you can specify validation, a default, and multiple places to look for a
10 #pod value. The first valid value found is accepted and returned in the results.
11 #pod
12 #pod =cut
13  
14 1 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 #pod =method new
17 #pod
18 #pod my $monster = Data::InputMonster->new({
19 #pod fields => {
20 #pod field_name => \%field_spec,
21 #pod ...
22 #pod },
23 #pod });
24 #pod
25 #pod This builds a new monster. For more information on the C<%field_spec>
26 #pod parameters, see below.
27 #pod
28 #pod =cut
29  
30 1 ▶sub new {
Subroutine new: called CC=5 SCAR=18.6
Pod documented
31 1 my ($class, $arg) = @_;
32
33 Carp::confess("illegal parameters to Data::InputMonster constructor")
34 1 ▶ unless $arg and (keys %$arg == 1) and exists $arg->{fields};
Branch: 1 branch
BranchTrueFalseTotal
unless $arg and keys %$arg == 1 and exists $arg->{"fields"} 0 1 1
Condition33%2/6
$arg and keys %$arg == 1 and exists $arg->{"fields"}
!ll&&!rl&&r
001
$arg and keys %$arg == 1
!ll&&!rl&&r
001
MC/DC0%0/3
$arg and keys %$arg == 1 and exists $arg->{"fields"}
$arg keys %$arg == 1 exists $arg->{"fields"}
Truth table25%1/4
A and B and C
A= $argB= keys %$arg == 1C= exists $arg->{"fields"}
ABCresult
0XX0
10X0
1100
1111
35  
36 1 my $fields = $arg->{fields};
37  
38 1 $class->_assert_field_spec_ok($_) for values %$fields;
39  
40 1 bless { fields => $fields } => $class;
41 }
42  
43 3 ▶sub _assert_field_spec_ok {
Subroutine _assert_field_spec_ok: called CC=11 SCAR=29.3
44 3 my ($self, $spec) = @_;
45  
46 Carp::confess("illegal or missing sources")
47 3 ▶ unless $spec->{sources} and ref $spec->{sources} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless $spec->{"sources"} and ref $spec->{"sources"} eq "ARRAY" 0 3 3
Condition33%1/3
$spec->{"sources"} and ref $spec->{"sources"} eq "ARRAY"
!ll&&!rl&&r
003
MC/DC0%0/2
$spec->{"sources"} and ref $spec->{"sources"} eq "ARRAY"
$spec->{"sources"} ref $spec->{"sources"} eq "ARRAY"
Truth table33%1/3
A and B
A= $spec->{"sources"}B= ref $spec->{"sources"} eq "ARRAY"
ABresult
0X0
100
111
48  
49 Carp::confess("if given, filter must be a coderef")
50 3 ▶ if $spec->{filter} and ref $spec->{filter} ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if $spec->{"filter"} and ref $spec->{"filter"} ne "CODE" 0 3 3
Condition66%2/3
$spec->{"filter"} and ref $spec->{"filter"} ne "CODE"
!ll&&!rl&&r
210
MC/DC0%0/2
$spec->{"filter"} and ref $spec->{"filter"} ne "CODE"
$spec->{"filter"} ref $spec->{"filter"} ne "CODE"
Truth table66%2/3
A and B
A= $spec->{"filter"}B= ref $spec->{"filter"} ne "CODE"
ABresult
0X0
100
111
51  
52 Carp::confess("if given, check must be a coderef")
53 3 ▶ if $spec->{check} and ref $spec->{check} ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if $spec->{"check"} and ref $spec->{"check"} ne "CODE" 0 3 3
Condition33%1/3
$spec->{"check"} and ref $spec->{"check"} ne "CODE"
!ll&&!rl&&r
030
MC/DC0%0/2
$spec->{"check"} and ref $spec->{"check"} ne "CODE"
$spec->{"check"} ref $spec->{"check"} ne "CODE"
Truth table33%1/3
A and B
A= $spec->{"check"}B= ref $spec->{"check"} ne "CODE"
ABresult
0X0
100
111
54  
55 Carp::confess("if given, store must be a coderef")
56 3 ▶ if $spec->{store} and ref $spec->{store} ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if $spec->{"store"} and ref $spec->{"store"} ne "CODE" 0 3 3
Condition66%2/3
$spec->{"store"} and ref $spec->{"store"} ne "CODE"
!ll&&!rl&&r
210
MC/DC0%0/2
$spec->{"store"} and ref $spec->{"store"} ne "CODE"
$spec->{"store"} ref $spec->{"store"} ne "CODE"
Truth table66%2/3
A and B
A= $spec->{"store"}B= ref $spec->{"store"} ne "CODE"
ABresult
0X0
100
111
57  
58 Carp::confess("defaults that are references must be wrapped in code")
59 3 ▶ if ((ref $spec->{default})||'CODE') ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if (ref $spec->{"default"} || "CODE") ne "CODE" 0 3 3
Condition50%1/2
ref $spec->{'default'} || 'CODE'
l!l
03
MC/DC0%0/1
ref $spec->{'default'} || 'CODE'
ref $spec->{'default'}
Truth table50%1/2
A
A= ref $spec->{'default'}
Aresult
11
00
60 }
61  
62 #pod =method consume
63 #pod
64 #pod my $result = $monster->consume($input, \%arg);
65 #pod
66 #pod This method processes the given input and returns a hashref of the finally
67 #pod accepted values. C<$input> can be anything; it is up to the field definitions
68 #pod to expect and handle the data you plan to feed the monster.
69 #pod
70 #pod Valid arguments are:
71 #pod
72 #pod no_default_for - a field name or arrayref of field names for which to NOT
73 #pod fall back to default values
74 #pod
75 #pod =cut
76  
77 4 ▶sub consume {
Subroutine consume: called CC=16 SCAR=27.7
Pod documented
78 4 my ($self, $input, $arg) = @_;
79 4 ▶ $arg ||= {};
Condition100%2/2
$arg ||= {}
l!l
22
MC/DC100%1/1
$arg ||= {}
$arg
Truth table100%2/2
A
A= $arg
Aresult
11
00
80  
81 my %no_default_for
82 = (! $arg->{no_default_for}) ? ()
83 1 : (ref $arg->{no_default_for}) ? (map {$_=>1} @{$arg->{no_default_for}})
84 4 ▶ : ($arg->{no_default_for} => 1);
Branch: 2 branches
BranchTrueFalseTotal
!$arg->{'no_default_for'} ? : 2 2 4
ref $arg->{'no_default_for'} ? : 1 1 2
85  
86 4 my $field = $self->{fields};
87 4 my %output;
88  
89 4 FIELD: for my $field_name (keys %$field) {
90 12 my $spec = $field->{$field_name};
91  
92 12 my $checker = $spec->{check};
93 12 my $filter = $spec->{filter};
94 12 my $storer = $spec->{store};
95  
96 12 my @sources = @{ $spec->{sources} };
97  
98 12 ▶ if (ref $sources[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $sources[0]) 4 8 12
99 4 my $i = 1;
100 4 @sources = map { ("source_" . $i++) => $_ } @sources;
101 }
102  
103 12 my $input_arg = { field_name => $field_name };
104  
105 12 ▶ SOURCE: for (my $i = 0; $i < @sources; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @sources) 23 7 30
106 23 my ($name, $getter) = @sources[ $i, $i + 1 ];
107 23 my $value = $getter->($self, $input, $input_arg);
108 23 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 17 6 23
109 6 ▶ if ($filter) { $filter->() for $value; }
Branch: 1 branch
BranchTrueFalseTotal
if ($filter) 1 5 6
110 6 ▶ if ($checker) { $checker->() or next SOURCE for $value; }
Branch: 2 branches
BranchTrueFalseTotal
if ($checker) 6 0 6
&$checker() or next SOURCE 5 1 6
111
112 5 $output{ $field_name } = $value;
113 5 ▶ if ($storer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($storer) 4 1 5
114 4 $storer->(
115 $self,
116 {
117 input => $input,
118 source => $name,
119 value => $value,
120 field_name => $field_name,
121 },
122 );
123 }
124  
125 5 next FIELD;
126 }
127  
128 7 ▶ my $default = $no_default_for{ $field_name } ? undef : $spec->{default};
Branch: 1 branch
BranchTrueFalseTotal
$no_default_for{$field_name} ? : 2 5 7
129 7 ▶ $output{ $field_name } = ref $default ? $default->() : $default;
Branch: 1 branch
BranchTrueFalseTotal
ref $default ? : 0 7 7
130 }
131  
132 4 return \%output;
133 }
134  
135 #pod =head1 FIELD DEFINITIONS
136 #pod
137 #pod Each field is defined by a hashref with the following entries:
138 #pod
139 #pod sources - an arrayref of sources; see below; required
140 #pod filter - a coderef to preprocess candidate values
141 #pod check - a coderef to validate candidate values
142 #pod store - a coderef to store accepted values
143 #pod default - a value to use if no source provides an acceptable value
144 #pod
145 #pod Sources may be given in one of two formats:
146 #pod
147 #pod [ source_name => $source, ... ]
148 #pod [ $source_1, $source_2, ... ]
149 #pod
150 #pod In the second form, sources will be assigned unique names.
151 #pod
152 #pod The source value is a coderef which, handed the C<$input> argument to the
153 #pod C<consume> method, returns a candidate value (or undef). It is also handed a
154 #pod hashref of relevant information, most importantly C<field_name>.
155 #pod
156 #pod A filter is a coderef that works by altering C<$_>.
157 #pod
158 #pod If given, check must be a coderef that inspects C<$_> and returns a true if the
159 #pod value is acceptable.
160 #pod
161 #pod Store is called if a value is accepted. It is passed the monster and a hashref
162 #pod with the following entries:
163 #pod
164 #pod value - the value accepted
165 #pod source - the name of the source from which the value was accepted
166 #pod input - the input given to the consume method
167 #pod field_name - the field name
168 #pod
169 #pod If default is given, it must be a simple scalar (in which case that is the
170 #pod default) or a coderef that will be called to provide a default value as needed.
171 #pod
172 #pod =cut
173  
174 1 "OM NOM NOM I EAT DATA";
175  
176 __END__
↑ Summary InputMonster/Util.pm »