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