No more alias_method for roles either. This meant more or less copying
[gitmo/Moose.git] / lib / Moose / Meta / Attribute.pm
1
2 package Moose::Meta::Attribute;
3
4 use strict;
5 use warnings;
6
7 use Scalar::Util 'blessed', 'weaken';
8 use overload     ();
9
10 our $VERSION   = '0.57';
11 our $AUTHORITY = 'cpan:STEVAN';
12
13 use Moose::Meta::Method::Accessor;
14 use Moose::Util ();
15 use Moose::Util::TypeConstraints ();
16
17 use base 'Class::MOP::Attribute';
18
19 # options which are not directly used
20 # but we store them for metadata purposes
21 __PACKAGE__->meta->add_attribute('isa'  => (reader    => '_isa_metadata'));
22 __PACKAGE__->meta->add_attribute('does' => (reader    => '_does_metadata'));
23 __PACKAGE__->meta->add_attribute('is'   => (reader    => '_is_metadata'));
24
25 # these are actual options for the attrs
26 __PACKAGE__->meta->add_attribute('required'   => (reader => 'is_required'      ));
27 __PACKAGE__->meta->add_attribute('lazy'       => (reader => 'is_lazy'          ));
28 __PACKAGE__->meta->add_attribute('lazy_build' => (reader => 'is_lazy_build'    ));
29 __PACKAGE__->meta->add_attribute('coerce'     => (reader => 'should_coerce'    ));
30 __PACKAGE__->meta->add_attribute('weak_ref'   => (reader => 'is_weak_ref'      ));
31 __PACKAGE__->meta->add_attribute('auto_deref' => (reader => 'should_auto_deref'));
32 __PACKAGE__->meta->add_attribute('type_constraint' => (
33     reader    => 'type_constraint',
34     predicate => 'has_type_constraint',
35 ));
36 __PACKAGE__->meta->add_attribute('trigger' => (
37     reader    => 'trigger',
38     predicate => 'has_trigger',
39 ));
40 __PACKAGE__->meta->add_attribute('handles' => (
41     reader    => 'handles',
42     predicate => 'has_handles',
43 ));
44 __PACKAGE__->meta->add_attribute('documentation' => (
45     reader    => 'documentation',
46     predicate => 'has_documentation',
47 ));
48 __PACKAGE__->meta->add_attribute('traits' => (
49     reader    => 'applied_traits',
50     predicate => 'has_applied_traits',
51 ));
52
53 # we need to have a ->does method in here to 
54 # more easily support traits, and the introspection 
55 # of those traits. We extend the does check to look
56 # for metatrait aliases.
57 sub does {
58     my ($self, $role_name) = @_;
59     my $name = eval {
60         Moose::Util::resolve_metatrait_alias(Attribute => $role_name)
61     };
62     return 0 if !defined($name); # failed to load class
63     return Moose::Object::does($self, $name);
64 }
65
66 sub throw_error {
67     my $self = shift;
68     my $class = ( ref $self && $self->associated_class ) || "Moose::Meta::Class";
69     unshift @_, "message" if @_ % 2 == 1;
70     unshift @_, attr => $self if ref $self;
71     unshift @_, $class;
72     goto $class->can("throw_error"); # to avoid incrementing depth by 1
73 }
74
75 sub new {
76     my ($class, $name, %options) = @_;
77     $class->_process_options($name, \%options) unless $options{__hack_no_process_options}; # used from clone()... YECHKKK FIXME ICKY YUCK GROSS
78     return $class->SUPER::new($name, %options);
79 }
80
81 sub interpolate_class_and_new {
82     my ($class, $name, @args) = @_;
83
84     my ( $new_class, @traits ) = $class->interpolate_class(@args);
85     
86     $new_class->new($name, @args, ( scalar(@traits) ? ( traits => \@traits ) : () ) );
87 }
88
89 sub interpolate_class {
90     my ($class, %options) = @_;
91
92     $class = ref($class) || $class;
93
94     if ( my $metaclass_name = delete $options{metaclass} ) {
95         my $new_class = Moose::Util::resolve_metaclass_alias( Attribute => $metaclass_name );
96         
97         if ( $class ne $new_class ) {
98             if ( $new_class->can("interpolate_class") ) {
99                 return $new_class->interpolate_class(%options);
100             } else {
101                 $class = $new_class;
102             }
103         }
104     }
105
106     my @traits;
107
108     if (my $traits = $options{traits}) {
109         if ( @traits = grep { not $class->does($_) } map {
110             Moose::Util::resolve_metatrait_alias( Attribute => $_ )
111                 or
112             $_
113         } @$traits ) {
114             my $anon_class = Moose::Meta::Class->create_anon_class(
115                 superclasses => [ $class ],
116                 roles        => [ @traits ],
117                 cache        => 1,
118             );
119
120             $class = $anon_class->name;
121         }
122     }
123
124     return ( wantarray ? ( $class, @traits ) : $class );
125 }
126
127 # ...
128
129 my @legal_options_for_inheritance = qw(
130     default coerce required 
131     documentation lazy handles 
132     builder type_constraint
133 );
134
135 sub legal_options_for_inheritance { @legal_options_for_inheritance }
136
137 # NOTE/TODO
138 # This method *must* be able to handle 
139 # Class::MOP::Attribute instances as 
140 # well. Yes, I know that is wrong, but 
141 # apparently we didn't realize it was 
142 # doing that and now we have some code 
143 # which is dependent on it. The real 
144 # solution of course is to push this 
145 # feature back up into Class::MOP::Attribute
146 # but I not right now, I am too lazy.
147 # However if you are reading this and 
148 # looking for something to do,.. please 
149 # be my guest.
150 # - stevan
151 sub clone_and_inherit_options {
152     my ($self, %options) = @_;
153     
154     my %copy = %options;
155     
156     my %actual_options;
157     
158     # NOTE:
159     # we may want to extends a Class::MOP::Attribute
160     # in which case we need to be able to use the 
161     # core set of legal options that have always 
162     # been here. But we allows Moose::Meta::Attribute
163     # instances to changes them.
164     # - SL
165     my @legal_options = $self->can('legal_options_for_inheritance')
166         ? $self->legal_options_for_inheritance
167         : @legal_options_for_inheritance;
168     
169     foreach my $legal_option (@legal_options) {
170         if (exists $options{$legal_option}) {
171             $actual_options{$legal_option} = $options{$legal_option};
172             delete $options{$legal_option};
173         }
174     }    
175
176     if ($options{isa}) {
177         my $type_constraint;
178         if (blessed($options{isa}) && $options{isa}->isa('Moose::Meta::TypeConstraint')) {
179             $type_constraint = $options{isa};
180         }
181         else {
182             $type_constraint = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($options{isa});
183             (defined $type_constraint)
184                 || $self->throw_error("Could not find the type constraint '" . $options{isa} . "'", data => $options{isa});
185         }
186
187         $actual_options{type_constraint} = $type_constraint;
188         delete $options{isa};
189     }
190     
191     if ($options{does}) {
192         my $type_constraint;
193         if (blessed($options{does}) && $options{does}->isa('Moose::Meta::TypeConstraint')) {
194             $type_constraint = $options{does};
195         }
196         else {
197             $type_constraint = Moose::Util::TypeConstraints::find_or_create_does_type_constraint($options{does});
198             (defined $type_constraint)
199                 || $self->throw_error("Could not find the type constraint '" . $options{does} . "'", data => $options{does});
200         }
201
202         $actual_options{type_constraint} = $type_constraint;
203         delete $options{does};
204     }    
205
206     # NOTE:
207     # this doesn't apply to Class::MOP::Attributes, 
208     # so we can ignore it for them.
209     # - SL
210     if ($self->can('interpolate_class')) {
211         ( $actual_options{metaclass}, my @traits ) = $self->interpolate_class(%options);
212
213         my %seen;
214         my @all_traits = grep { $seen{$_}++ } @{ $self->applied_traits || [] }, @traits;
215         $actual_options{traits} = \@all_traits if @all_traits;
216
217         delete @options{qw(metaclass traits)};
218     }
219
220     (scalar keys %options == 0)
221         || $self->throw_error("Illegal inherited options => (" . (join ', ' => keys %options) . ")", data => \%options);
222
223
224     $self->clone(%actual_options);
225 }
226
227 sub clone {
228     my ( $self, %params ) = @_;
229
230     my $class = $params{metaclass} || ref $self;
231
232     if ( 0 and $class eq ref $self ) {
233         return $self->SUPER::clone(%params);
234     } else {
235         my ( @init, @non_init );
236
237         foreach my $attr ( grep { $_->has_value($self) } $self->meta->compute_all_applicable_attributes ) {
238             push @{ $attr->has_init_arg ? \@init : \@non_init }, $attr;
239         }
240
241         my %new_params = ( ( map { $_->init_arg => $_->get_value($self) } @init ), %params );
242
243         my $name = delete $new_params{name};
244
245         my $clone = $class->new($name, %new_params, __hack_no_process_options => 1 );
246
247         foreach my $attr ( @non_init ) {
248             $attr->set_value($clone, $attr->get_value($self));
249         }
250
251
252         return $clone;
253     }
254 }
255
256 sub _process_options {
257     my ($class, $name, $options) = @_;
258
259     if (exists $options->{is}) {
260
261         ### -------------------------
262         ## is => ro, writer => _foo    # turns into (reader => foo, writer => _foo) as before
263         ## is => rw, writer => _foo    # turns into (reader => foo, writer => _foo)
264         ## is => rw, accessor => _foo  # turns into (accessor => _foo)
265         ## is => ro, accessor => _foo  # error, accesor is rw
266         ### -------------------------
267         
268         if ($options->{is} eq 'ro') {
269             $class->throw_error("Cannot define an accessor name on a read-only attribute, accessors are read/write", data => $options)
270                 if exists $options->{accessor};
271             $options->{reader} ||= $name;
272         }
273         elsif ($options->{is} eq 'rw') {
274             if ($options->{writer}) {
275                 $options->{reader} ||= $name;
276             }
277             else {
278                 $options->{accessor} ||= $name;
279             }
280         }
281         else {
282             $class->throw_error("I do not understand this option (is => " . $options->{is} . ") on attribute ($name)", data => $options->{is});
283         }
284     }
285
286     if (exists $options->{isa}) {
287         if (exists $options->{does}) {
288             if (eval { $options->{isa}->can('does') }) {
289                 ($options->{isa}->does($options->{does}))
290                     || $class->throw_error("Cannot have an isa option and a does option if the isa does not do the does on attribute ($name)", data => $options);
291             }
292             else {
293                 $class->throw_error("Cannot have an isa option which cannot ->does() on attribute ($name)", data => $options);
294             }
295         }
296
297         # allow for anon-subtypes here ...
298         if (blessed($options->{isa}) && $options->{isa}->isa('Moose::Meta::TypeConstraint')) {
299             $options->{type_constraint} = $options->{isa};
300         }
301         else {
302             $options->{type_constraint} = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($options->{isa});
303         }
304     }
305     elsif (exists $options->{does}) {
306         # allow for anon-subtypes here ...
307         if (blessed($options->{does}) && $options->{does}->isa('Moose::Meta::TypeConstraint')) {
308                 $options->{type_constraint} = $options->{does};
309         }
310         else {
311             $options->{type_constraint} = Moose::Util::TypeConstraints::find_or_create_does_type_constraint($options->{does});
312         }
313     }
314
315     if (exists $options->{coerce} && $options->{coerce}) {
316         (exists $options->{type_constraint})
317             || $class->throw_error("You cannot have coercion without specifying a type constraint on attribute ($name)", data => $options);
318         $class->throw_error("You cannot have a weak reference to a coerced value on attribute ($name)", data => $options)
319             if $options->{weak_ref};
320     }
321
322     if (exists $options->{trigger}) {
323         ('CODE' eq ref $options->{trigger})
324             || $class->throw_error("Trigger must be a CODE ref on attribute ($name)", data => $options->{trigger});
325     }
326
327     if (exists $options->{auto_deref} && $options->{auto_deref}) {
328         (exists $options->{type_constraint})
329             || $class->throw_error("You cannot auto-dereference without specifying a type constraint on attribute ($name)", data => $options);
330         ($options->{type_constraint}->is_a_type_of('ArrayRef') ||
331          $options->{type_constraint}->is_a_type_of('HashRef'))
332             || $class->throw_error("You cannot auto-dereference anything other than a ArrayRef or HashRef on attribute ($name)", data => $options);
333     }
334
335     if (exists $options->{lazy_build} && $options->{lazy_build} == 1) {
336         $class->throw_error("You can not use lazy_build and default for the same attribute ($name)", data => $options)
337             if exists $options->{default};
338         $options->{lazy}      = 1;
339         $options->{required}  = 1;
340         $options->{builder} ||= "_build_${name}";
341         if ($name =~ /^_/) {
342             $options->{clearer}   ||= "_clear${name}";
343             $options->{predicate} ||= "_has${name}";
344         } 
345         else {
346             $options->{clearer}   ||= "clear_${name}";
347             $options->{predicate} ||= "has_${name}";
348         }
349     }
350
351     if (exists $options->{lazy} && $options->{lazy}) {
352         (exists $options->{default} || defined $options->{builder} )
353             || $class->throw_error("You cannot have lazy attribute ($name) without specifying a default value for it", data => $options);
354     }
355
356     if ( $options->{required} && !( ( !exists $options->{init_arg} || defined $options->{init_arg} ) || exists $options->{default} || defined $options->{builder} ) ) {
357         $class->throw_error("You cannot have a required attribute ($name) without a default, builder, or an init_arg", data => $options);
358     }
359
360 }
361
362 sub initialize_instance_slot {
363     my ($self, $meta_instance, $instance, $params) = @_;
364     my $init_arg = $self->init_arg();
365     # try to fetch the init arg from the %params ...
366
367     my $val;
368     my $value_is_set;
369     if ( defined($init_arg) and exists $params->{$init_arg}) {
370         $val = $params->{$init_arg};
371         $value_is_set = 1;    
372     }
373     else {
374         # skip it if it's lazy
375         return if $self->is_lazy;
376         # and die if it's required and doesn't have a default value
377         $self->throw_error("Attribute (" . $self->name . ") is required", object => $instance, data => $params)
378             if $self->is_required && !$self->has_default && !$self->has_builder;
379
380         # if nothing was in the %params, we can use the
381         # attribute's default value (if it has one)
382         if ($self->has_default) {
383             $val = $self->default($instance);
384             $value_is_set = 1;
385         } 
386         elsif ($self->has_builder) {
387             $val = $self->_call_builder($instance);
388             $value_is_set = 1;
389         }
390     }
391
392     return unless $value_is_set;
393
394     if ($self->has_type_constraint) {
395         my $type_constraint = $self->type_constraint;
396         if ($self->should_coerce && $type_constraint->has_coercion) {
397             $val = $type_constraint->coerce($val);
398         }
399         $type_constraint->check($val)
400             || $self->throw_error("Attribute (" 
401                      . $self->name 
402                      . ") does not pass the type constraint because: " 
403                      . $type_constraint->get_message($val), data => $val, object => $instance);
404     }
405
406     $self->set_initial_value($instance, $val);
407     $meta_instance->weaken_slot_value($instance, $self->name)
408         if ref $val && $self->is_weak_ref;
409 }
410
411 sub _call_builder {
412     my ( $self, $instance ) = @_;
413
414     my $builder = $self->builder();
415
416     return $instance->$builder()
417         if $instance->can( $self->builder );
418
419     $self->throw_error(  blessed($instance)
420             . " does not support builder method '"
421             . $self->builder
422             . "' for attribute '"
423             . $self->name
424             . "'",
425             object => $instance,
426      );
427 }
428
429 ## Slot management
430
431 # FIXME:
432 # this duplicates too much code from 
433 # Class::MOP::Attribute, we need to 
434 # refactor these bits eventually.
435 # - SL
436 sub _set_initial_slot_value {
437     my ($self, $meta_instance, $instance, $value) = @_;
438
439     my $slot_name = $self->name;
440
441     return $meta_instance->set_slot_value($instance, $slot_name, $value)
442         unless $self->has_initializer;
443
444     my ($type_constraint, $can_coerce);
445     if ($self->has_type_constraint) {
446         $type_constraint = $self->type_constraint;
447         $can_coerce      = ($self->should_coerce && $type_constraint->has_coercion);
448     }
449
450     my $callback = sub {
451         my $val = shift;
452         if ($type_constraint) {
453             $val = $type_constraint->coerce($val)
454                 if $can_coerce;
455             $type_constraint->check($val)
456                 || $self->throw_error("Attribute (" 
457                          . $slot_name 
458                          . ") does not pass the type constraint because: " 
459                          . $type_constraint->get_message($val), data => $val, object => $instance);
460         }
461         $meta_instance->set_slot_value($instance, $slot_name, $val);
462     };
463     
464     my $initializer = $self->initializer;
465
466     # most things will just want to set a value, so make it first arg
467     $instance->$initializer($value, $callback, $self);
468 }
469
470 sub set_value {
471     my ($self, $instance, @args) = @_;
472     my $value = $args[0];
473
474     my $attr_name = $self->name;
475
476     if ($self->is_required and not @args) {
477         $self->throw_error("Attribute ($attr_name) is required", object => $instance);
478     }
479
480     if ($self->has_type_constraint) {
481
482         my $type_constraint = $self->type_constraint;
483
484         if ($self->should_coerce) {
485             $value = $type_constraint->coerce($value);
486         }        
487         $type_constraint->_compiled_type_constraint->($value)
488             || $self->throw_error("Attribute (" 
489                      . $self->name 
490                      . ") does not pass the type constraint because " 
491                      . $type_constraint->get_message($value), object => $instance, data => $value);
492     }
493
494     my $meta_instance = Class::MOP::Class->initialize(blessed($instance))
495                                          ->get_meta_instance;
496
497     $meta_instance->set_slot_value($instance, $attr_name, $value);
498
499     if (ref $value && $self->is_weak_ref) {
500         $meta_instance->weaken_slot_value($instance, $attr_name);
501     }
502
503     if ($self->has_trigger) {
504         $self->trigger->($instance, $value, $self);
505     }
506 }
507
508 sub get_value {
509     my ($self, $instance) = @_;
510
511     if ($self->is_lazy) {
512         unless ($self->has_value($instance)) {
513             my $value;
514             if ($self->has_default) {
515                 $value = $self->default($instance);
516             } elsif ( $self->has_builder ) {
517                 $value = $self->_call_builder($instance);
518             }
519             if ($self->has_type_constraint) {
520                 my $type_constraint = $self->type_constraint;
521                 $value = $type_constraint->coerce($value)
522                     if ($self->should_coerce);
523                 $type_constraint->check($value) 
524                   || c$self->throw_error("Attribute (" . $self->name
525                       . ") does not pass the type constraint because: "
526                       . $type_constraint->get_message($value), type_constraint => $type_constraint, data => $value);
527             }
528             $self->set_initial_value($instance, $value);
529         }
530     }
531
532     if ($self->should_auto_deref) {
533
534         my $type_constraint = $self->type_constraint;
535
536         if ($type_constraint->is_a_type_of('ArrayRef')) {
537             my $rv = $self->SUPER::get_value($instance);
538             return unless defined $rv;
539             return wantarray ? @{ $rv } : $rv;
540         }
541         elsif ($type_constraint->is_a_type_of('HashRef')) {
542             my $rv = $self->SUPER::get_value($instance);
543             return unless defined $rv;
544             return wantarray ? %{ $rv } : $rv;
545         }
546         else {
547             $self->throw_error("Can not auto de-reference the type constraint '" . $type_constraint->name . "'", object => $instance, type_constraint => $type_constraint);
548         }
549
550     }
551     else {
552
553         return $self->SUPER::get_value($instance);
554     }
555 }
556
557 ## installing accessors
558
559 sub accessor_metaclass { 'Moose::Meta::Method::Accessor' }
560
561 sub install_accessors {
562     my $self = shift;
563     $self->SUPER::install_accessors(@_);
564     $self->install_delegation if $self->has_handles;
565     return;
566 }
567
568 sub install_delegation {
569     my $self = shift;
570
571     # NOTE:
572     # Here we canonicalize the 'handles' option
573     # this will sort out any details and always
574     # return an hash of methods which we want
575     # to delagate to, see that method for details
576     my %handles = $self->_canonicalize_handles;
577
578     # find the accessor method for this attribute
579     my $accessor = $self->_get_delegate_accessor;
580
581     # install the delegation ...
582     my $associated_class = $self->associated_class;
583     foreach my $handle (keys %handles) {
584         my $method_to_call = $handles{$handle};
585         my $class_name = $associated_class->name;
586         my $name = "${class_name}::${handle}";
587
588             (!$associated_class->has_method($handle))
589                 || $self->throw_error("You cannot overwrite a locally defined method ($handle) with a delegation", method_name => $handle);
590
591         # NOTE:
592         # handles is not allowed to delegate
593         # any of these methods, as they will
594         # override the ones in your class, which
595         # is almost certainly not what you want.
596
597         # FIXME warn when $handle was explicitly specified, but not if the source is a regex or something
598         #cluck("Not delegating method '$handle' because it is a core method") and
599         next if $class_name->isa("Moose::Object") and $handle =~ /^BUILD|DEMOLISH$/ || Moose::Object->can($handle);
600
601         if ('CODE' eq ref($method_to_call)) {
602             $associated_class->add_method($handle => Class::MOP::subname($name, $method_to_call));
603         }
604         else {
605             # NOTE:
606             # we used to do a goto here, but the
607             # goto didn't handle failure correctly
608             # (it just returned nothing), so I took 
609             # that out. However, the more I thought
610             # about it, the less I liked it doing 
611             # the goto, and I prefered the act of 
612             # delegation being actually represented
613             # in the stack trace. 
614             # - SL
615             $associated_class->add_method($handle => Class::MOP::subname($name, sub {
616                 my $instance = shift;
617                 my $proxy = $instance->$accessor();
618                 (defined $proxy) 
619                     || $self->throw_error("Cannot delegate $handle to $method_to_call because " . 
620                               "the value of " . $self->name . " is not defined", method_name => $method_to_call, object => $instance);
621                 $proxy->$method_to_call(@_);
622             }));
623         }
624     }    
625 }
626
627 # private methods to help delegation ...
628
629 sub _get_delegate_accessor {
630     my $self = shift;
631     # find the accessor method for this attribute
632     my $accessor = $self->get_read_method_ref;
633     # then unpack it if we need too ...
634     $accessor = $accessor->body if blessed $accessor;    
635     # return the accessor
636     return $accessor;
637 }
638
639 sub _canonicalize_handles {
640     my $self    = shift;
641     my $handles = $self->handles;
642     if (my $handle_type = ref($handles)) {
643         if ($handle_type eq 'HASH') {
644             return %{$handles};
645         }
646         elsif ($handle_type eq 'ARRAY') {
647             return map { $_ => $_ } @{$handles};
648         }
649         elsif ($handle_type eq 'Regexp') {
650             ($self->has_type_constraint)
651                 || $self->throw_error("Cannot delegate methods based on a RegExpr without a type constraint (isa)", data => $handles);
652             return map  { ($_ => $_) }
653                    grep { /$handles/ } $self->_get_delegate_method_list;
654         }
655         elsif ($handle_type eq 'CODE') {
656             return $handles->($self, $self->_find_delegate_metaclass);
657         }
658         else {
659             $self->throw_error("Unable to canonicalize the 'handles' option with $handles", data => $handles);
660         }
661     }
662     else {
663         my $role_meta = eval { $handles->meta };
664         if ($@) {
665             $self->throw_error("Unable to canonicalize the 'handles' option with $handles because : $@", data => $handles, error => $@);
666         }
667
668         (blessed $role_meta && $role_meta->isa('Moose::Meta::Role'))
669             || $self->throw_error("Unable to canonicalize the 'handles' option with $handles because ->meta is not a Moose::Meta::Role", data => $handles);
670
671         return map { $_ => $_ } (
672             $role_meta->get_method_list,
673             $role_meta->get_required_method_list
674         );
675     }
676 }
677
678 sub _find_delegate_metaclass {
679     my $self = shift;
680     if (my $class = $self->_isa_metadata) {
681         # if the class does have
682         # a meta method, use it
683         return $class->meta if $class->can('meta');
684         # otherwise we might be
685         # dealing with a non-Moose
686         # class, and need to make
687         # our own metaclass
688         return Moose::Meta::Class->initialize($class);
689     }
690     elsif (my $role = $self->_does_metadata) {
691         # our role will always have
692         # a meta method
693         return $role->meta;
694     }
695     else {
696         $self->throw_error("Cannot find delegate metaclass for attribute " . $self->name);
697     }
698 }
699
700 sub _get_delegate_method_list {
701     my $self = shift;
702     my $meta = $self->_find_delegate_metaclass;
703     if ($meta->isa('Class::MOP::Class')) {
704         return map  { $_->name }  # NOTE: !never! delegate &meta
705                grep { $_->package_name ne 'Moose::Object' && $_->name ne 'meta' }
706                     $meta->get_all_methods;
707     }
708     elsif ($meta->isa('Moose::Meta::Role')) {
709         return $meta->get_method_list;
710     }
711     else {
712         $self->throw_error("Unable to recognize the delegate metaclass '$meta'", data => $meta);
713     }
714 }
715
716 package Moose::Meta::Attribute::Custom::Moose;
717 sub register_implementation { 'Moose::Meta::Attribute' }
718
719 1;
720
721 __END__
722
723 =pod
724
725 =head1 NAME
726
727 Moose::Meta::Attribute - The Moose attribute metaclass
728
729 =head1 DESCRIPTION
730
731 This is a subclass of L<Class::MOP::Attribute> with Moose specific
732 extensions.
733
734 For the most part, the only time you will ever encounter an
735 instance of this class is if you are doing some serious deep
736 introspection. To really understand this class, you need to refer
737 to the L<Class::MOP::Attribute> documentation.
738
739 =head1 METHODS
740
741 =head2 Overridden methods
742
743 These methods override methods in L<Class::MOP::Attribute> and add
744 Moose specific features. You can safely assume though that they
745 will behave just as L<Class::MOP::Attribute> does.
746
747 =over 4
748
749 =item B<new>
750
751 =item B<clone>
752
753 =item B<does>
754
755 =item B<initialize_instance_slot>
756
757 =item B<install_accessors>
758
759 =item B<install_delegation>
760
761 =item B<accessor_metaclass>
762
763 =item B<get_value>
764
765 =item B<set_value>
766
767   eval { $point->meta->get_attribute('x')->set_value($point, 'fourty-two') };
768   if($@) {
769     print "Oops: $@\n";
770   }
771
772 I<Attribute (x) does not pass the type constraint (Int) with 'fourty-two'>
773
774 Before setting the value, a check is made on the type constraint of
775 the attribute, if it has one, to see if the value passes it. If the
776 value fails to pass, the set operation dies with a L<throw_error>.
777
778 Any coercion to convert values is done before checking the type constraint.
779
780 To check a value against a type constraint before setting it, fetch the
781 attribute instance using L<Class::MOP::Class/find_attribute_by_name>,
782 fetch the type_constraint from the attribute using L<Moose::Meta::Attribute/type_constraint>
783 and call L<Moose::Meta::TypeConstraint/check>. See L<Moose::Cookbook::Basics::Recipe4>
784 for an example.
785
786 =back
787
788 =head2 Additional Moose features
789
790 Moose attributes support type-constraint checking, weak reference
791 creation and type coercion.
792
793 =over 4
794
795 =item B<throw_error>
796
797 Delegates to C<associated_class> or C<Moose::Meta::Class> if there is none.
798
799 =item B<interpolate_class_and_new>
800
801 =item B<interpolate_class>
802
803 When called as a class method causes interpretation of the C<metaclass> and
804 C<traits> options.
805
806 =item B<clone_and_inherit_options>
807
808 This is to support the C<has '+foo'> feature, it clones an attribute
809 from a superclass and allows a very specific set of changes to be made
810 to the attribute.
811
812 =item B<legal_options_for_inheritance>
813
814 Whitelist with options you can change. You can overload it in your custom
815 metaclass to allow your options be inheritable.
816
817 =item B<has_type_constraint>
818
819 Returns true if this meta-attribute has a type constraint.
820
821 =item B<type_constraint>
822
823 A read-only accessor for this meta-attribute's type constraint. For
824 more information on what you can do with this, see the documentation
825 for L<Moose::Meta::TypeConstraint>.
826
827 =item B<has_handles>
828
829 Returns true if this meta-attribute performs delegation.
830
831 =item B<handles>
832
833 This returns the value which was passed into the handles option.
834
835 =item B<is_weak_ref>
836
837 Returns true if this meta-attribute produces a weak reference.
838
839 =item B<is_required>
840
841 Returns true if this meta-attribute is required to have a value.
842
843 =item B<is_lazy>
844
845 Returns true if this meta-attribute should be initialized lazily.
846
847 NOTE: lazy attributes, B<must> have a C<default> or C<builder> field set.
848
849 =item B<is_lazy_build>
850
851 Returns true if this meta-attribute should be initialized lazily through
852 the builder generated by lazy_build. Using C<lazy_build =E<gt> 1> will
853 make your attribute required and lazy. In addition it will set the builder, clearer
854 and predicate options for you using the following convention.
855
856    #If your attribute name starts with an underscore:
857    has '_foo' => (lazy_build => 1);
858    #is the same as
859    has '_foo' => (lazy => 1, required => 1, predicate => '_has_foo', clearer => '_clear_foo', builder => '_build__foo');
860    # or
861    has '_foo' => (lazy => 1, required => 1, predicate => '_has_foo', clearer => '_clear_foo', default => sub{shift->_build__foo});
862
863    #If your attribute name does not start with an underscore:
864    has 'foo' => (lazy_build => 1);
865    #is the same as
866    has 'foo' => (lazy => 1, required => 1, predicate => 'has_foo', clearer => 'clear_foo', builder => '_build_foo');
867    # or
868    has 'foo' => (lazy => 1, required => 1, predicate => 'has_foo', clearer => 'clear_foo', default => sub{shift->_build_foo});
869
870 The reason for the different naming of the C<builder> is that the C<builder>
871 method is a private method while the C<clearer> and C<predicate> methods
872 are public methods.
873
874 NOTE: This means your class should provide a method whose name matches the value
875 of the builder part, in this case _build__foo or _build_foo.
876
877 =item B<should_coerce>
878
879 Returns true if this meta-attribute should perform type coercion.
880
881 =item B<should_auto_deref>
882
883 Returns true if this meta-attribute should perform automatic
884 auto-dereferencing.
885
886 NOTE: This can only be done for attributes whose type constraint is
887 either I<ArrayRef> or I<HashRef>.
888
889 =item B<has_trigger>
890
891 Returns true if this meta-attribute has a trigger set.
892
893 =item B<trigger>
894
895 This is a CODE reference which will be executed every time the
896 value of an attribute is assigned. The CODE ref will get two values,
897 the invocant and the new value. This can be used to handle I<basic>
898 bi-directional relations.
899
900 =item B<documentation>
901
902 This is a string which contains the documentation for this attribute.
903 It serves no direct purpose right now, but it might in the future
904 in some kind of automated documentation system perhaps.
905
906 =item B<has_documentation>
907
908 Returns true if this meta-attribute has any documentation.
909
910 =item B<applied_traits>
911
912 This will return the ARRAY ref of all the traits applied to this 
913 attribute, or if no traits have been applied, it returns C<undef>.
914
915 =item B<has_applied_traits>
916
917 Returns true if this meta-attribute has any traits applied.
918
919 =back
920
921 =head1 BUGS
922
923 All complex software has bugs lurking in it, and this module is no
924 exception. If you find a bug please either email me, or add the bug
925 to cpan-RT.
926
927 =head1 AUTHOR
928
929 Stevan Little E<lt>stevan@iinteractive.comE<gt>
930
931 Yuval Kogman E<lt>nothingmuch@woobling.comE<gt>
932
933 =head1 COPYRIGHT AND LICENSE
934
935 Copyright 2006-2008 by Infinity Interactive, Inc.
936
937 L<http://www.iinteractive.com>
938
939 This library is free software; you can redistribute it and/or modify
940 it under the same terms as Perl itself.
941
942 =cut