1 package Moose::Meta::TypeConstraint::DuckType;
8 use Scalar::Util 'blessed';
9 use List::MoreUtils qw(all);
10 use Moose::Util 'english_list';
12 use Moose::Util::TypeConstraints ();
14 use base 'Moose::Meta::TypeConstraint';
16 __PACKAGE__->meta->add_attribute('methods' => (
17 accessor => 'methods',
18 Class::MOP::_definition_context(),
25 return 'my $val = ' . $val . '; Scalar::Util::blessed($val) '
26 . '&& Scalar::Util::blessed($val) ne "Regexp" '
27 . '&& &List::MoreUtils::all('
28 . 'sub { $val->can($_) }, '
29 . join(', ', map { B::perlstring($_) } @{ $self->methods })
34 my ( $class, %args ) = @_;
37 = Moose::Util::TypeConstraints::find_type_constraint('Object');
39 my @methods = @{ $args{methods} };
40 $args{constraint} = sub {
42 blessed($val) ne 'Regexp'
43 && all { $val->can($_) } @methods;
46 $args{inlined} = $inliner;
48 my $self = $class->SUPER::new(\%args);
50 $self->compile_type_constraint()
51 unless $self->_has_compiled_type_constraint;
57 my ( $self, $type_or_name ) = @_;
59 my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
61 return unless $other->isa(__PACKAGE__);
63 my @self_methods = sort @{ $self->methods };
64 my @other_methods = sort @{ $other->methods };
66 return unless @self_methods == @other_methods;
68 while ( @self_methods ) {
69 my $method = shift @self_methods;
70 my $other_method = shift @other_methods;
72 return unless $method eq $other_method;
81 my @methods = @{ $self->methods };
85 return all { $obj->can($_) } @methods
89 sub create_child_type {
90 my ($self, @args) = @_;
91 return Moose::Meta::TypeConstraint->new(@args, parent => $self);
98 if ($self->has_message) {
99 return $self->SUPER::get_message(@_);
102 return $self->SUPER::get_message($value) unless blessed($value);
104 my @methods = grep { !$value->can($_) } @{ $self->methods };
105 my $class = blessed $value;
109 . " is missing methods "
110 . english_list(map { "'$_'" } @methods);
115 # ABSTRACT: Type constraint for duck typing
123 This class represents type constraints based on an enumerated list of
128 C<Moose::Meta::TypeConstraint::DuckType> is a subclass of
129 L<Moose::Meta::TypeConstraint>.
135 =item B<< Moose::Meta::TypeConstraint::DuckType->new(%options) >>
137 This creates a new duck type constraint based on the given
140 It takes the same options as its parent, with several
141 exceptions. First, it requires an additional option, C<methods>. This
142 should be an array reference containing a list of required method
143 names. Second, it automatically sets the parent to the C<Object> type.
145 Finally, it ignores any provided C<constraint> option. The constraint
146 is generated automatically based on the provided C<methods>.
148 =item B<< $constraint->methods >>
150 Returns the array reference of required methods provided to the
153 =item B<< $constraint->create_child_type >>
155 This returns a new L<Moose::Meta::TypeConstraint> object with the type
158 Note that it does I<not> return a C<Moose::Meta::TypeConstraint::DuckType>
165 See L<Moose/BUGS> for details on reporting bugs.