fa70470030356bc9a348bd2fc9f8cf634854fddb
[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->_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     return $class
103          . " is missing methods "
104          . english_list(map { "'$_'" } @methods);
105 }
106
107 1;
108
109 # ABSTRACT: Type constraint for duck typing
110
111 __END__
112
113 =pod
114
115 =head1 DESCRIPTION
116
117 This class represents type constraints based on an enumerated list of
118 required methods.
119
120 =head1 INHERITANCE
121
122 C<Moose::Meta::TypeConstraint::DuckType> is a subclass of
123 L<Moose::Meta::TypeConstraint>.
124
125 =head1 METHODS
126
127 =over 4
128
129 =item B<< Moose::Meta::TypeConstraint::DuckType->new(%options) >>
130
131 This creates a new duck type constraint based on the given
132 C<%options>.
133
134 It takes the same options as its parent, with several
135 exceptions. First, it requires an additional option, C<methods>. This
136 should be an array reference containing a list of required method
137 names. Second, it automatically sets the parent to the C<Object> type.
138
139 Finally, it ignores any provided C<constraint> option. The constraint
140 is generated automatically based on the provided C<methods>.
141
142 =item B<< $constraint->methods >>
143
144 Returns the array reference of required methods provided to the
145 constructor.
146
147 =item B<< $constraint->create_child_type >>
148
149 This returns a new L<Moose::Meta::TypeConstraint> object with the type
150 as its parent.
151
152 Note that it does I<not> return a C<Moose::Meta::TypeConstraint::DuckType>
153 object!
154
155 =back
156
157 =head1 BUGS
158
159 See L<Moose/BUGS> for details on reporting bugs.
160
161 =cut
162