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   925 use strict;
  2         6  
  2         66  
4 2     2   10 use warnings;
  2         4  
  2         88  
5              
6             our $VERSION = '0.47';
7              
8 2     2   11 use List::Util qw( all );
  2         3  
  2         109  
9 2     2   12 use Role::Tiny::With;
  2         4  
  2         104  
10 2     2   15 use Specio::OO;
  2         4  
  2         113  
11 2     2   12 use Storable qw( dclone );
  2         4  
  2         91  
12              
13 2     2   13 use Specio::Constraint::Role::Interface;
  2         13  
  2         1476  
14             with 'Specio::Constraint::Role::Interface';
15              
16             {
17             ## no critic (Subroutines::ProtectPrivateSubs)
18             my $attrs = dclone( 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 410 sub parent {undef}
47              
48             ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
49 2     2   12 sub _has_parent {0}
50              
51             sub _has_name {
52 11     11   21 my $self = shift;
53 11         78 return defined $self->name;
54             }
55              
56             sub _build_name {
57 3     3   30 my $self = shift;
58              
59 3 100   4   13 return unless all { $_->_has_name } @{ $self->of };
  4         28  
  3         11  
60 1         7 return join q{ & }, map { $_->name } @{ $self->of };
  2         12  
  1         4  
61             }
62              
63             ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
64             sub _has_constraint {
65 20     20   38 my $self = shift;
66              
67 20         55 return !$self->_has_inline_generator;
68             }
69             ## use critic
70              
71             sub _build_constraint {
72 3     3   84 return $_[0]->_optimized_constraint;
73             }
74              
75             sub _build_optimized_constraint {
76 5     5   50 my $self = shift;
77              
78             ## no critic (Subroutines::ProtectPrivateSubs)
79 5         10 my @c = map { $_->_optimized_constraint } @{ $self->of };
  10         458  
  5         21  
80             return sub {
81 326     326   2991 return all { $_->( $_[0] ) } @c;
  346         2430  
82 5         447 };
83             }
84              
85             sub _has_inline_generator {
86 46     46   150 my $self = shift;
87              
88             ## no critic (Subroutines::ProtectPrivateSubs)
89 46     72   133 return all { $_->_has_inline_generator } @{ $self->of };
  72         383  
  46         104  
90             }
91              
92             sub _build_inline_generator {
93 2     2   16 my $self = shift;
94              
95             return sub {
96             return '(' . (
97             join q{ && },
98 8         34 map { sprintf( '( %s )', $_->_inline_generator->( $_, $_[1] ) ) }
99 4     4   24 @{ $self->of }
  4         9  
100             ) . ')';
101             }
102 2         13 }
103              
104             sub _build_inline_environment {
105 2     2   16 my $self = shift;
106              
107 2         4 my %env;
108 2         4 for my $type ( @{ $self->of } ) {
  2         6  
109             %env = (
110             %env,
111 4         25 %{ $type->inline_environment },
  4         15  
112             );
113             }
114              
115 2         14 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.47
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             I am also usually active on IRC as 'autarch' on C<irc://irc.perl.org>.
179              
180             =head1 SOURCE
181              
182             The source code repository for Specio can be found at L<https://github.com/houseabsolute/Specio>.
183              
184             =head1 AUTHOR
185              
186             Dave Rolsky <autarch@urth.org>
187              
188             =head1 COPYRIGHT AND LICENSE
189              
190             This software is Copyright (c) 2012 - 2021 by Dave Rolsky.
191              
192             This is free software, licensed under:
193              
194             The Artistic License 2.0 (GPL Compatible)
195              
196             The full text of the license can be found in the
197             F<LICENSE> file included with this distribution.
198              
199             =cut