Changelog
[gitmo/Moose.git] / lib / Moose / Meta / TypeConstraint / Union.pm
1
2 package Moose::Meta::TypeConstraint::Union;
3
4 use strict;
5 use warnings;
6 use metaclass;
7
8 use Moose::Meta::TypeCoercion::Union;
9
10 our $VERSION   = '0.06';
11 our $AUTHORITY = 'cpan:STEVAN';
12
13 use base 'Moose::Meta::TypeConstraint';
14
15 __PACKAGE__->meta->add_attribute('type_constraints' => (
16     accessor  => 'type_constraints',
17     default   => sub { [] }
18 ));
19
20 sub new { 
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     ));
40     return $self;
41 }
42
43 sub validate {
44     my ($self, $value) = @_;
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
77 =head1 NAME
78
79 Moose::Meta::TypeConstraint::Union - A union of Moose type constraints
80
81 =head1 DESCRIPTION
82
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
89 =head1 METHODS
90
91 This class is not a subclass of Moose::Meta::TypeConstraint, 
92 but it does provide the same API
93
94 =over 4
95
96 =item B<meta>
97
98 =item B<new>
99
100 =item B<name>
101
102 =item B<type_constraints>
103
104 =item B<constraint>
105
106 =item B<includes_type>
107
108 =back
109
110 =head2 Overriden methods 
111
112 =over 4
113
114 =item B<check>
115
116 =item B<coerce>
117
118 =item B<validate>
119
120 =item B<is_a_type_of>
121
122 =item B<is_subtype_of>
123
124 =back
125
126 =head2 Empty or Stub methods
127
128 These methods tend to not be very relevant in 
129 the context of a union. Either that or they are 
130 just difficult to specify and not very useful 
131 anyway. They are here for completeness.
132
133 =over 4
134
135 =item B<parent>
136
137 =item B<coercion>
138
139 =item B<has_coercion>
140
141 =item B<message>
142
143 =item B<has_message>
144
145 =item B<hand_optimized_type_constraint>
146
147 =item B<has_hand_optimized_type_constraint>
148
149 =back
150
151 =head1 BUGS
152
153 All complex software has bugs lurking in it, and this module is no 
154 exception. If you find a bug please either email me, or add the bug
155 to cpan-RT.
156
157 =head1 AUTHOR
158
159 Stevan Little E<lt>stevan@iinteractive.comE<gt>
160
161 =head1 COPYRIGHT AND LICENSE
162
163 Copyright 2006-2008 by Infinity Interactive, Inc.
164
165 L<http://www.iinteractive.com>
166
167 This library is free software; you can redistribute it and/or modify
168 it under the same terms as Perl itself.
169
170 =cut