Unknown type constraints are now interpreted as blessed($value) eq $type
[gitmo/Mouse.git] / lib / Mouse / Attribute.pm
1 #!/usr/bin/env perl
2 package Mouse::Attribute;
3 use strict;
4 use warnings;
5
6 use Carp 'confess';
7 use Scalar::Util 'blessed';
8
9 sub new {
10     my $class = shift;
11     my %args  = @_;
12
13     $args{init_arg} = $args{name}
14         unless exists $args{init_arg};
15     $args{is} ||= '';
16
17     bless \%args, $class;
18 }
19
20 sub name            { $_[0]->{name}            }
21 sub class           { $_[0]->{class}           }
22 sub _is_metadata    { $_[0]->{is}              }
23 sub is_required     { $_[0]->{required}        }
24 sub default         { $_[0]->{default}         }
25 sub is_lazy         { $_[0]->{lazy}            }
26 sub predicate       { $_[0]->{predicate}       }
27 sub clearer         { $_[0]->{clearer}         }
28 sub handles         { $_[0]->{handles}         }
29 sub weak_ref        { $_[0]->{weak_ref}        }
30 sub init_arg        { $_[0]->{init_arg}        }
31 sub type_constraint { $_[0]->{type_constraint} }
32 sub trigger         { $_[0]->{trigger}         }
33 sub builder         { $_[0]->{builder}         }
34
35 sub has_default         { exists $_[0]->{default}         }
36 sub has_predicate       { exists $_[0]->{predicate}       }
37 sub has_clearer         { exists $_[0]->{clearer}         }
38 sub has_handles         { exists $_[0]->{handles}         }
39 sub has_weak_ref        { exists $_[0]->{weak_ref}        }
40 sub has_init_arg        { exists $_[0]->{init_arg}        }
41 sub has_type_constraint { exists $_[0]->{type_constraint} }
42 sub has_trigger         { exists $_[0]->{trigger}         }
43 sub has_builder         { exists $_[0]->{builder}         }
44
45 sub generate_accessor {
46     my $attribute = shift;
47
48     my $name       = $attribute->name;
49     my $key        = $attribute->init_arg;
50     my $default    = $attribute->default;
51     my $trigger    = $attribute->trigger;
52     my $type       = $attribute->type_constraint;
53     my $constraint = $attribute->find_type_constraint;
54
55     my $accessor = 'sub {
56         my $self = shift;';
57
58     if ($attribute->_is_metadata eq 'rw') {
59         $accessor .= 'if (@_) {
60             local $_ = $_[0];';
61
62         if ($constraint) {
63             $accessor .= 'do {
64                 my $display = defined($_) ? $_ : "undef";
65                 Carp::confess("Attribute ($name) does not pass the type constraint because: Validation failed for \'$type\' failed with value $display") unless $constraint->();
66             };'
67         }
68
69         $accessor .= '$self->{$key} = $_;';
70
71         if ($attribute->weak_ref) {
72             $accessor .= 'Scalar::Util::weaken($self->{$key});';
73         }
74
75         if ($trigger) {
76             $accessor .= '$trigger->($self, $_, $attribute);';
77         }
78
79         $accessor .= '}';
80     }
81     else {
82     }
83
84     if ($attribute->is_lazy) {
85         $accessor .= '$self->{$key} = ';
86         $accessor .= ref($default) eq 'CODE'
87                    ? '$default->($self)'
88                    : '$default';
89         $accessor .= ' if !exists($self->{$key});';
90     }
91
92     $accessor .= 'return $self->{$key}
93     }';
94
95     return eval $accessor;
96 }
97
98 sub generate_predicate {
99     my $attribute = shift;
100     my $key = $attribute->init_arg;
101
102     my $predicate = 'sub { exists($_[0]->{$key}) }';
103
104     return eval $predicate;
105 }
106
107 sub generate_clearer {
108     my $attribute = shift;
109     my $key = $attribute->init_arg;
110
111     my $predicate = 'sub { delete($_[0]->{$key}) }';
112
113     return eval $predicate;
114 }
115
116 sub generate_handles {
117     my $attribute = shift;
118     my $reader = $attribute->name;
119
120     my %method_map;
121
122     for my $local_method (keys %{ $attribute->handles }) {
123         my $remote_method = $attribute->handles->{$local_method};
124
125         my $method = 'sub {
126             my $self = shift;
127             $self->$reader->$remote_method(@_)
128         }';
129
130         $method_map{$local_method} = eval $method;
131     }
132
133     return \%method_map;
134 }
135
136 sub create {
137     my ($self, $class, $name, %args) = @_;
138
139     confess "You must specify a default for lazy attribute '$name'"
140         if $args{lazy} && !exists($args{default});
141
142     confess "Trigger is not allowed on read-only attribute '$name'"
143         if $args{trigger} && $args{is} ne 'rw';
144
145     confess "References are not allowed as default values, you must wrap the default of '$name' in a CODE reference (ex: sub { [] } and not [])"
146         if ref($args{default})
147         && ref($args{default}) ne 'CODE';
148
149     $args{handles} = { map { $_ => $_ } @{ $args{handles} } }
150         if $args{handles}
151         && ref($args{handles}) eq 'ARRAY';
152
153     confess "You must pass a HASH or ARRAY to handles"
154         if exists($args{handles})
155         && ref($args{handles}) ne 'HASH';
156
157     $args{type_constraint} = delete $args{isa};
158
159     my $attribute = $self->new(%args, name => $name, class => $class);
160     my $meta = $class->meta;
161
162     $meta->add_attribute($attribute);
163
164     # install an accessor
165     if ($attribute->_is_metadata eq 'rw' || $attribute->_is_metadata eq 'ro') {
166         my $accessor = $attribute->generate_accessor;
167         no strict 'refs';
168         *{ $class . '::' . $name } = $accessor;
169     }
170
171     for my $method (qw/predicate clearer/) {
172         my $predicate = "has_$method";
173         if ($attribute->$predicate) {
174             my $generator = "generate_$method";
175             my $coderef = $attribute->$generator;
176             no strict 'refs';
177             *{ $class . '::' . $attribute->$method } = $coderef;
178         }
179     }
180
181     if ($attribute->has_handles) {
182         my $method_map = $attribute->generate_handles;
183         for my $method_name (keys %$method_map) {
184             no strict 'refs';
185             *{ $class . '::' . $method_name } = $method_map->{$method_name};
186         }
187     }
188
189     return $attribute;
190 }
191
192 sub find_type_constraint {
193     my $self = shift;
194     my $type = $self->type_constraint;
195
196     return unless $type;
197
198     my $checker = Mouse::TypeRegistry->optimized_constraints->{$type};
199     return $checker if $checker;
200
201     return sub { blessed($_) && blessed($_) eq $type };
202 }
203
204 sub verify_type_constraint {
205     my $self = shift;
206     local $_ = shift;
207
208     my $type = $self->type_constraint
209         or return 1;
210     my $constraint = $self->find_type_constraint
211         or return 1;
212
213     return 1 if $constraint->($_);
214
215     my $name = $self->name;
216     local $_ = "undef" unless defined($_);
217     Carp::confess("Attribute ($name) does not pass the type constraint because: Validation failed for \'$type\' failed with value $_");
218 }
219
220 1;
221
222 __END__
223
224 =head1 NAME
225
226 Mouse::Attribute - attribute metaclass
227
228 =head1 METHODS
229
230 =head2 new %args -> Mouse::Attribute
231
232 Instantiates a new Mouse::Attribute. Does nothing else.
233
234 =head2 create OwnerClass, AttributeName, %args -> Mouse::Attribute
235
236 Creates a new attribute in OwnerClass. Accessors and helper methods are
237 installed. Some error checking is done.
238
239 =head2 name -> AttributeName
240
241 =head2 class -> OwnerClass
242
243 =head2 default -> Value
244
245 =head2 predicate -> MethodName
246
247 =head2 clearer -> MethodName
248
249 =head2 handles -> { LocalName => RemoteName }
250
251 =head2 weak_ref -> Bool
252
253 =head2 init_arg -> Str
254
255 Informational methods.
256
257 =head2 generate_accessor -> CODE
258
259 Creates a new code reference for the attribute's accessor.
260
261 =head2 generate_predicate -> CODE
262
263 Creates a new code reference for the attribute's predicate.
264
265 =head2 generate_clearer -> CODE
266
267 Creates a new code reference for the attribute's clearer.
268
269 =head2 generate_handles -> { MethodName => CODE }
270
271 Creates a new code reference for each of the attribute's handles methods.
272
273 =cut
274