| 1 | 380 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | package HTML::FormFu::Role::Element::Coercible; | ||||||||||||||||||||||||||||
| 4 | 380 | $HTML::FormFu::Role::Element::Coercible::VERSION = '2.06'; | |||||||||||||||||||||||||||
| 5 | 380 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 380 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 380 | ▶ | use HTML::FormFu::Util qw( require_class ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | sub as { | ||||||||||||||||||||||||||||
| 11 | 2 | ▶ | my ( $self, $type, %attrs ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
as: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 2 | return $self->_coerce( | |||||||||||||||||||||||||||
| 14 | type => $type, | ||||||||||||||||||||||||||||
| 15 | attributes => \%attrs, | ||||||||||||||||||||||||||||
| 16 | errors => $self->_errors, | ||||||||||||||||||||||||||||
| 17 | package => __PACKAGE__, | ||||||||||||||||||||||||||||
| 18 | ); | ||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | sub _coerce { | ||||||||||||||||||||||||||||
| 22 | 2 | ▶ | my ( $self, %args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_coerce: called CC=7 SCAR=20.1
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 2 | for (qw( type attributes package )) { | |||||||||||||||||||||||||||
| 25 | 6 | ▶ | croak "$_ argument required" if !defined $args{$_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 2 | my $class = $args{type}; | |||||||||||||||||||||||||||
| 29 | 2 | ▶ | if ( $class !~ m/^\+/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 2 | $class = "HTML::FormFu::Element::$class"; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 2 | require_class($class); | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 2 | my $element = $class->new( { type => $args{type}, } ); | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 2 | for my $method ( qw( | |||||||||||||||||||||||||||
| 38 | name | ||||||||||||||||||||||||||||
| 39 | attributes comment | ||||||||||||||||||||||||||||
| 40 | comment_attributes label | ||||||||||||||||||||||||||||
| 41 | label_attributes label_filename | ||||||||||||||||||||||||||||
| 42 | render_method parent | ||||||||||||||||||||||||||||
| 43 | ) ) | ||||||||||||||||||||||||||||
| 44 | { | ||||||||||||||||||||||||||||
| 45 | 18 | $element->$method( $self->$method ); | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 2 | _coerce_processors_and_errors( $self, $element, %args ); | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 2 | $element->attributes( $args{attributes} ); | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | croak "element cannot be coerced to type '$args{type}'" | ||||||||||||||||||||||||||||
| 53 | unless $element->isa( $args{package} ) | ||||||||||||||||||||||||||||
| 54 | 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"})
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 2 | $element->value( $self->value ); | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 2 | return $element; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | sub _coerce_processors_and_errors { | ||||||||||||||||||||||||||||
| 62 | 2 | ▶ | my ( $self, $element, %args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_coerce_processors_and_errors: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 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
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 1 | my @errors = @{ $args{errors} }; | |||||||||||||||||||||||||||
| 67 | 1 | my @new_errors; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 1 | for my $list ( qw( | |||||||||||||||||||||||||||
| 70 | _filters _constraints | ||||||||||||||||||||||||||||
| 71 | _inflators _validators | ||||||||||||||||||||||||||||
| 72 | _transformers _deflators | ||||||||||||||||||||||||||||
| 73 | ) ) | ||||||||||||||||||||||||||||
| 74 | { | ||||||||||||||||||||||||||||
| 75 | 6 | $element->$list( [] ); | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 6 | for my $processor ( @{ $self->$list } ) { | |||||||||||||||||||||||||||
| 78 | 1 | my @errors_to_copy = map { $_->clone } | |||||||||||||||||||||||||||
| 79 | 1 | grep { $_->processor == $processor } @errors; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 1 | my $processor_clone = $processor->clone; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 1 | $processor_clone->parent($element); | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 1 | map { $_->processor($processor_clone) } @errors_to_copy; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 1 | push @{ $element->$list }, $processor_clone; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 1 | push @new_errors, @errors_to_copy; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | 1 | $element->_errors( \@new_errors ); | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | else { | ||||||||||||||||||||||||||||
| 95 | 1 | $element->_errors( [] ); | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 2 | return; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 380 | 1; | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | __END__ | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | =pod | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | =head1 NAME | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | HTML::FormFu::Role::Element::Coercible | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | =head1 VERSION | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | version 2.06 | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | Carl Franks <cpan@fireartist.com> | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | This software is copyright (c) 2018 by Carl Franks. | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 126 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | =cut | ||||||||||||||||||||||||||||