File Coverage

blib/lib/Specio/Constraint/Intersection.pm
Criterion Covered Total %
statement 59 59 100.0
branch 2 2 100.0
condition n/a
subroutine 22 22 100.0
pod 0 1 0.0
total 83 84 98.8


line stmt bran cond sub pod time code
1             package Specio::Constraint::Intersection;
2              
3 2     2   1298 use strict;
  2         5  
  2         71  
4 2     2   10 use warnings;
  2         4  
  2         159  
5              
6             our $VERSION = '0.53';
7              
8 2     2   12 use List::Util 1.33 qw( all );
  2         37  
  2         389  
9 2     2   13 use Role::Tiny::With;
  2         5  
  2         120  
10 2     2   11 use Specio qw( _clone );
  2         3  
  2         90  
11 2     2   10 use Specio::OO;
  2         5  
  2         120  
12              
13 2     2   11 use Specio::Constraint::Role::Interface;
  2         14  
  2         1512  
14             with 'Specio::Constraint::Role::Interface';
15              
16             {
17             ## no critic (Subroutines::ProtectPrivateSubs)
18             my $attrs = _clone( Specio::Constraint::Role::Interface::_attrs() );
19             ## use critic
20              
21             for my $name (qw( _constraint _inline_generator )) {
22             delete $attrs->{$name}{predicate};
23             $attrs->{$name}{init_arg} = undef;
24             $attrs->{$name}{lazy} = 1;
25             $attrs->{$name}{builder}
26             = $name =~ /^_/ ? '_build' . $name : '_build_' . $name;
27             }
28              
29             delete $attrs->{parent};
30              
31             delete $attrs->{name}{predicate};
32             $attrs->{name}{lazy} = 1;
33             $attrs->{name}{builder} = '_build_name';
34              
35             $attrs->{of} = {
36             isa => 'ArrayRef',
37             required => 1,
38             };
39              
40             ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
41             sub _attrs {
42 4     4   10 return $attrs;
43             }
44             }
45              
46 5     5 0 395 sub parent {undef}
47              
48             ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
49 2     2   8 sub _has_parent {0}
50              
51             sub _has_name {
52 11     11   20 my $self = shift;
53 11         38 return defined $self->name;
54             }
55              
56             sub _build_name {
57 3     3   20 my $self = shift;
58              
59 3 100   4   10 return unless all { $_->_has_name } @{ $self->of };
  4         21  
  3         11  
60 1         5 return join q{ & }, map { $_->name } @{ $self->of };
  2         9  
  1         3  
61             }
62              
63             ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
64             sub _has_constraint {
65 20     20   35 my $self = shift;
66              
67 20         74 return !$self->_has_inline_generator;
68             }
69             ## use critic
70              
71             sub _build_constraint {
72 3     3   64 return $_[0]->_optimized_constraint;
73             }
74              
75             sub _build_optimized_constraint {
76 5     5   38 my $self = shift;
77              
78             ## no critic (Subroutines::ProtectPrivateSubs)
79 5         12 my @c = map { $_->_optimized_constraint } @{ $self->of };
  10         516  
  5         15  
80             return sub {
81 326     326   15187 return all { $_->( $_[0] ) } @c;
  346         3453  
82 5         489 };
83             }
84              
85             sub _has_inline_generator {
86 46     46   133 my $self = shift;
87              
88             ## no critic (Subroutines::ProtectPrivateSubs)
89 46     72   142 return all { $_->_has_inline_generator } @{ $self->of };
  72         457  
  46         121  
90             }
91              
92             sub _build_inline_generator {
93 2     2   12 my $self = shift;
94              
95             return sub {
96             return '(' . (
97             join q{ && },
98 8         28 map { sprintf( '( %s )', $_->_inline_generator->( $_, $_[1] ) ) }
99 4     4   19 @{ $self->of }
  4         9  
100             ) . ')';
101             }
102 2         14 }
103              
104             sub _build_inline_environment {
105 2     2   13 my $self = shift;
106              
107 2         3 my %env;
108 2         5 for my $type ( @{ $self->of } ) {
  2         5  
109             %env = (
110             %env,
111 4         24 %{ $type->inline_environment },
  4         12  
112             );
113             }
114              
115 2         13 return \%env;
116             }
117              
118             __PACKAGE__->_ooify;
119              
120             1;
121              
122             # ABSTRACT: A class for intersection constraints
123              
124             __END__
125              
126             =pod
127              
128             =encoding UTF-8
129              
130             =head1 NAME
131              
132             Specio::Constraint::Intersection - A class for intersection constraints
133              
134             =head1 VERSION
135              
136             version 0.53
137              
138             =head1 SYNOPSIS
139              
140             my $type = Specio::Constraint::Untion->new(...);
141              
142             =head1 DESCRIPTION
143              
144             This is a specialized type constraint class for intersections, which will allow
145             a value which matches each one of several distinct types.
146              
147             =for Pod::Coverage parent
148              
149             =head1 API
150              
151             This class provides all of the same methods as L<Specio::Constraint::Simple>,
152             with a few differences:
153              
154             =head2 Specio::Constraint::Intersection->new( ... )
155              
156             The C<parent> parameter is ignored if it passed, as it is always C<undef>
157              
158             The C<inline_generator> and C<constraint> parameters are also ignored. This
159             class provides its own default inline generator subroutine reference.
160              
161             Finally, this class requires an additional parameter, C<of>. This must be an
162             arrayref of type objects.
163              
164             =head2 $union->of
165              
166             Returns an array reference of the individual types which makes up this
167             intersection.
168              
169             =head1 ROLES
170              
171             This class does the L<Specio::Constraint::Role::Interface> and
172             L<Specio::Role::Inlinable> roles.
173              
174             =head1 SUPPORT
175              
176             Bugs may be submitted at L<https://github.com/houseabsolute/Specio/issues>.
177              
178             =head1 SOURCE
179              
180             The source code repository for Specio can be found at L<https://github.com/houseabsolute/Specio>.
181              
182             =head1 AUTHOR
183              
184             Dave Rolsky <autarch@urth.org>
185              
186             =head1 COPYRIGHT AND LICENSE
187              
188             This software is Copyright (c) 2012 - 2025 by Dave Rolsky.
189              
190             This is free software, licensed under:
191              
192             The Artistic License 2.0 (GPL Compatible)
193              
194             The full text of the license can be found in the
195             F<LICENSE> file included with this distribution.
196              
197             =cut