File Coverage

blib/lib/Specio/Constraint/Union.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::Union;
2              
3 3     3   923 use strict;
  3         6  
  3         101  
4 3     3   49 use warnings;
  3         9  
  3         142  
5              
6             our $VERSION = '0.47';
7              
8 3     3   18 use List::Util qw( all any );
  3         6  
  3         187  
9 3     3   18 use Role::Tiny::With;
  3         4  
  3         132  
10 3     3   21 use Specio::OO;
  3         5  
  3         133  
11 3     3   17 use Storable qw( dclone );
  3         6  
  3         156  
12              
13 3     3   21 use Specio::Constraint::Role::Interface;
  3         6  
  3         2237  
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 7     7   18 return $attrs;
43             }
44             }
45              
46 5     5 0 384 sub parent {undef}
47              
48             ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
49 2     2   12 sub _has_parent {0}
50              
51             sub _has_name {
52 12     12   23 my $self = shift;
53 12         45 return defined $self->name;
54             }
55              
56             sub _build_name {
57 3     3   22 my $self = shift;
58              
59 3 100   4   13 return unless all { $_->_has_name } @{ $self->of };
  4         27  
  3         10  
60 1         9 return join q{ | }, map { $_->name } @{ $self->of };
  2         15  
  1         3  
61             }
62              
63             ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
64             sub _has_constraint {
65 22     22   39 my $self = shift;
66              
67 22         53 return !$self->_has_inline_generator;
68             }
69             ## use critic
70              
71             sub _build_constraint {
72 3     3   51 return $_[0]->_optimized_constraint;
73             }
74              
75             sub _build_optimized_constraint {
76 5     5   43 my $self = shift;
77              
78             ## no critic (Subroutines::ProtectPrivateSubs)
79 5         11 my @c = map { $_->_optimized_constraint } @{ $self->of };
  10         1154  
  5         19  
80             return sub {
81 346     346   3050 return any { $_->( $_[0] ) } @c;
  596         8644  
82 5         938 };
83             }
84              
85             sub _has_inline_generator {
86 49     49   176 my $self = shift;
87              
88             ## no critic (Subroutines::ProtectPrivateSubs)
89 49     78   125 return all { $_->_has_inline_generator } @{ $self->of };
  78         411  
  49         124  
90             }
91              
92             sub _build_inline_generator {
93 2     2   14 my $self = shift;
94              
95             return sub {
96             return '(' . (
97             join q{ || },
98 8         44 map { sprintf( '( %s )', $_->_inline_generator->( $_, $_[1] ) ) }
99 4     4   25 @{ $self->of }
  4         7  
100             ) . ')';
101             }
102 2         12 }
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         23 %{ $type->inline_environment },
  4         30  
112             );
113             }
114              
115 2         22 return \%env;
116             }
117              
118             __PACKAGE__->_ooify;
119              
120             1;
121              
122             # ABSTRACT: A class for union constraints
123              
124             __END__
125              
126             =pod
127              
128             =encoding UTF-8
129              
130             =head1 NAME
131              
132             Specio::Constraint::Union - A class for union 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 unions, which will allow a
145             value which matches any 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::Union->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 union.
167              
168             =head1 ROLES
169              
170             This class does the L<Specio::Constraint::Role::Interface> and
171             L<Specio::Role::Inlinable> roles.
172              
173             =head1 SUPPORT
174              
175             Bugs may be submitted at L<https://github.com/houseabsolute/Specio/issues>.
176              
177             I am also usually active on IRC as 'autarch' on C<irc://irc.perl.org>.
178              
179             =head1 SOURCE
180              
181             The source code repository for Specio can be found at L<https://github.com/houseabsolute/Specio>.
182              
183             =head1 AUTHOR
184              
185             Dave Rolsky <autarch@urth.org>
186              
187             =head1 COPYRIGHT AND LICENSE
188              
189             This software is Copyright (c) 2012 - 2021 by Dave Rolsky.
190              
191             This is free software, licensed under:
192              
193             The Artistic License 2.0 (GPL Compatible)
194              
195             The full text of the license can be found in the
196             F<LICENSE> file included with this distribution.
197              
198             =cut