| 1 | package Workflow::InputField; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 30 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 30 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 30 | ▶ | use v5.14.0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 30 | ▶ | use parent qw( Class::Accessor ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 30 | ▶ | use Log::Any; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 30 | ▶ | use Module::Runtime qw( require_module ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 30 | ▶ | use Workflow::Exception qw( configuration_error ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 30 | ▶ | use Syntax::Keyword::Try; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 30 | $Workflow::InputField::VERSION = '2.12'; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 30 | my @PROPS = qw( name label description type requirement | |||||||||||||||||||||||||||
| 15 | source_class source_list class ); | ||||||||||||||||||||||||||||
| 16 | 30 | __PACKAGE__->mk_accessors(@PROPS); | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 30 | my %INCLUDED = (); | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 75 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=14 SCAR=26.4
Pod
documented
| |||||||||||||||||||||||||||||
| 21 | 75 | my ( $class, $params ) = @_; | |||||||||||||||||||||||||||
| 22 | 75 | my $log = Log::Any->get_logger( category => $class ); | |||||||||||||||||||||||||||
| 23 | $log->debug("Instantiating new field '$params->{name}'") | ||||||||||||||||||||||||||||
| 24 | 75 | ▶ | if $params->{name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 75 | my $self = bless {}, $class; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | # Set all our parameters | ||||||||||||||||||||||||||||
| 29 | 75 | foreach my $prop (@PROPS) { | |||||||||||||||||||||||||||
| 30 | 600 | ▶ | next unless ( $params->{$prop} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 236 | $self->$prop( $params->{$prop} ); | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | # ...ensure our name is defined | ||||||||||||||||||||||||||||
| 35 | 75 | ▶ | unless ( $self->name ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | my $id_string = '[' | ||||||||||||||||||||||||||||
| 37 | . join( | ||||||||||||||||||||||||||||
| 38 | 0 | '] [', map {"$_: $params->{$_}"} | |||||||||||||||||||||||||||
| 39 | 1 | sort keys %{$params} | |||||||||||||||||||||||||||
| 40 | ) . ']'; | ||||||||||||||||||||||||||||
| 41 | 1 | configuration_error "Field found without name: $id_string"; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 74 | my $name = $self->name; | |||||||||||||||||||||||||||
| 45 | 74 | ▶ | unless ( $self->label ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 9 | $self->label($name); | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | my $requirement = ( defined $params->{is_required} | ||||||||||||||||||||||||||||
| 49 | 74 | ▶ | && $params->{is_required} eq 'yes' ) ? 'required' : 'optional'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $params->{"is_required"} and $params->{"is_required"} eq "yes"
MC/DC100%2/2
defined $params->{"is_required"} and $params->{"is_required"} eq "yes"
defined $params->{"is_required"}
$params->{"is_required"} eq "yes"
Truth table100%3/3
A and B
A= defined $params->{"is_required"}B= $params->{"is_required"} eq "yes"
| |||||||||||||||||||||||||||||
| 50 | 74 | $self->requirement($requirement); | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | # ...ensure a class associated with the input source exists | ||||||||||||||||||||||||||||
| 53 | 74 | ▶ | if ( my $source_class = $self->source_class ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 54 | 13 | $log->debug("Possible values for '$name' from '$source_class'"); | |||||||||||||||||||||||||||
| 55 | 13 | ▶ | unless ( $INCLUDED{$source_class} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | try { | ||||||||||||||||||||||||||||
| 57 | 6 | require_module( $source_class ); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | 6 | catch ($error) { | |||||||||||||||||||||||||||
| 60 | 0 | configuration_error "Failed to include source class ", | |||||||||||||||||||||||||||
| 61 | "'$source_class' used in field '$name': $error"; | ||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | 6 | $INCLUDED{$source_class}++; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | 13 | $params->{values} = [ $source_class->get_possible_values($self) ]; | |||||||||||||||||||||||||||
| 66 | } elsif ( $self->source_list ) { | ||||||||||||||||||||||||||||
| 67 | 13 | $log->debug("Possible values for '$name' specified in config"); | |||||||||||||||||||||||||||
| 68 | 13 | $params->{values} = [ split /\s*,\s*/, $self->source_list ]; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 74 | ▶ | my $values = $params->{values} || $params->{possible_values}; | ||||||||||||||||||||||||||
|
Condition66%2/3
$params->{'values'} || $params->{'possible_values'}
MC/DC50%1/2
$params->{'values'} || $params->{'possible_values'}
$params->{'values'}
$params->{'possible_values'}
Truth table66%2/3
A || B
A= $params->{'values'}B= $params->{'possible_values'}
| |||||||||||||||||||||||||||||
| 72 | 74 | ▶ | if ($values) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 26 | ▶ | my @add_values = ( ref $values eq 'ARRAY' ) ? @{$values} : ($values); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 26 | $log->debug( "Values to use as source for field '$name': ", | |||||||||||||||||||||||||||
| 75 | join ', ', @add_values ); | ||||||||||||||||||||||||||||
| 76 | 26 | $self->add_possible_values(@add_values); | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # Assign the default field type, subclasses may override... | ||||||||||||||||||||||||||||
| 80 | 74 | $self->type('basic'); | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 74 | $self->init($params); | |||||||||||||||||||||||||||
| 83 | 74 | return $self; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 74 | ▶ | sub init {return} | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 3 | ▶ | sub is_required { | ||||||||||||||||||||||||||
|
Subroutine
is_required: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 89 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||
| 90 | 3 | ▶ | return ( $self->requirement eq 'required' ) ? 'yes' : 'no'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 3 | ▶ | sub is_optional { | ||||||||||||||||||||||||||
|
Subroutine
is_optional: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 94 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||
| 95 | 3 | ▶ | return ( $self->requirement eq 'optional' ) ? 'yes' : 'no'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 2 | ▶ | sub get_possible_values { | ||||||||||||||||||||||||||
|
Subroutine
get_possible_values: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 99 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||
| 100 | 2 | ▶ | $self->{_enumerated} ||= []; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"_enumerated"} ||= []
MC/DC100%1/1
$self->{"_enumerated"} ||= []
$self->{"_enumerated"}
Truth table100%2/2
A
A= $self->{"_enumerated"}
| |||||||||||||||||||||||||||||
| 101 | 2 | return @{ $self->{_enumerated} }; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 27 | ▶ | sub add_possible_values { | ||||||||||||||||||||||||||
|
Subroutine
add_possible_values: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 105 | 27 | my ( $self, @values ) = @_; | |||||||||||||||||||||||||||
| 106 | 27 | foreach my $value (@values) { | |||||||||||||||||||||||||||
| 107 | 132 | ▶ | my $this_value | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | = ( ref $value eq 'HASH' ) | ||||||||||||||||||||||||||||
| 109 | ? $value | ||||||||||||||||||||||||||||
| 110 | : { label => $value, value => $value }; | ||||||||||||||||||||||||||||
| 111 | 132 | push @{ $self->{_enumerated} }, $this_value; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | 27 | return @{ $self->{_enumerated} }; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 30 | 1; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | __END__ | ||||||||||||||||||||||||||||