refactor the default type constraint message logic a bit
[gitmo/Moose.git] / lib / Moose / Meta / TypeConstraint / DuckType.pm
1 package Moose::Meta::TypeConstraint::DuckType;
2
3 use strict;
4 use warnings;
5 use metaclass;
6
7 use B;
8 use Scalar::Util 'blessed';
9 use List::MoreUtils qw(all);
10 use Moose::Util 'english_list';
11
12 use Moose::Util::TypeConstraints ();
13
14 use base 'Moose::Meta::TypeConstraint';
15
16 __PACKAGE__->meta->add_attribute('methods' => (
17     accessor => 'methods',
18 ));
19
20 my $inliner = sub {
21     my $self = shift;
22     my $val  = shift;
23
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              . ')';
30 };
31
32 sub new {
33     my ( $class, %args ) = @_;
34
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;
45
46     my $self = $class->SUPER::new(\%args);
47
48     $self->compile_type_constraint()
49         unless $self->_has_compiled_type_constraint;
50
51     return $self;
52 }
53
54 sub 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
76 sub 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
87 sub create_child_type {
88     my ($self, @args) = @_;
89     return Moose::Meta::TypeConstraint->new(@args, parent => $self);
90 }
91
92 sub 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;
102     $class ||= $value;
103
104     return $class
105          . " is missing methods "
106          . english_list(map { "'$_'" } @methods);
107 }
108
109 1;
110
111 # ABSTRACT: Type constraint for duck typing
112
113 __END__
114
115 =pod
116
117 =head1 DESCRIPTION
118
119 This class represents type constraints based on an enumerated list of
120 required methods.
121
122 =head1 INHERITANCE
123
124 C<Moose::Meta::TypeConstraint::DuckType> is a subclass of
125 L<Moose::Meta::TypeConstraint>.
126
127 =head1 METHODS
128
129 =over 4
130
131 =item B<< Moose::Meta::TypeConstraint::DuckType->new(%options) >>
132
133 This creates a new duck type constraint based on the given
134 C<%options>.
135
136 It takes the same options as its parent, with several
137 exceptions. First, it requires an additional option, C<methods>. This
138 should be an array reference containing a list of required method
139 names. Second, it automatically sets the parent to the C<Object> type.
140
141 Finally, it ignores any provided C<constraint> option. The constraint
142 is generated automatically based on the provided C<methods>.
143
144 =item B<< $constraint->methods >>
145
146 Returns the array reference of required methods provided to the
147 constructor.
148
149 =item B<< $constraint->create_child_type >>
150
151 This returns a new L<Moose::Meta::TypeConstraint> object with the type
152 as its parent.
153
154 Note that it does I<not> return a C<Moose::Meta::TypeConstraint::DuckType>
155 object!
156
157 =back
158
159 =head1 BUGS
160
161 See L<Moose/BUGS> for details on reporting bugs.
162
163 =cut
164