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 2     2   688 use strict;
  2         4  
  2         51  
4 2     2   8 use warnings;
  2         3  
  2         71  
5              
6             our $VERSION = '0.46';
7              
8 2     2   10 use List::Util qw( all any );
  2         4  
  2         124  
9 2     2   13 use Role::Tiny::With;
  2         3  
  2         115  
10 2     2   13 use Specio::OO;
  2         3  
  2         97  
11 2     2   12 use Storable qw( dclone );
  2         4  
  2         68  
12              
13 2     2   10 use Specio::Constraint::Role::Interface;
  2         6  
  2         1236  
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 5     5   8 return $attrs;
43             }
44             }
45              
46 5     5 0 351 sub parent {undef}
47              
48             ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
49 1     1   4 sub _has_parent {0}
50              
51             sub _has_name {
52 8     8   16 my $self = shift;
53 8         23 return defined $self->name;
54             }
55              
56             sub _build_name {
57 3     3   17 my $self = shift;
58              
59 3 100   4   10 return unless all { $_->_has_name } @{ $self->of };
  4         17  
  3         6  
60 1         5 return join q{ | }, map { $_->name } @{ $self->of };
  2         9  
  1         2  
61             }
62              
63             ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
64             sub _has_constraint {
65 20     20   32 my $self = shift;
66              
67 20         31 return !$self->_has_inline_generator;
68             }
69             ## use critic
70              
71             sub _build_constraint {
72 2     2   37 return $_[0]->_optimized_constraint;
73             }
74              
75             sub _build_optimized_constraint {
76 4     4   28 my $self = shift;
77              
78             ## no critic (Subroutines::ProtectPrivateSubs)
79 4         8 my @c = map { $_->_optimized_constraint } @{ $self->of };
  8         451  
  4         10  
80             return sub {
81 346     346   2532 return any { $_->( $_[0] ) } @c;
  596         7362  
82 4         362 };
83             }
84              
85             sub _has_inline_generator {
86 46     46   125 my $self = shift;
87              
88             ## no critic (Subroutines::ProtectPrivateSubs)
89 46     72   104 return all { $_->_has_inline_generator } @{ $self->of };
  72         290  
  46         86  
90             }
91              
92             sub _build_inline_generator {
93 2     2   13 my $self = shift;
94              
95             return sub {
96             return '(' . (
97             join q{ || },
98 8         33 map { sprintf( '( %s )', $_->_inline_generator->( $_, $_[1] ) ) }
99 4     4   21 @{ $self->of }
  4         9  
100             ) . ')';
101             }
102 2         9 }
103              
104             sub _build_inline_environment {
105 2     2   10 my $self = shift;
106              
107 2         3 my %env;
108 2         4 for my $type ( @{ $self->of } ) {
  2         4  
109             %env = (
110             %env,
111 4         18 %{ $type->inline_environment },
  4         17  
112             );
113             }
114              
115 2         13 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.46
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 - 2020 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