Commit | Line | Data |
8ee73eeb |
1 | |
2 | package Moose::Meta::TypeConstraint::Union; |
3 | |
4 | use strict; |
5 | use warnings; |
6 | use metaclass; |
7 | |
3726f905 |
8 | use Moose::Meta::TypeCoercion::Union; |
9 | |
d67145ed |
10 | our $VERSION = '0.06'; |
d44714be |
11 | our $AUTHORITY = 'cpan:STEVAN'; |
8ee73eeb |
12 | |
d67145ed |
13 | use base 'Moose::Meta::TypeConstraint'; |
14 | |
8ee73eeb |
15 | __PACKAGE__->meta->add_attribute('type_constraints' => ( |
16 | accessor => 'type_constraints', |
17 | default => sub { [] } |
18 | )); |
19 | |
20 | sub new { |
3726f905 |
21 | my ($class, %options) = @_; |
22 | my $self = $class->SUPER::new( |
23 | name => (join ' | ' => map { $_->name } @{$options{type_constraints}}), |
24 | parent => undef, |
25 | message => undef, |
26 | hand_optimized_type_constraint => undef, |
27 | compiled_type_constraint => sub { |
28 | my $value = shift; |
29 | foreach my $type (@{$options{type_constraints}}) { |
30 | return 1 if $type->check($value); |
31 | } |
32 | return undef; |
33 | }, |
34 | %options |
35 | ); |
36 | $self->_set_constraint(sub { $self->check($_[0]) }); |
37 | $self->coercion(Moose::Meta::TypeCoercion::Union->new( |
38 | type_constraint => $self |
39 | )); |
8ee73eeb |
40 | return $self; |
41 | } |
42 | |
8ee73eeb |
43 | sub validate { |
3726f905 |
44 | my ($self, $value) = @_; |
8ee73eeb |
45 | my $message; |
46 | foreach my $type (@{$self->type_constraints}) { |
47 | my $err = $type->validate($value); |
48 | return unless defined $err; |
49 | $message .= ($message ? ' and ' : '') . $err |
50 | if defined $err; |
51 | } |
52 | return ($message . ' in (' . $self->name . ')') ; |
53 | } |
54 | |
55 | sub is_a_type_of { |
56 | my ($self, $type_name) = @_; |
57 | foreach my $type (@{$self->type_constraints}) { |
58 | return 1 if $type->is_a_type_of($type_name); |
59 | } |
60 | return 0; |
61 | } |
62 | |
63 | sub is_subtype_of { |
64 | my ($self, $type_name) = @_; |
65 | foreach my $type (@{$self->type_constraints}) { |
66 | return 1 if $type->is_subtype_of($type_name); |
67 | } |
68 | return 0; |
69 | } |
70 | |
71 | 1; |
72 | |
73 | __END__ |
74 | |
75 | =pod |
76 | |
39b3bc94 |
77 | =head1 NAME |
78 | |
ecb59493 |
79 | Moose::Meta::TypeConstraint::Union - A union of Moose type constraints |
39b3bc94 |
80 | |
81 | =head1 DESCRIPTION |
82 | |
ecb59493 |
83 | This metaclass represents a union of Moose type constraints. More |
84 | details to be explained later (possibly in a Cookbook::Recipe). |
85 | |
86 | This actually used to be part of Moose::Meta::TypeConstraint, but it |
87 | is now better off in it's own file. |
88 | |
39b3bc94 |
89 | =head1 METHODS |
90 | |
ecb59493 |
91 | This class is not a subclass of Moose::Meta::TypeConstraint, |
92 | but it does provide the same API |
93 | |
39b3bc94 |
94 | =over 4 |
95 | |
ecb59493 |
96 | =item B<meta> |
39b3bc94 |
97 | |
ecb59493 |
98 | =item B<new> |
39b3bc94 |
99 | |
ecb59493 |
100 | =item B<name> |
101 | |
102 | =item B<type_constraints> |
39b3bc94 |
103 | |
104 | =item B<constraint> |
105 | |
ecb59493 |
106 | =back |
39b3bc94 |
107 | |
ecb59493 |
108 | =head2 Overriden methods |
109 | |
110 | =over 4 |
111 | |
112 | =item B<check> |
113 | |
114 | =item B<coerce> |
115 | |
116 | =item B<validate> |
39b3bc94 |
117 | |
118 | =item B<is_a_type_of> |
119 | |
120 | =item B<is_subtype_of> |
121 | |
ecb59493 |
122 | =back |
39b3bc94 |
123 | |
ecb59493 |
124 | =head2 Empty or Stub methods |
39b3bc94 |
125 | |
ecb59493 |
126 | These methods tend to not be very relevant in |
127 | the context of a union. Either that or they are |
128 | just difficult to specify and not very useful |
129 | anyway. They are here for completeness. |
39b3bc94 |
130 | |
ecb59493 |
131 | =over 4 |
39b3bc94 |
132 | |
133 | =item B<parent> |
134 | |
ecb59493 |
135 | =item B<coercion> |
39b3bc94 |
136 | |
ecb59493 |
137 | =item B<has_coercion> |
138 | |
139 | =item B<message> |
140 | |
141 | =item B<has_message> |
39b3bc94 |
142 | |
0eec94be |
143 | =item B<hand_optimized_type_constraint> |
144 | |
145 | =item B<has_hand_optimized_type_constraint> |
146 | |
39b3bc94 |
147 | =back |
148 | |
149 | =head1 BUGS |
150 | |
151 | All complex software has bugs lurking in it, and this module is no |
152 | exception. If you find a bug please either email me, or add the bug |
153 | to cpan-RT. |
154 | |
155 | =head1 AUTHOR |
156 | |
157 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
158 | |
39b3bc94 |
159 | =head1 COPYRIGHT AND LICENSE |
160 | |
778db3ac |
161 | Copyright 2006-2008 by Infinity Interactive, Inc. |
39b3bc94 |
162 | |
163 | L<http://www.iinteractive.com> |
164 | |
165 | This library is free software; you can redistribute it and/or modify |
166 | it under the same terms as Perl itself. |
167 | |
8ee73eeb |
168 | =cut |