1 package Moose::Meta::TypeConstraint::DuckType;
7 use List::MoreUtils qw(all);
8 use Moose::Util 'english_list';
10 use Moose::Util::TypeConstraints ();
12 our $VERSION = '0.83';
13 $VERSION = eval $VERSION;
14 our $AUTHORITY = 'cpan:STEVAN';
16 use base 'Moose::Meta::TypeConstraint';
18 __PACKAGE__->meta->add_attribute('methods' => (
19 accessor => 'methods',
23 my ( $class, %args ) = @_;
25 $args{parent} = Moose::Util::TypeConstraints::find_type_constraint('Object');
27 my $self = $class->_new(\%args);
29 $self->compile_type_constraint()
30 unless $self->_has_compiled_type_constraint;
36 my ( $self, $type_or_name ) = @_;
38 my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
40 return unless $other->isa(__PACKAGE__);
42 my @self_methods = sort @{ $self->methods };
43 my @other_methods = sort @{ $other->methods };
45 return unless @self_methods == @other_methods;
47 while ( @self_methods ) {
48 my $method = shift @self_methods;
49 my $other_method = shift @other_methods;
51 return unless $method eq $other_method;
60 my @methods = @{ $self->methods };
64 return all { $obj->can($_) } @methods
68 sub _compile_hand_optimized_type_constraint {
71 my @methods = @{ $self->methods };
77 && blessed($obj) ne 'Regexp'
78 && all { $obj->can($_) } @methods;
82 sub create_child_type {
83 my ($self, @args) = @_;
84 return Moose::Meta::TypeConstraint->new(@args, parent => $self);
91 if ($self->has_message) {
92 return $self->SUPER::get_message(@_);
95 my @methods = grep { !$value->can($_) } @{ $self->methods };
96 my $class = blessed $value;
98 . " is missing methods "
99 . english_list(map { "'$_'" } @methods);
110 Moose::Meta::TypeConstraint::DuckType - Type constraint for duck typing
114 This class represents type constraints based on an enumerated list of
119 C<Moose::Meta::TypeConstraint::DuckType> is a subclass of
120 L<Moose::Meta::TypeConstraint>.
126 =item B<< Moose::Meta::TypeConstraint::DuckType->new(%options) >>
128 This creates a new duck type constraint based on the given
131 It takes the same options as its parent, with several
132 exceptions. First, it requires an additional option, C<methods>. This
133 should be an array reference containing a list of required method
134 names. Second, it automatically sets the parent to the C<Object> type.
136 Finally, it ignores any provided C<constraint> option. The constraint
137 is generated automatically based on the provided C<methods>.
139 =item B<< $constraint->methods >>
141 Returns the array reference of required methods provided to the
144 =item B<< $constraint->create_child_type >>
146 This returns a new L<Moose::Meta::TypeConstraint> object with the type
149 Note that it does I<not> return a C<Moose::Meta::TypeConstraint::DuckType>
156 All complex software has bugs lurking in it, and this module is no
157 exception. If you find a bug please either email me, or add the bug
162 Chris Prather E<lt>chris@prather.orgE<gt>
164 Shawn M Moore E<lt>sartak@gmail.comE<gt>
166 =head1 COPYRIGHT AND LICENSE
168 Copyright 2006-2009 by Infinity Interactive, Inc.
170 L<http://www.iinteractive.com>
172 This library is free software; you can redistribute it and/or modify
173 it under the same terms as Perl itself.