implement Maybe
[gitmo/Mouse.git] / lib / Mouse / Meta / Attribute.pm
1 package Mouse::Meta::Attribute;
2 use strict;
3 use warnings;
4 require overload;
5
6 use Carp 'confess';
7 use Scalar::Util ();
8
9 sub new {
10     my ($class, $name, %options) = @_;
11
12     $options{name} = $name;
13
14     $options{init_arg} = $name
15         unless exists $options{init_arg};
16
17     $options{is} ||= '';
18
19     bless \%options, $class;
20 }
21
22 sub name                 { $_[0]->{name}                   }
23 sub associated_class     { $_[0]->{associated_class}       }
24 sub _is_metadata         { $_[0]->{is}                     }
25 sub is_required          { $_[0]->{required}               }
26 sub default              { $_[0]->{default}                }
27 sub is_lazy              { $_[0]->{lazy}                   }
28 sub is_lazy_build        { $_[0]->{lazy_build}             }
29 sub predicate            { $_[0]->{predicate}              }
30 sub clearer              { $_[0]->{clearer}                }
31 sub handles              { $_[0]->{handles}                }
32 sub is_weak_ref          { $_[0]->{weak_ref}               }
33 sub init_arg             { $_[0]->{init_arg}               }
34 sub type_constraint      { $_[0]->{type_constraint}        }
35 sub trigger              { $_[0]->{trigger}                }
36 sub builder              { $_[0]->{builder}                }
37 sub should_auto_deref    { $_[0]->{auto_deref}             }
38 sub should_coerce        { $_[0]->{should_coerce}          }
39 sub find_type_constraint { $_[0]->{find_type_constraint}   }
40
41 sub has_default          { exists $_[0]->{default}         }
42 sub has_predicate        { exists $_[0]->{predicate}       }
43 sub has_clearer          { exists $_[0]->{clearer}         }
44 sub has_handles          { exists $_[0]->{handles}         }
45 sub has_type_constraint  { exists $_[0]->{type_constraint} }
46 sub has_trigger          { exists $_[0]->{trigger}         }
47 sub has_builder          { exists $_[0]->{builder}         }
48
49 sub _create_args {
50     $_[0]->{_create_args} = $_[1] if @_ > 1;
51     $_[0]->{_create_args}
52 }
53
54 sub inlined_name {
55     my $self = shift;
56     my $name = $self->name;
57     my $key   = "'" . $name . "'";
58     return $key;
59 }
60
61 sub generate_accessor {
62     my $attribute = shift;
63
64     my $name          = $attribute->name;
65     my $default       = $attribute->default;
66     my $constraint    = $attribute->find_type_constraint;
67     my $builder       = $attribute->builder;
68     my $trigger       = $attribute->trigger;
69     my $is_weak       = $attribute->is_weak_ref;
70     my $should_deref  = $attribute->should_auto_deref;
71     my $should_coerce = $attribute->should_coerce;
72
73     my $self  = '$_[0]';
74     my $key   = $attribute->inlined_name;
75
76     my $accessor = "sub {\n";
77     if ($attribute->_is_metadata eq 'rw') {
78         $accessor .= 'if (@_ >= 2) {' . "\n";
79
80         my $value = '$_[1]';
81
82         if ($constraint) {
83             $accessor .= 'my $val = ';
84             if ($should_coerce) {
85                 $accessor  .= 'Mouse::Util::TypeConstraints->typecast_constraints("'.$attribute->associated_class->name.'", $attribute->{find_type_constraint}, $attribute->{type_constraint}, '.$value.');';
86             } else {
87                 $accessor .= $value.';';
88             }
89             $accessor .= '
90                 unless ($constraint->($val)) {
91                     $attribute->verify_type_constraint_error($name, $val, $attribute->type_constraint);
92                 }' . "\n";
93             $value = '$val';
94         }
95
96         # if there's nothing left to do for the attribute we can return during
97         # this setter
98         $accessor .= 'return ' if !$is_weak && !$trigger && !$should_deref;
99
100         $accessor .= $self.'->{'.$key.'} = '.$value.';' . "\n";
101
102         if ($is_weak) {
103             $accessor .= 'Scalar::Util::weaken('.$self.'->{'.$key.'}) if ref('.$self.'->{'.$key.'});' . "\n";
104         }
105
106         if ($trigger) {
107             $accessor .= '$trigger->('.$self.', '.$value.');' . "\n";
108         }
109
110         $accessor .= "}\n";
111     }
112     else {
113         $accessor .= 'confess "Cannot assign a value to a read-only accessor" if scalar(@_) >= 2;' . "\n";
114     }
115
116     if ($attribute->is_lazy) {
117         $accessor .= $self.'->{'.$key.'} = ';
118
119         $accessor .= $attribute->has_builder
120                 ? $self.'->$builder'
121                     : ref($default) eq 'CODE'
122                     ? '$default->('.$self.')'
123                     : '$default';
124         $accessor .= ' if !exists '.$self.'->{'.$key.'};' . "\n";
125     }
126
127     if ($should_deref) {
128         my $type_constraint = $attribute->type_constraint;
129         if (!ref($type_constraint) && $type_constraint eq 'ArrayRef') {
130             $accessor .= 'if (wantarray) {
131                 return @{ '.$self.'->{'.$key.'} || [] };
132             }';
133         }
134         else {
135             $accessor .= 'if (wantarray) {
136                 return %{ '.$self.'->{'.$key.'} || {} };
137             }';
138         }
139     }
140
141     $accessor .= 'return '.$self.'->{'.$key.'};
142     }';
143
144     my $sub = eval $accessor;
145     confess $@ if $@;
146     return $sub;
147 }
148
149 sub generate_predicate {
150     my $attribute = shift;
151     my $key = $attribute->inlined_name;
152
153     my $predicate = 'sub { exists($_[0]->{'.$key.'}) }';
154
155     my $sub = eval $predicate;
156     confess $@ if $@;
157     return $sub;
158 }
159
160 sub generate_clearer {
161     my $attribute = shift;
162     my $key = $attribute->inlined_name;
163
164     my $clearer = 'sub { delete($_[0]->{'.$key.'}) }';
165
166     my $sub = eval $clearer;
167     confess $@ if $@;
168     return $sub;
169 }
170
171 sub generate_handles {
172     my $attribute = shift;
173     my $reader = $attribute->name;
174     my %handles = $attribute->_canonicalize_handles($attribute->handles);
175
176     my %method_map;
177
178     for my $local_method (keys %handles) {
179         my $remote_method = $handles{$local_method};
180
181         my $method = 'sub {
182             my $self = shift;
183             $self->'.$reader.'->'.$remote_method.'(@_)
184         }';
185
186         $method_map{$local_method} = eval $method;
187         confess $@ if $@;
188     }
189
190     return \%method_map;
191 }
192
193 my $optimized_constraints;
194 sub _build_type_constraint {
195     my $spec = shift;
196     $optimized_constraints ||= Mouse::Util::TypeConstraints->optimized_constraints;
197     my $code;
198     if ($spec =~ /^([^\[]+)\[(.+)\]$/) {
199         # parameterized
200         my $constraint = $1;
201         my $param      = $2;
202         my $parent;
203         if ($constraint eq 'Maybe') {
204             $parent = _build_type_constraint('Undef');
205         } else {
206             $parent = _build_type_constraint($constraint);
207         }
208         my $child = _build_type_constraint($param);
209         if ($constraint eq 'ArrayRef') {
210             my $code_str = 
211                 "sub {\n" .
212                 "    if (\$parent->(\$_)) {\n" .
213                 "        foreach my \$e (@\$_) {\n" .
214                 "            local \$_ = \$e;\n" .
215                 "            return () unless \$child->(\$_);\n" .
216                 "        }\n" .
217                 "        return 1;\n" .
218                 "    }\n" .
219                 "    return ();\n" .
220                 "};\n"
221             ;
222             $code = eval $code_str or Carp::confess($@);
223         } elsif ($constraint eq 'HashRef') {
224             my $code_str = 
225                 "sub {\n" .
226                 "    if (\$parent->(\$_)) {\n" .
227                 "        foreach my \$e (values %\$_) {\n" .
228                 "            local \$_ = \$e;\n" .
229                 "            return () unless \$child->(\$_);\n" .
230                 "        }\n" .
231                 "        return 1;\n" .
232                 "    }\n" .
233                 "    return ();\n" .
234                 "};\n"
235             ;
236             $code = eval $code_str or Carp::confess($@);
237         } elsif ($constraint eq 'Maybe') {
238             my $code_str =
239                 "sub {\n" .
240                 "    return \$child->(\$_) || \$parent->(\$_);\n" .
241                 "};\n"
242             ;
243             $code = eval $code_str or Carp::confess($@);
244         } else {
245             Carp::confess("Support for parameterized types other than ArrayRef or HashRef is not implemented yet");
246         }
247         $optimized_constraints->{$spec} = $code;
248     } else {
249         $code = $optimized_constraints->{ $spec };
250         if (! $code) {
251             $code = sub { Scalar::Util::blessed($_[0]) && $_[0]->isa($spec) };
252             $optimized_constraints->{$spec} = $code;
253         }
254     }
255     return $code;
256 }
257
258 sub create {
259     my ($self, $class, $name, %args) = @_;
260
261     $args{name} = $name;
262     $args{associated_class} = $class;
263
264     %args = $self->canonicalize_args($name, %args);
265     $self->validate_args($name, \%args);
266
267     $args{should_coerce} = delete $args{coerce}
268         if exists $args{coerce};
269
270     if (exists $args{isa}) {
271         confess "Got isa => $args{isa}, but Mouse does not yet support parameterized types for containers other than ArrayRef and HashRef (rt.cpan.org #39795)"
272             if $args{isa} =~ /^([^\[]+)\[.+\]$/ &&
273                $1 ne 'ArrayRef' &&
274                $1 ne 'HashRef'  &&
275                $1 ne 'Maybe'
276         ;
277
278         my $type_constraint = delete $args{isa};
279         $type_constraint =~ s/\s//g;
280         my @type_constraints = split /\|/, $type_constraint;
281
282         my $code;
283         if (@type_constraints == 1) {
284             $code = _build_type_constraint($type_constraints[0]);
285             $args{type_constraint} = $type_constraints[0];
286         } else {
287             my @code_list = map {
288                 _build_type_constraint($_)
289             } @type_constraints;
290             $code = sub {
291                 local $_ = $_[0];
292                 for my $code (@code_list) {
293                     return 1 if $code->($_);
294                 }
295                 return 0;
296             };
297             $args{type_constraint} = \@type_constraints;
298         }
299         $args{find_type_constraint} = $code;
300     }
301
302     my $attribute = $self->new($name, %args);
303
304     $attribute->_create_args(\%args);
305
306     $class->add_attribute($attribute);
307
308     # install an accessor
309     if ($attribute->_is_metadata eq 'rw' || $attribute->_is_metadata eq 'ro') {
310         my $accessor = $attribute->generate_accessor;
311         $class->add_method($name => $accessor);
312     }
313
314     for my $method (qw/predicate clearer/) {
315         my $predicate = "has_$method";
316         if ($attribute->$predicate) {
317             my $generator = "generate_$method";
318             my $coderef = $attribute->$generator;
319             $class->add_method($attribute->$method => $coderef);
320         }
321     }
322
323     if ($attribute->has_handles) {
324         my $method_map = $attribute->generate_handles;
325         for my $method_name (keys %$method_map) {
326             $class->add_method($method_name => $method_map->{$method_name});
327         }
328     }
329
330     return $attribute;
331 }
332
333 sub canonicalize_args {
334     my $self = shift;
335     my $name = shift;
336     my %args = @_;
337
338     if ($args{lazy_build}) {
339         $args{lazy}      = 1;
340         $args{required}  = 1;
341         $args{builder}   = "_build_${name}"
342             if !exists($args{builder});
343         if ($name =~ /^_/) {
344             $args{clearer}   = "_clear${name}" if !exists($args{clearer});
345             $args{predicate} = "_has${name}" if !exists($args{predicate});
346         }
347         else {
348             $args{clearer}   = "clear_${name}" if !exists($args{clearer});
349             $args{predicate} = "has_${name}" if !exists($args{predicate});
350         }
351     }
352
353     return %args;
354 }
355
356 sub validate_args {
357     my $self = shift;
358     my $name = shift;
359     my $args = shift;
360
361     confess "You can not use lazy_build and default for the same attribute ($name)"
362         if $args->{lazy_build} && exists $args->{default};
363
364     confess "You cannot have lazy attribute ($name) without specifying a default value for it"
365         if $args->{lazy}
366         && !exists($args->{default})
367         && !exists($args->{builder});
368
369     confess "References are not allowed as default values, you must wrap the default of '$name' in a CODE reference (ex: sub { [] } and not [])"
370         if ref($args->{default})
371         && ref($args->{default}) ne 'CODE';
372
373     confess "You cannot auto-dereference without specifying a type constraint on attribute ($name)"
374         if $args->{auto_deref} && !exists($args->{isa});
375
376     confess "You cannot auto-dereference anything other than a ArrayRef or HashRef on attribute ($name)"
377         if $args->{auto_deref}
378         && $args->{isa} ne 'ArrayRef'
379         && $args->{isa} ne 'HashRef';
380
381     if ($args->{trigger}) {
382         if (ref($args->{trigger}) eq 'HASH') {
383             Carp::carp "HASH-based form of trigger has been removed. Only the coderef form of triggers are now supported.";
384         }
385
386         confess "Trigger must be a CODE ref on attribute ($name)"
387             if ref($args->{trigger}) ne 'CODE';
388     }
389
390     return 1;
391 }
392
393 sub verify_against_type_constraint {
394     return 1 unless $_[0]->{type_constraint};
395
396     local $_ = $_[1];
397     return 1 if $_[0]->{find_type_constraint}->($_);
398
399     my $self = shift;
400     $self->verify_type_constraint_error($self->name, $_, $self->type_constraint);
401 }
402
403 sub verify_type_constraint_error {
404     my($self, $name, $value, $type) = @_;
405     $type = ref($type) eq 'ARRAY' ? join '|', @{ $type } : $type;
406     my $display = defined($value) ? overload::StrVal($value) : 'undef';
407     Carp::confess("Attribute ($name) does not pass the type constraint because: Validation failed for \'$type\' failed with value $display");
408 }
409
410 sub coerce_constraint { ## my($self, $value) = @_;
411     my $type = $_[0]->{type_constraint}
412         or return $_[1];
413     return Mouse::Util::TypeConstraints->typecast_constraints($_[0]->associated_class->name, $_[0]->find_type_constraint, $type, $_[1]);
414 }
415
416 sub _canonicalize_handles {
417     my $self    = shift;
418     my $handles = shift;
419
420     if (ref($handles) eq 'HASH') {
421         return %$handles;
422     }
423     elsif (ref($handles) eq 'ARRAY') {
424         return map { $_ => $_ } @$handles;
425     }
426     else {
427         confess "Unable to canonicalize the 'handles' option with $handles";
428     }
429 }
430
431 sub clone_parent {
432     my $self  = shift;
433     my $class = shift;
434     my $name  = shift;
435     my %args  = ($self->get_parent_args($class, $name), @_);
436
437     $self->create($class, $name, %args);
438 }
439
440 sub get_parent_args {
441     my $self  = shift;
442     my $class = shift;
443     my $name  = shift;
444
445     for my $super ($class->linearized_isa) {
446         my $super_attr = $super->can("meta") && $super->meta->get_attribute($name)
447             or next;
448         return %{ $super_attr->_create_args };
449     }
450
451     confess "Could not find an attribute by the name of '$name' to inherit from";
452 }
453
454 1;
455
456 __END__
457
458 =head1 NAME
459
460 Mouse::Meta::Attribute - attribute metaclass
461
462 =head1 METHODS
463
464 =head2 new %args -> Mouse::Meta::Attribute
465
466 Instantiates a new Mouse::Meta::Attribute. Does nothing else.
467
468 =head2 create OwnerClass, AttributeName, %args -> Mouse::Meta::Attribute
469
470 Creates a new attribute in OwnerClass. Accessors and helper methods are
471 installed. Some error checking is done.
472
473 =head2 name -> AttributeName
474
475 =head2 associated_class -> OwnerClass
476
477 =head2 is_required -> Bool
478
479 =head2 default -> Item
480
481 =head2 has_default -> Bool
482
483 =head2 is_lazy -> Bool
484
485 =head2 predicate -> MethodName | Undef
486
487 =head2 has_predicate -> Bool
488
489 =head2 clearer -> MethodName | Undef
490
491 =head2 has_clearer -> Bool
492
493 =head2 handles -> { LocalName => RemoteName }
494
495 =head2 has_handles -> Bool
496
497 =head2 is_weak_ref -> Bool
498
499 =head2 init_arg -> Str
500
501 =head2 type_constraint -> Str
502
503 =head2 has_type_constraint -> Bool
504
505 =head2 trigger => CODE | Undef
506
507 =head2 has_trigger -> Bool
508
509 =head2 builder => MethodName | Undef
510
511 =head2 has_builder -> Bool
512
513 =head2 is_lazy_build => Bool
514
515 =head2 should_auto_deref -> Bool
516
517 Informational methods.
518
519 =head2 generate_accessor -> CODE
520
521 Creates a new code reference for the attribute's accessor.
522
523 =head2 generate_predicate -> CODE
524
525 Creates a new code reference for the attribute's predicate.
526
527 =head2 generate_clearer -> CODE
528
529 Creates a new code reference for the attribute's clearer.
530
531 =head2 generate_handles -> { MethodName => CODE }
532
533 Creates a new code reference for each of the attribute's handles methods.
534
535 =head2 find_type_constraint -> CODE
536
537 Returns a code reference which can be used to check that a given value passes
538 this attribute's type constraint;
539
540 =head2 verify_against_type_constraint Item -> 1 | ERROR
541
542 Checks that the given value passes this attribute's type constraint. Returns 1
543 on success, otherwise C<confess>es.
544
545 =head2 canonicalize_args Name, %args -> %args
546
547 Canonicalizes some arguments to create. In particular, C<lazy_build> is
548 canonicalized into C<lazy>, C<builder>, etc.
549
550 =head2 validate_args Name, \%args -> 1 | ERROR
551
552 Checks that the arguments to create the attribute (ie those specified by
553 C<has>) are valid.
554
555 =head2 clone_parent OwnerClass, AttributeName, %args -> Mouse::Meta::Attribute
556
557 Creates a new attribute in OwnerClass, inheriting options from parent classes.
558 Accessors and helper methods are installed. Some error checking is done.
559
560 =head2 get_parent_args OwnerClass, AttributeName -> Hash
561
562 Returns the options that the parent class of C<OwnerClass> used for attribute
563 C<AttributeName>.
564
565 =cut
566