Data-InputMonster

 view release on metacpan or  search on metacpan

lib/Data/InputMonster.pm  view on Meta::CPAN

use strict;
use warnings;
package Data::InputMonster 0.011;
# ABSTRACT: consume data from multiple sources, best first; om nom nom!

#pod =head1 DESCRIPTION
#pod
#pod This module lets you describe a bunch of input fields you expect.  For each
#pod field, you can specify validation, a default, and multiple places to look for a
#pod value.  The first valid value found is accepted and returned in the results.
#pod
#pod =cut

use Carp ();

#pod =method new
#pod
#pod   my $monster = Data::InputMonster->new({
#pod     fields => {
#pod       field_name => \%field_spec,
#pod       ...
#pod     },
#pod   });
#pod
#pod This builds a new monster.  For more information on the C<%field_spec>
#pod parameters, see below.
#pod
#pod =cut

sub new {
  my ($class, $arg) = @_;
  
  Carp::confess("illegal parameters to Data::InputMonster constructor")
    unless $arg and (keys %$arg == 1) and exists $arg->{fields};

  my $fields = $arg->{fields};

  $class->_assert_field_spec_ok($_) for values %$fields;

  bless { fields => $fields } => $class;
}

sub _assert_field_spec_ok {
  my ($self, $spec) = @_;

  Carp::confess("illegal or missing sources")
    unless $spec->{sources} and ref $spec->{sources} eq 'ARRAY';

  Carp::confess("if given, filter must be a coderef")
    if $spec->{filter} and ref $spec->{filter} ne 'CODE';

  Carp::confess("if given, check must be a coderef")
    if $spec->{check} and ref $spec->{check} ne 'CODE';

  Carp::confess("if given, store must be a coderef")
    if $spec->{store} and ref $spec->{store} ne 'CODE';

  Carp::confess("defaults that are references must be wrapped in code")
    if ((ref $spec->{default})||'CODE') ne 'CODE';
}

#pod =method consume
#pod
#pod   my $result = $monster->consume($input, \%arg);
#pod
#pod This method processes the given input and returns a hashref of the finally
#pod accepted values.  C<$input> can be anything; it is up to the field definitions
#pod to expect and handle the data you plan to feed the monster.
#pod
#pod Valid arguments are:
#pod
#pod   no_default_for - a field name or arrayref of field names for which to NOT
#pod                    fall back to default values
#pod
#pod =cut

sub consume {
  my ($self, $input, $arg) = @_;
  $arg ||= {};

  my %no_default_for
    = (! $arg->{no_default_for})   ? ()
    : (ref $arg->{no_default_for}) ? (map {$_=>1} @{$arg->{no_default_for}})
    : ($arg->{no_default_for} => 1);

  my $field  = $self->{fields};
  my %output;

  FIELD: for my $field_name (keys %$field) {
    my $spec = $field->{$field_name};

    my $checker = $spec->{check};
    my $filter  = $spec->{filter};
    my $storer  = $spec->{store};

    my @sources = @{ $spec->{sources} };

    if (ref $sources[0]) {
      my $i = 1;
      @sources = map { ("source_" . $i++) => $_ } @sources;

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 0.584 second using v1.00-cache-2.02-grep-82fe00e-cpan-9e6bc14194b6 )