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