| 1 | #!perl | ||||||||||||||||||||||||||||||||
| 2 | #PODNAME: Raisin::Param | ||||||||||||||||||||||||||||||||
| 3 | #ABSTRACT: Parameter class for Raisin | ||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||
| 5 | 20 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 20 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | package Raisin::Param; | ||||||||||||||||||||||||||||||||
| 9 | 20 | $Raisin::Param::VERSION = '0.94'; | |||||||||||||||||||||||||||||||
| 10 | 20 | ▶ | use Carp; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | 20 | ▶ | use Plack::Util::Accessor qw( | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | named | ||||||||||||||||||||||||||||||||
| 13 | required | ||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | default | ||||||||||||||||||||||||||||||||
| 16 | desc | ||||||||||||||||||||||||||||||||
| 17 | enclosed | ||||||||||||||||||||||||||||||||
| 18 | name | ||||||||||||||||||||||||||||||||
| 19 | regex | ||||||||||||||||||||||||||||||||
| 20 | type | ||||||||||||||||||||||||||||||||
| 21 | coerce | ||||||||||||||||||||||||||||||||
| 22 | 20 | ); | |||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||
| 24 | 20 | ▶ | use Raisin::Util; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | 20 | my @ATTRIBUTES = qw(name type default regex desc coerce); | |||||||||||||||||||||||||||||||
| 27 | 20 | my @LOCATIONS = qw(path formData body header query); | |||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | 160 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 30 | 160 | my ($class, %args) = @_; | |||||||||||||||||||||||||||||||
| 31 | 160 | my $self = bless {}, $class; | |||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||
| 33 | 160 | ▶ | $self->{named} = $args{named} || 0; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$args{'named'} || 0
MC/DC100%1/1
$args{'named'} || 0
$args{'named'}
Truth table100%2/2
A
A= $args{'named'}
| |||||||||||||||||||||||||||||||||
| 34 | 160 | ▶ | $self->{required} = $args{type} =~ /^require(s|d)$/ ? 1 : 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||
| 36 | 160 | ▶ | return unless $self->_parse($args{spec}); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||
| 38 | 159 | $self; | |||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||
| 41 | 160 | ▶ | sub _parse { | ||||||||||||||||||||||||||||||
|
Subroutine
_parse: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||
| 42 | 160 | my ($self, $spec) = @_; | |||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | 160 | $self->{$_} = $spec->{$_} for @ATTRIBUTES; | |||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||
| 46 | 160 | ▶ | if ($spec->{in}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 47 | 8 | ▶ | return unless $self->in($spec->{in}); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||
| 50 | 159 | ▶ | if ($spec->{encloses}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 51 | 30 | ▶ | if ($self->type->name eq 'HashRef') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 52 | 28 | $self->{enclosed} = _compile_enclosed($spec->{encloses}); | |||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||
| 54 | else { | ||||||||||||||||||||||||||||||||
| 55 | 2 | Raisin::log( | |||||||||||||||||||||||||||||||
| 56 | warn => 'Ignoring enclosed parameters for `%s`, type should be `HashRef` not `%s`', | ||||||||||||||||||||||||||||||||
| 57 | $self->name, $self->type->name | ||||||||||||||||||||||||||||||||
| 58 | ); | ||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||
| 62 | 159 | ▶ | $self->{coerce} = defined($spec->{coerce}) ? $spec->{coerce} : 1; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||
| 64 | 159 | return 1; | |||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||
| 67 | 28 | ▶ | sub _compile_enclosed { | ||||||||||||||||||||||||||||||
|
Subroutine
_compile_enclosed: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 68 | 28 | my $params = shift; | |||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||
| 70 | 28 | my @enclosed; | |||||||||||||||||||||||||||||||
| 71 | 28 | my $next_param = Raisin::Util::iterate_params($params); | |||||||||||||||||||||||||||||||
| 72 | 28 | ▶ | while (my ($type, $spec) = $next_param->()) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 73 | 84 | ▶ | last unless $type; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | 56 | push @enclosed, Raisin::Param->new( | |||||||||||||||||||||||||||||||
| 76 | named => 0, | ||||||||||||||||||||||||||||||||
| 77 | type => $type, # -> requires/optional | ||||||||||||||||||||||||||||||||
| 78 | spec => $spec, # -> { name => ..., type => ... } | ||||||||||||||||||||||||||||||||
| 79 | ); | ||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||
| 82 | 28 | \@enclosed; | |||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||
| 85 | 16 | ▶ | sub display_name { shift->name } | ||||||||||||||||||||||||||||||
|
Subroutine
display_name: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||
| 87 | 58 | ▶ | sub in { | ||||||||||||||||||||||||||||||
|
Subroutine
in: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 88 | 58 | my ($self, $value) = @_; | |||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||
| 90 | 58 | ▶ | if (defined $value) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 91 | 8 | ▶ | unless (grep { $value eq $_ } @LOCATIONS) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 92 | 1 | Raisin::log(warn => '`%s` should be one of: %s', | |||||||||||||||||||||||||||||||
| 93 | $self->name, join ', ', @LOCATIONS); | ||||||||||||||||||||||||||||||||
| 94 | 1 | return; | |||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||
| 97 | 7 | $self->{in} = $value; | |||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||
| 100 | 57 | $self->{in}; | |||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||
| 103 | 128 | ▶ | sub validate { | ||||||||||||||||||||||||||||||
|
Subroutine
validate: called CC=19 SCAR=29.7
Pod
documented
| |||||||||||||||||||||||||||||||||
| 104 | 128 | my ($self, $ref_value, $quiet) = @_; | |||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||
| 106 | # Required and empty | ||||||||||||||||||||||||||||||||
| 107 | # Only optional parameters can have default value | ||||||||||||||||||||||||||||||||
| 108 | 128 | ▶ | if ($self->required && !defined($$ref_value)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->required and not defined $$ref_value
MC/DC100%2/2
$self->required and not defined $$ref_value
$self->required
not defined $$ref_value
Truth table100%3/3
A and not(B)
A= $self->requiredB= not defined $$ref_value
| |||||||||||||||||||||||||||||||||
| 109 | 3 | ▶ | Raisin::log(warn => '`%s` is required', $self->name) unless $quiet; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 110 | 3 | return; | |||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||
| 113 | # Optional and empty | ||||||||||||||||||||||||||||||||
| 114 | 125 | ▶ | if (!$self->required && !defined($$ref_value)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->required or defined $$ref_value
MC/DC100%2/2
$self->required or defined $$ref_value
$self->required
defined $$ref_value
Truth table100%3/3
A or B
A= $self->requiredB= defined $$ref_value
| |||||||||||||||||||||||||||||||||
| 115 | 21 | Raisin::log(info => '`%s` optional and empty', $self->name); | |||||||||||||||||||||||||||||||
| 116 | 21 | return 1; | |||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||
| 119 | # Type check | ||||||||||||||||||||||||||||||||
| 120 | 104 | eval { | |||||||||||||||||||||||||||||||
| 121 | 104 | ▶ | if ($self->type->has_coercion && $self->coerce) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->type->has_coercion and $self->coerce
MC/DC50%1/2
$self->type->has_coercion and $self->coerce
$self->type->has_coercion
$self->coerce
Truth table66%2/3
A and B
A= $self->type->has_coercionB= $self->coerce
| |||||||||||||||||||||||||||||||||
| 122 | 1 | $$ref_value = $self->type->coerce($$ref_value); | |||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||
| 125 | 104 | ▶ | if ($self->type->isa('Moose::Meta::TypeConstraint')) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 126 | 0 | $self->type->assert_valid($$ref_value); | |||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||
| 128 | else { | ||||||||||||||||||||||||||||||||
| 129 | 104 | $$ref_value = $self->type->($$ref_value); | |||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||
| 131 | }; | ||||||||||||||||||||||||||||||||
| 132 | 104 | ▶ | if (my $e = $@) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 133 | 6 | ▶ | unless ($quiet) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 134 | 5 | Raisin::log(warn => 'Param `%s` didn\'t pass constraint `%s` with value "%s"', | |||||||||||||||||||||||||||||||
| 135 | $self->name, $self->type->name, $$ref_value); | ||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||
| 137 | 6 | return; | |||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||
| 140 | # Nested | ||||||||||||||||||||||||||||||||
| 141 | 98 | ▶ | if ($self->type->name eq 'HashRef' && $self->enclosed) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$self->type->name eq "HashRef" and $self->enclosed
MC/DC50%1/2
$self->type->name eq "HashRef" and $self->enclosed
$self->type->name eq "HashRef"
$self->enclosed
Truth table66%2/3
A and B
A= $self->type->name eq "HashRef"B= $self->enclosed
| |||||||||||||||||||||||||||||||||
| 142 | 11 | for my $p (@{ $self->enclosed }) { | |||||||||||||||||||||||||||||||
| 143 | 19 | my $v = $$ref_value; | |||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||
| 145 | 19 | ▶ | if ($self->type->name eq 'HashRef') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 146 | 19 | $v = $v->{ $p->name }; | |||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||
| 149 | 19 | ▶ | return unless $p->validate(\$v, $quiet); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||
| 152 | # Regex | ||||||||||||||||||||||||||||||||
| 153 | ▶ | elsif ($self->regex && $$ref_value !~ $self->regex) { | |||||||||||||||||||||||||||||||
|
Condition100%3/3
$self->regex and not $$ref_value =~ $self->regex
MC/DC100%2/2
$self->regex and not $$ref_value =~ $self->regex
$self->regex
not $$ref_value =~ $self->regex
Truth table100%3/3
A and not(B)
A= $self->regexB= not $$ref_value =~ $self->regex
| |||||||||||||||||||||||||||||||||
| 154 | 3 | ▶ | unless ($quiet) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 155 | 3 | Raisin::log(warn => 'Param `%s` didn\'t match regex `%s` with value "%s"', | |||||||||||||||||||||||||||||||
| 156 | $self->name, $self->regex, $$ref_value); | ||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||
| 158 | 3 | return; | |||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||
| 161 | 91 | 1; | |||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||
| 164 | 20 | 1; | |||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||
| 166 | __END__ | ||||||||||||||||||||||||||||||||