| 1 | 406 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | package HTML::FormFu::Role::Element::Coercible; | ||||||||||||||||||||||||||||
| 4 | # ABSTRACT: coercible element role | ||||||||||||||||||||||||||||
| 5 | 406 | $HTML::FormFu::Role::Element::Coercible::VERSION = '2.09'; | |||||||||||||||||||||||||||
| 6 | 406 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 406 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 406 | ▶ | use HTML::FormFu::Util qw( require_class ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | sub as { | ||||||||||||||||||||||||||
|
Subroutine
as: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 12 | 2 | my ( $self, $type, %attrs ) = @_; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 2 | return $self->_coerce( | |||||||||||||||||||||||||||
| 15 | type => $type, | ||||||||||||||||||||||||||||
| 16 | attributes => \%attrs, | ||||||||||||||||||||||||||||
| 17 | errors => $self->_errors, | ||||||||||||||||||||||||||||
| 18 | package => __PACKAGE__, | ||||||||||||||||||||||||||||
| 19 | ); | ||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 2 | ▶ | sub _coerce { | ||||||||||||||||||||||||||
|
Subroutine
_coerce: called CC=7 SCAR=20.1
| |||||||||||||||||||||||||||||
| 23 | 2 | my ( $self, %args ) = @_; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 2 | for (qw( type attributes package )) { | |||||||||||||||||||||||||||
| 26 | 6 | ▶ | croak "$_ argument required" if !defined $args{$_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 2 | my $class = $args{type}; | |||||||||||||||||||||||||||
| 30 | 2 | ▶ | if ( $class !~ m/^\+/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 2 | $class = "HTML::FormFu::Element::$class"; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 2 | require_class($class); | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 2 | my $element = $class->new( { type => $args{type}, } ); | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 2 | for my $method ( qw( | |||||||||||||||||||||||||||
| 39 | name | ||||||||||||||||||||||||||||
| 40 | attributes comment | ||||||||||||||||||||||||||||
| 41 | comment_attributes label | ||||||||||||||||||||||||||||
| 42 | label_attributes label_filename | ||||||||||||||||||||||||||||
| 43 | render_method parent | ||||||||||||||||||||||||||||
| 44 | ) ) | ||||||||||||||||||||||||||||
| 45 | { | ||||||||||||||||||||||||||||
| 46 | 18 | $element->$method( $self->$method ); | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 2 | _coerce_processors_and_errors( $self, $element, %args ); | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 2 | $element->attributes( $args{attributes} ); | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | croak "element cannot be coerced to type '$args{type}'" | ||||||||||||||||||||||||||||
| 54 | unless $element->isa( $args{package} ) | ||||||||||||||||||||||||||||
| 55 | 2 | ▶ | || $element->does( $args{package} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$element->isa($args{"package"}) or $element->does($args{"package"})
MC/DC0%0/2
$element->isa($args{"package"}) or $element->does($args{"package"})
$element->isa($args{"package"})
$element->does($args{"package"})
Truth table33%1/3
A or B
A= $element->isa($args{"package"})B= $element->does($args{"package"})
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 2 | $element->value( $self->value ); | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 2 | return $element; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 2 | ▶ | sub _coerce_processors_and_errors { | ||||||||||||||||||||||||||
|
Subroutine
_coerce_processors_and_errors: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 63 | 2 | my ( $self, $element, %args ) = @_; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 2 | ▶ | if ( $args{errors} && @{ $args{errors} } > 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$args{"errors"} and @{$args{"errors"};} > 0
MC/DC50%1/2
$args{"errors"} and @{$args{"errors"};} > 0
$args{"errors"}
@{$args{"errors"};} > 0
Truth table66%2/3
A and B
A= $args{"errors"}B= @{$args{"errors"};} > 0
| |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 1 | my @errors = @{ $args{errors} }; | |||||||||||||||||||||||||||
| 68 | 1 | my @new_errors; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 1 | for my $list ( qw( | |||||||||||||||||||||||||||
| 71 | _filters _constraints | ||||||||||||||||||||||||||||
| 72 | _inflators _validators | ||||||||||||||||||||||||||||
| 73 | _transformers _deflators | ||||||||||||||||||||||||||||
| 74 | ) ) | ||||||||||||||||||||||||||||
| 75 | { | ||||||||||||||||||||||||||||
| 76 | 6 | $element->$list( [] ); | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 6 | for my $processor ( @{ $self->$list } ) { | |||||||||||||||||||||||||||
| 79 | 1 | my @errors_to_copy = map { $_->clone } | |||||||||||||||||||||||||||
| 80 | 1 | grep { $_->processor == $processor } @errors; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 1 | my $processor_clone = $processor->clone; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 1 | $processor_clone->parent($element); | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 1 | map { $_->processor($processor_clone) } @errors_to_copy; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 1 | push @{ $element->$list }, $processor_clone; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 1 | push @new_errors, @errors_to_copy; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | 1 | $element->_errors( \@new_errors ); | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | else { | ||||||||||||||||||||||||||||
| 96 | 1 | $element->_errors( [] ); | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 2 | return; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 406 | 1; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | __END__ | ||||||||||||||||||||||||||||