File Coverage

blib/lib/Type/Coercion/Union.pm
Criterion Covered Total %
statement 46 47 100.0
branch 9 16 56.2
condition n/a
subroutine 13 13 100.0
pod 3 3 100.0
total 71 79 91.1


line stmt bran cond sub pod time code
1             package Type::Coercion::Union;
2              
3 26     26   1533 use 5.008001;
  26         107  
4 26     26   151 use strict;
  26         49  
  26         612  
5 26     26   147 use warnings;
  26         74  
  26         1389  
6              
7             BEGIN {
8 26     26   110 $Type::Coercion::Union::AUTHORITY = 'cpan:TOBYINK';
9 26         1210 $Type::Coercion::Union::VERSION = '2.003_000';
10             }
11              
12             $Type::Coercion::Union::VERSION =~ tr/_//d;
13              
14 26     26   222 use Scalar::Util qw< blessed >;
  26         65  
  26         1389  
15 26     26   172 use Types::TypeTiny ();
  26         76  
  26         13502  
16              
17 1     1   6 sub _croak ($;@) { require Error::TypeTiny; goto \&Error::TypeTiny::croak }
  1         6  
18              
19             require Type::Coercion;
20             our @ISA = 'Type::Coercion';
21              
22             sub _preserve_type_constraint {
23 74     74   156 my $self = shift;
24             $self->{_union_of} = $self->{type_constraint}->type_constraints
25 74 50       1211 if $self->{type_constraint};
26             }
27              
28             sub _maybe_restore_type_constraint {
29 2     2   3 my $self = shift;
30 2 50       6 if ( my $union = $self->{_union_of} ) {
31 2         12 return Type::Tiny::Union->new( type_constraints => $union );
32             }
33 0         0 return; # uncoverable statement
34             }
35              
36             sub type_coercion_map {
37 24     24 1 40 my $self = shift;
38            
39 24         68 Types::TypeTiny::assert_TypeTiny( my $type = $self->type_constraint );
40 24 50       60 $type->isa( 'Type::Tiny::Union' )
41             or _croak
42             "Type::Coercion::Union must be used in conjunction with Type::Tiny::Union";
43            
44 24         43 my @c;
45 24         62 for my $tc ( @$type ) {
46 48 100       106 next unless $tc->has_coercion;
47 46         69 push @c, @{ $tc->coercion->type_coercion_map };
  46         94  
48             }
49 24         123 return \@c;
50             } #/ sub type_coercion_map
51              
52             sub add_type_coercions {
53 1     1 1 13 my $self = shift;
54 1 50       6 _croak "Adding coercions to Type::Coercion::Union not currently supported"
55             if @_;
56             }
57              
58             sub _build_moose_coercion {
59 1     1   2 my $self = shift;
60            
61 1         2 my %options = ();
62 1 50       5 $options{type_constraint} = $self->type_constraint
63             if $self->has_type_constraint;
64            
65 1         6 require Moose::Meta::TypeCoercion::Union;
66 1         15 my $r = "Moose::Meta::TypeCoercion::Union"->new( %options );
67            
68 1         67 return $r;
69             } #/ sub _build_moose_coercion
70              
71             sub can_be_inlined {
72 7     7 1 12 my $self = shift;
73            
74 7         19 Types::TypeTiny::assert_TypeTiny( my $type = $self->type_constraint );
75            
76 7         26 for my $tc ( @$type ) {
77 14 50       31 next unless $tc->has_coercion;
78 14 50       40 return !!0 unless $tc->coercion->can_be_inlined;
79             }
80            
81 7         30 !!1;
82             } #/ sub can_be_inlined
83              
84             1;
85              
86             __END__
87              
88             =pod
89              
90             =encoding utf-8
91              
92             =head1 NAME
93              
94             Type::Coercion::Union - a set of coercions to a union type constraint
95              
96             =head1 STATUS
97              
98             This module is covered by the
99             L<Type-Tiny stability policy|Type::Tiny::Manual::Policies/"STABILITY">.
100              
101             =head1 DESCRIPTION
102              
103             This package inherits from L<Type::Coercion>; see that for most documentation.
104             The major differences are that C<add_type_coercions> always throws an
105             exception, and the C<type_coercion_map> is automatically populated from
106             the child constraints of the union type constraint.
107              
108             =head1 BUGS
109              
110             Please report any bugs to
111             L<https://github.com/tobyink/p5-type-tiny/issues>.
112              
113             =head1 SEE ALSO
114              
115             L<Type::Coercion>.
116              
117             L<Moose::Meta::TypeCoercion::Union>.
118              
119             =head1 AUTHOR
120              
121             Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
122              
123             =head1 COPYRIGHT AND LICENCE
124              
125             This software is copyright (c) 2013-2014, 2017-2023 by Toby Inkster.
126              
127             This is free software; you can redistribute it and/or modify it under
128             the same terms as the Perl 5 programming language system itself.
129              
130             =head1 DISCLAIMER OF WARRANTIES
131              
132             THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
133             WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
134             MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.