| 1 | package Type::Tiny::ConstrainedObject; | ||||||||||||
| 2 | |||||||||||||
| 3 | 45 | ▶ | use 5.008001; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 45 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 45 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 45 | ▶ | BEGIN { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 45 | $Type::Tiny::ConstrainedObject::AUTHORITY = 'cpan:TOBYINK'; | |||||||||||
| 9 | 45 | $Type::Tiny::ConstrainedObject::VERSION = '2.010001'; | |||||||||||
| 10 | } | ||||||||||||
| 11 | |||||||||||||
| 12 | 45 | $Type::Tiny::ConstrainedObject::VERSION =~ tr/_//d; | |||||||||||
| 13 | |||||||||||||
| 14 | 9 | ▶ | sub _croak ($;@) { require Error::TypeTiny; goto \&Error::TypeTiny::croak } | ||||||||||
|
Subroutine
_croak: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | |||||||||||||
| 16 | 45 | ▶ | use Type::Tiny (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 17 | 45 | our @ISA = 'Type::Tiny'; | |||||||||||
| 18 | |||||||||||||
| 19 | 45 | my %errlabel = ( | |||||||||||
| 20 | parent => 'a parent', | ||||||||||||
| 21 | constraint => 'a constraint coderef', | ||||||||||||
| 22 | inlined => 'an inlining coderef', | ||||||||||||
| 23 | ); | ||||||||||||
| 24 | |||||||||||||
| 25 | 282 | ▶ | sub new { | ||||||||||
|
Subroutine
new: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||||
| 26 | 282 | my $proto = shift; | |||||||||||
| 27 | 0 | ▶ | my %opts = ( @_ == 1 ) ? %{ $_[0] } : @_; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 28 | 282 | for my $key ( qw/ parent constraint inlined / ) { | |||||||||||
| 29 | 837 | ▶ | next unless exists $opts{$key}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 30 | _croak( | ||||||||||||
| 31 | '%s type constraints cannot have %s passed to the constructor', | ||||||||||||
| 32 | $proto->_short_name, | ||||||||||||
| 33 | 9 | $errlabel{$key}, | |||||||||||
| 34 | ); | ||||||||||||
| 35 | } | ||||||||||||
| 36 | 273 | $proto->SUPER::new( %opts ); | |||||||||||
| 37 | } #/ sub new | ||||||||||||
| 38 | |||||||||||||
| 39 | 7648 | ▶ | sub has_parent { | ||||||||||
|
Subroutine
has_parent: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 40 | 7648 | !!1; | |||||||||||
| 41 | } | ||||||||||||
| 42 | |||||||||||||
| 43 | 244 | ▶ | sub parent { | ||||||||||
|
Subroutine
parent: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 44 | 244 | require Types::Standard; | |||||||||||
| 45 | 244 | Types::Standard::Object(); | |||||||||||
| 46 | } | ||||||||||||
| 47 | |||||||||||||
| 48 | 0 | ▶ | sub _short_name { | ||||||||||
|
Subroutine
_short_name: not called CC=1 SCAR=0.0
| |||||||||||||
| 49 | 0 | die "subclasses must implement this"; # uncoverable statement | |||||||||||
| 50 | } | ||||||||||||
| 51 | |||||||||||||
| 52 | 45 | my $i = 0; | |||||||||||
| 53 | 55 | ▶ | my $_where_expressions = sub { | ||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=21.1
| |||||||||||||
| 54 | 55 | my $self = shift; | |||||||||||
| 55 | 55 | my $name = shift; | |||||||||||
| 56 | 55 | ▶ | $name ||= "where expression check"; | ||||||||||
|
Condition50%1/2
$name ||= "where expression check"
MC/DC0%0/1
$name ||= "where expression check"
$name
Truth table50%1/2
A
A= $name
| |||||||||||||
| 57 | 55 | my ( %env, @codes ); | |||||||||||
| 58 | 55 | ▶ | while ( @_ ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 59 | 79 | my $expr = shift; | |||||||||||
| 60 | 79 | my $constraint = shift; | |||||||||||
| 61 | 79 | ▶ | if ( !ref $constraint ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 62 | 22 | push @codes, sprintf( 'do { local $_ = %s; %s }', $expr, $constraint ); | |||||||||||
| 63 | } | ||||||||||||
| 64 | else { | ||||||||||||
| 65 | 57 | require Types::Standard; | |||||||||||
| 66 | 57 | ▶ | my $type = | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 67 | Types::Standard::is_RegexpRef( $constraint ) | ||||||||||||
| 68 | ? Types::Standard::StrMatch()->of( $constraint ) | ||||||||||||
| 69 | : Types::TypeTiny::to_TypeTiny( $constraint ); | ||||||||||||
| 70 | 57 | ▶ | if ( $type->can_be_inlined ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 71 | 57 | push @codes, | |||||||||||
| 72 | sprintf( | ||||||||||||
| 73 | 'do { my $tmp = %s; %s }', $expr, | ||||||||||||
| 74 | $type->inline_check( '$tmp' ) | ||||||||||||
| 75 | ); | ||||||||||||
| 76 | } | ||||||||||||
| 77 | else { | ||||||||||||
| 78 | 0 | ++$i; | |||||||||||
| 79 | 0 | $env{ '$chk' . $i } = do { my $chk = $type->compiled_check; \$chk }; | |||||||||||
| 80 | 0 | push @codes, sprintf( '$chk%d->(%s)', $i, $expr ); | |||||||||||
| 81 | } | ||||||||||||
| 82 | } #/ else [ if ( !ref $constraint )] | ||||||||||||
| 83 | } #/ while ( @_ ) | ||||||||||||
| 84 | |||||||||||||
| 85 | 55 | ▶ | if ( keys %env ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 86 | |||||||||||||
| 87 | # cannot inline | ||||||||||||
| 88 | 0 | my $sub = Eval::TypeTiny::eval_closure( | |||||||||||
| 89 | source => | ||||||||||||
| 90 | sprintf( 'sub ($) { local $_ = shift; %s }', join( q( and ), @codes ) ), | ||||||||||||
| 91 | description => sprintf( '%s for %s', $name, $self->name ), | ||||||||||||
| 92 | environment => \%env, | ||||||||||||
| 93 | ); | ||||||||||||
| 94 | 0 | return $self->where( $sub ); | |||||||||||
| 95 | } #/ if ( keys %env ) | ||||||||||||
| 96 | else { | ||||||||||||
| 97 | 55 | return $self->where( join( q( and ), @codes ) ); | |||||||||||
| 98 | } | ||||||||||||
| 99 | 45 | }; | |||||||||||
| 100 | |||||||||||||
| 101 | 16 | ▶ | sub stringifies_to { | ||||||||||
|
Subroutine
stringifies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 102 | 16 | my $self = shift; | |||||||||||
| 103 | 16 | my ( $constraint ) = @_; | |||||||||||
| 104 | 16 | $self->$_where_expressions( "stringification check", q{"$_"}, $constraint ); | |||||||||||
| 105 | } | ||||||||||||
| 106 | |||||||||||||
| 107 | 15 | ▶ | sub numifies_to { | ||||||||||
|
Subroutine
numifies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 108 | 15 | my $self = shift; | |||||||||||
| 109 | 15 | my ( $constraint ) = @_; | |||||||||||
| 110 | 15 | $self->$_where_expressions( "numification check", q{0+$_}, $constraint ); | |||||||||||
| 111 | } | ||||||||||||
| 112 | |||||||||||||
| 113 | 24 | ▶ | sub with_attribute_values { | ||||||||||
|
Subroutine
with_attribute_values: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 114 | 24 | my $self = shift; | |||||||||||
| 115 | 24 | my %constraint = @_; | |||||||||||
| 116 | $self->$_where_expressions( | ||||||||||||
| 117 | "attributes check", | ||||||||||||
| 118 | 24 | map { my $attr = $_; qq{\$_->$attr} => $constraint{$attr} } | |||||||||||
| 119 | sort keys %constraint, | ||||||||||||
| 120 | ); | ||||||||||||
| 121 | } | ||||||||||||
| 122 | |||||||||||||
| 123 | 45 | 1; | |||||||||||
| 124 | |||||||||||||
| 125 | __END__ | ||||||||||||