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