Validation-Class
view release on metacpan or search on metacpan
lib/Validation/Class.pm view on Meta::CPAN
$proto->set_method($alias, sub {
shift @_;
$proto->$alias(@_);
});
}
# inject wrapped prototype class aliases unless exist
my @wrapped_aliases = $proto->proxy_methods_wrapped;
foreach my $alias (@wrapped_aliases) {
next if $class->can($alias);
# slight-of-hand
$proto->set_method($alias, sub {
my $self = shift @_;
$proto->$alias($self, @_);
});
}
# cache prototype
prototype_registry->add($class => $proto);
$proto; # return-once
};
}
sub configure_class_proto {
my $configuration_routine = pop;
return unless "CODE" eq ref $configuration_routine;
no strict 'refs';
my $proto = return_class_proto shift;
$configuration_routine->($proto);
return $proto;
}
sub import {
my $caller = caller(0) || caller(1);
strict->import;
warnings->import;
__PACKAGE__->export_to_level(1, @_);
return return_class_proto $caller # provision prototype when used
}
sub initialize_validator {
my $self = shift;
my $proto = $self->prototype;
my $arguments = $proto->build_args(@_);
# provision a validation class configuration
$proto->snapshot;
# override prototype attributes if requested
if (defined($arguments->{fields})) {
my $fields = delete $arguments->{fields};
$proto->fields->clear->add($fields);
}
if (defined($arguments->{params})) {
my $params = delete $arguments->{params};
$proto->params->clear->add(clone $params);
}
# process attribute assignments
my $proxy_methods = { map { $_ => 1 } ($proto->proxy_methods) } ;
while (my($name, $value) = each (%{$arguments})) {
$self->$name($value) if
$self->can($name) &&
$proto->fields->has($name) ||
$proto->attributes->has($name) || $proxy_methods->{$name}
;
}
# process builders
foreach my $builder ($proto->builders->list) {
$builder->($self, $arguments);
}
# initialize prototype
$proto->normalize($self);
# ready-set-go !!!
( run in 1.671 second using v1.01-cache-2.11-cpan-39bf76dae61 )