Commit | Line | Data |
0a6bff54 |
1 | package Moose::Meta::TypeConstraint::DuckType; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | use metaclass; |
6 | |
964294c1 |
7 | use B; |
42af2755 |
8 | use Scalar::Util 'blessed'; |
0a6bff54 |
9 | use List::MoreUtils qw(all); |
10 | use Moose::Util 'english_list'; |
11 | |
12 | use Moose::Util::TypeConstraints (); |
13 | |
0a6bff54 |
14 | use base 'Moose::Meta::TypeConstraint'; |
15 | |
16 | __PACKAGE__->meta->add_attribute('methods' => ( |
17 | accessor => 'methods', |
18 | )); |
19 | |
964294c1 |
20 | my $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 |
31 | sub 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 | |
53 | sub 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 | |
75 | sub 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 | |
86 | sub _compile_hand_optimized_type_constraint { |
87 | my $self = shift; |
88 | |
89 | my @methods = @{ $self->methods }; |
90 | |
91 | sub { |
92 | my $obj = shift; |
93 | |
94 | return blessed($obj) |
95 | && blessed($obj) ne 'Regexp' |
96 | && all { $obj->can($_) } @methods; |
97 | }; |
98 | } |
99 | |
100 | sub create_child_type { |
101 | my ($self, @args) = @_; |
102 | return Moose::Meta::TypeConstraint->new(@args, parent => $self); |
103 | } |
104 | |
105 | sub get_message { |
106 | my $self = shift; |
107 | my ($value) = @_; |
108 | |
109 | if ($self->has_message) { |
110 | return $self->SUPER::get_message(@_); |
111 | } |
112 | |
113 | my @methods = grep { !$value->can($_) } @{ $self->methods }; |
114 | my $class = blessed $value; |
115 | return $class |
116 | . " is missing methods " |
117 | . english_list(map { "'$_'" } @methods); |
118 | } |
119 | |
120 | 1; |
121 | |
ad46f524 |
122 | # ABSTRACT: Type constraint for duck typing |
123 | |
0a6bff54 |
124 | __END__ |
125 | |
126 | =pod |
127 | |
0a6bff54 |
128 | =head1 DESCRIPTION |
129 | |
130 | This class represents type constraints based on an enumerated list of |
131 | required methods. |
132 | |
133 | =head1 INHERITANCE |
134 | |
135 | C<Moose::Meta::TypeConstraint::DuckType> is a subclass of |
136 | L<Moose::Meta::TypeConstraint>. |
137 | |
138 | =head1 METHODS |
139 | |
140 | =over 4 |
141 | |
142 | =item B<< Moose::Meta::TypeConstraint::DuckType->new(%options) >> |
143 | |
144 | This creates a new duck type constraint based on the given |
145 | C<%options>. |
146 | |
147 | It takes the same options as its parent, with several |
148 | exceptions. First, it requires an additional option, C<methods>. This |
149 | should be an array reference containing a list of required method |
150 | names. Second, it automatically sets the parent to the C<Object> type. |
151 | |
152 | Finally, it ignores any provided C<constraint> option. The constraint |
153 | is generated automatically based on the provided C<methods>. |
154 | |
155 | =item B<< $constraint->methods >> |
156 | |
157 | Returns the array reference of required methods provided to the |
158 | constructor. |
159 | |
160 | =item B<< $constraint->create_child_type >> |
161 | |
162 | This returns a new L<Moose::Meta::TypeConstraint> object with the type |
163 | as its parent. |
164 | |
165 | Note that it does I<not> return a C<Moose::Meta::TypeConstraint::DuckType> |
166 | object! |
167 | |
168 | =back |
169 | |
170 | =head1 BUGS |
171 | |
d4048ef3 |
172 | See L<Moose/BUGS> for details on reporting bugs. |
0a6bff54 |
173 | |
0a6bff54 |
174 | =cut |
175 | |