| 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
Condition33%2/6
$arg and keys %$arg == 1 and exists $arg->{"fields"}
$arg and keys %$arg == 1
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
$spec->{"sources"} and ref $spec->{"sources"} eq "ARRAY"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$spec->{"filter"} and ref $spec->{"filter"} ne "CODE"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
$spec->{"check"} and ref $spec->{"check"} ne "CODE"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$spec->{"store"} and ref $spec->{"store"} ne "CODE"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition50%1/2
ref $spec->{'default'} || 'CODE'
MC/DC0%0/1
ref $spec->{'default'} || 'CODE'
ref $spec->{'default'}
Truth table50%1/2
A
A= ref $spec->{'default'}
| |||||||||||||||||||||||||||||||||||||||||||
| 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 ||= {}
MC/DC100%1/1
$arg ||= {}
$arg
Truth table100%2/2
A
A= $arg
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | 6 | ▶ | if ($filter) { $filter->() for $value; } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | 6 | ▶ | if ($checker) { $checker->() or next SOURCE for $value; } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | 5 | $output{ $field_name } = $value; | |||||||||||||||||||||||||||||||||||||||||
| 113 | 5 | ▶ | if ($storer) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | 7 | ▶ | $output{ $field_name } = ref $default ? $default->() : $default; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||