Poz/Types.pm

filter: Statementstmt 26.3% 10 / 38 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 42.8% 3 / 7 Podpod 100% 4 / 4 totaltotal 23.2% 17 / 73 CC 13 SCAR 38.7
CC
13
Cov
17%
safe_parse
90.0
new
20.0
parse
6.0
CRAP 47.7
↑ Summary Poz/Types/array.pm »
1 package Poz::Types;
2 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
7 0 my ($class, $opts) = @_;
8 0 ▶ $opts = $opts || {};
Condition0%0/2
$opts || {}
l!l
00
MC/DC0%0/1
$opts || {}
$opts
Truth table0%0/2
A
A= $opts
Aresult
11
00
9 0 my $rulecode = $class . "::rule";
10 my $self = bless {
11 0 %{$opts},
12 rules => [\&$rulecode],
13 transform => [],
14 caller => [ caller() ],
15 }, $class;
16 0 ▶ $self->{required_error} //= 'required';
Condition0%0/2
$self->{"required_error"} //= "required"
l!l
00
MC/DC0%0/1
$self->{"required_error"} //= "required"
$self->{"required_error"}
Truth table0%0/2
A
A= $self->{"required_error"}
Aresult
11
00
17 0 ▶ $self->{invalid_type_error} //= 'invalid type';
Condition0%0/2
$self->{"invalid_type_error"} //= "invalid type"
l!l
00
MC/DC0%0/1
$self->{"invalid_type_error"} //= "invalid type"
$self->{"invalid_type_error"}
Truth table0%0/2
A
A= $self->{"invalid_type_error"}
Aresult
11
00
18 0 return $self;
19 }
20  
21 0 ▶sub rule {
Subroutine rule: not called CC=1 SCAR=6.9
Pod documented
22 0 Carp::croak("Not implemented");
23 }
24  
25 0 ▶sub parse {
Subroutine parse: not called CC=2 SCAR=17.9
Pod documented
26 0 my $self = shift;
27 0 my ($ret, $err) = $self->safe_parse(@_);
28 0 ▶ Carp::croak $err if defined $err;
Branch: 1 branch
BranchTrueFalseTotal
if defined $err 0 0 0
29 0 $ret;
30 }
31  
32 0 ▶sub safe_parse {
Subroutine safe_parse: not called CC=9 SCAR=45.0
Pod documented
33 0 ▶ Carp::croak "Must handle error" unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
34  
35 0 my ($self, $value) = @_;
36 0 ▶ if (length($self->{transform}) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{"transform"} > 0) 0 0 0
37 0 for my $transformer (@{$self->{transform}}) {
38 0 $value = $transformer->($self, $value);
39 }
40 }
41 0 for my $rule (@{$self->{rules}}) {
42 0 my $err = $rule->($self, $value);
43 0 ▶ if ( (ref($err)||'') eq 'Poz::Result::ShortCircuit') {
Branch: 1 branch
BranchTrueFalseTotal
if ((ref $err || "") eq "Poz::Result::ShortCircuit") 0 0 0
Condition0%0/2
ref $err || ''
l!l
00
MC/DC0%0/1
ref $err || ''
ref $err
Truth table0%0/2
A
A= ref $err
Aresult
11
00
44 0 return;
45 }
46 0 ▶ return (undef, $err) if defined $err;
Branch: 1 branch
BranchTrueFalseTotal
if defined $err 0 0 0
47 }
48 0 ▶ if ($self->{need_coerce}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"need_coerce"}) 0 0 0
49 0 $value = $self->coerce($value);
50 }
51 0 return ($value, undef);
52 }
53  
54 11 1;
55  
56 =head1 NAME
57  
58 Poz::Types - A module for handling type validation and transformation
59  
60 =head1 SYNOPSIS
61  
62 use Poz::Types;
63  
64 my $type = Poz::Types->new({
65 required_error => 'This field is required',
66 invalid_type_error => 'Invalid type provided',
67 });
68  
69 my $result = $type->parse($value);
70  
71 =head1 DESCRIPTION
72  
73 Poz::Types is a module designed to handle type validation and transformation.
74 It provides a flexible way to define rules and transformations for different types.
75  
76 =head1 METHODS
77  
78 =head2 new
79  
80 my $type = Poz::Types->new(\%options);
81  
82 Creates a new Poz::Types object. The optional hash reference can contain the following keys:
83 - required_error: Custom error message for required fields.
84 - invalid_type_error: Custom error message for invalid types.
85  
86 =head2 rule
87  
88 $type->rule();
89  
90 This method should be overridden in subclasses to provide specific validation rules.
91 By default, it throws a "Not implemented" error.
92  
93 =head2 parse
94  
95 my $result = $type->parse($value);
96  
97 Parses the given value according to the defined rules and transformations.
98 Returns the transformed value or an error if validation fails.
99  
100 =head2 safe_parse
101  
102 my ($result, $error) = $type->safe_parse($value);
103  
104 Parses the given value and returns the transformed value.
105 If succeeds, returns a tuple of the transformed value and undef.
106 If fails, returns a tuple of undef and an error message.
107  
108 =head1 LICENSE
109  
110 Copyright (C) ytnobody.
111  
112 This library is free software; you can redistribute it and/or modify
113 it under the same terms as Perl itself.
114  
115 =head1 AUTHOR
116  
117 ytnobody E<lt>ytnobody@gmail.comE<gt>
118  
119 =cut
↑ Summary Poz/Types/array.pm »