Union inlining should always be surrounded by its own set of parens, for safety
[gitmo/Moose.git] / lib / Moose / Meta / TypeConstraint / DuckType.pm
CommitLineData
0a6bff54 1package Moose::Meta::TypeConstraint::DuckType;
2
3use strict;
4use warnings;
5use metaclass;
6
964294c1 7use B;
42af2755 8use Scalar::Util 'blessed';
0a6bff54 9use List::MoreUtils qw(all);
10use Moose::Util 'english_list';
11
12use Moose::Util::TypeConstraints ();
13
0a6bff54 14use base 'Moose::Meta::TypeConstraint';
15
16__PACKAGE__->meta->add_attribute('methods' => (
17 accessor => 'methods',
18));
19
964294c1 20my $inliner = sub {
21 my $self = shift;
22 my $val = shift;
23
3975b592 24 return 'Scalar::Util::blessed(' . $val . ') '
25 . '&& Scalar::Util::blessed(' . $val . ') ne "Regexp" '
26 . '&& &List::MoreUtils::all('
27 . 'sub { ' . $val . '->can($_) }, '
28 . join(', ', map { B::perlstring($_) } @{ $self->methods })
29 . ')';
964294c1 30};
31
0a6bff54 32sub new {
33 my ( $class, %args ) = @_;
34
964294c1 35 $args{parent}
36 = Moose::Util::TypeConstraints::find_type_constraint('Object');
37
38 my @methods = @{ $args{methods} };
39 $args{constraint} = sub {
40 blessed( $_[0] ) ne 'Regexp'
41 && all { $_[0]->can($_) } @methods;
42 };
43
44 $args{inlined} = $inliner;
0a6bff54 45
46 my $self = $class->_new(\%args);
47
48 $self->compile_type_constraint()
49 unless $self->_has_compiled_type_constraint;
50
51 return $self;
52}
53
54sub equals {
55 my ( $self, $type_or_name ) = @_;
56
57 my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
58
59 return unless $other->isa(__PACKAGE__);
60
61 my @self_methods = sort @{ $self->methods };
62 my @other_methods = sort @{ $other->methods };
63
64 return unless @self_methods == @other_methods;
65
66 while ( @self_methods ) {
67 my $method = shift @self_methods;
68 my $other_method = shift @other_methods;
69
70 return unless $method eq $other_method;
71 }
72
73 return 1;
74}
75
76sub constraint {
77 my $self = shift;
78
79 my @methods = @{ $self->methods };
80
81 return sub {
82 my $obj = shift;
83 return all { $obj->can($_) } @methods
84 };
85}
86
0a6bff54 87sub create_child_type {
88 my ($self, @args) = @_;
89 return Moose::Meta::TypeConstraint->new(@args, parent => $self);
90}
91
92sub get_message {
93 my $self = shift;
94 my ($value) = @_;
95
96 if ($self->has_message) {
97 return $self->SUPER::get_message(@_);
98 }
99
100 my @methods = grep { !$value->can($_) } @{ $self->methods };
101 my $class = blessed $value;
d9f45e2e 102 $class ||= $value;
103
0a6bff54 104 return $class
105 . " is missing methods "
106 . english_list(map { "'$_'" } @methods);
107}
108
1091;
110
ad46f524 111# ABSTRACT: Type constraint for duck typing
112
0a6bff54 113__END__
114
115=pod
116
0a6bff54 117=head1 DESCRIPTION
118
119This class represents type constraints based on an enumerated list of
120required methods.
121
122=head1 INHERITANCE
123
124C<Moose::Meta::TypeConstraint::DuckType> is a subclass of
125L<Moose::Meta::TypeConstraint>.
126
127=head1 METHODS
128
129=over 4
130
131=item B<< Moose::Meta::TypeConstraint::DuckType->new(%options) >>
132
133This creates a new duck type constraint based on the given
134C<%options>.
135
136It takes the same options as its parent, with several
137exceptions. First, it requires an additional option, C<methods>. This
138should be an array reference containing a list of required method
139names. Second, it automatically sets the parent to the C<Object> type.
140
141Finally, it ignores any provided C<constraint> option. The constraint
142is generated automatically based on the provided C<methods>.
143
144=item B<< $constraint->methods >>
145
146Returns the array reference of required methods provided to the
147constructor.
148
149=item B<< $constraint->create_child_type >>
150
151This returns a new L<Moose::Meta::TypeConstraint> object with the type
152as its parent.
153
154Note that it does I<not> return a C<Moose::Meta::TypeConstraint::DuckType>
155object!
156
157=back
158
159=head1 BUGS
160
d4048ef3 161See L<Moose/BUGS> for details on reporting bugs.
0a6bff54 162
0a6bff54 163=cut
164