56f3bc90b43ffd0ce47748d09ff9caa9079c6d03
[gitmo/Moose.git] / lib / Moose / Meta / Attribute.pm
1
2 package Moose::Meta::Attribute;
3
4 use strict;
5 use warnings;
6
7 use B ();
8 use Class::Load qw(is_class_loaded load_class);
9 use Scalar::Util 'blessed', 'weaken';
10 use List::MoreUtils 'any';
11 use Try::Tiny;
12 use overload     ();
13
14 use Moose::Deprecated;
15 use Moose::Meta::Method::Accessor;
16 use Moose::Meta::Method::Delegation;
17 use Moose::Util ();
18 use Moose::Util::TypeConstraints ();
19 use Class::MOP::MiniTrait;
20
21 use base 'Class::MOP::Attribute', 'Moose::Meta::Mixin::AttributeCore';
22
23 Class::MOP::MiniTrait::apply(__PACKAGE__, 'Moose::Meta::Object::Trait');
24
25 __PACKAGE__->meta->add_attribute('traits' => (
26     reader    => 'applied_traits',
27     predicate => 'has_applied_traits',
28     Class::MOP::_definition_context(),
29 ));
30
31 # we need to have a ->does method in here to
32 # more easily support traits, and the introspection
33 # of those traits. We extend the does check to look
34 # for metatrait aliases.
35 sub does {
36     my ($self, $role_name) = @_;
37     my $name = try {
38         Moose::Util::resolve_metatrait_alias(Attribute => $role_name)
39     };
40     return 0 if !defined($name); # failed to load class
41     return $self->Moose::Object::does($name);
42 }
43
44 sub _error_thrower {
45     my $self = shift;
46     require Moose::Meta::Class;
47     ( ref $self && $self->associated_class ) || "Moose::Meta::Class";
48 }
49
50 sub throw_error {
51     my $self = shift;
52     my $inv = $self->_error_thrower;
53     unshift @_, "message" if @_ % 2 == 1;
54     unshift @_, attr => $self if ref $self;
55     unshift @_, $inv;
56     my $handler = $inv->can("throw_error"); # to avoid incrementing depth by 1
57     goto $handler;
58 }
59
60 sub _inline_throw_error {
61     my ( $self, $msg, $args ) = @_;
62
63     my $inv = $self->_error_thrower;
64     # XXX ugh
65     $inv = 'Moose::Meta::Class' unless $inv->can('_inline_throw_error');
66
67     # XXX ugh ugh UGH
68     my $class = $self->associated_class;
69     if ($class) {
70         my $class_name = B::perlstring($class->name);
71         my $attr_name = B::perlstring($self->name);
72         $args = 'attr => Class::MOP::class_of(' . $class_name . ')'
73               . '->find_attribute_by_name(' . $attr_name . '), '
74               . (defined $args ? $args : '');
75     }
76
77     return $inv->_inline_throw_error($msg, $args)
78 }
79
80 sub new {
81     my ($class, $name, %options) = @_;
82     $class->_process_options($name, \%options) unless $options{__hack_no_process_options}; # used from clone()... YECHKKK FIXME ICKY YUCK GROSS
83
84     delete $options{__hack_no_process_options};
85
86     my %attrs =
87         ( map { $_ => 1 }
88           grep { defined }
89           map { $_->init_arg() }
90           $class->meta()->get_all_attributes()
91         );
92
93     my @bad = sort grep { ! $attrs{$_} }  keys %options;
94
95     if (@bad)
96     {
97         Carp::cluck "Found unknown argument(s) passed to '$name' attribute constructor in '$class': @bad";
98     }
99
100     return $class->SUPER::new($name, %options);
101 }
102
103 sub interpolate_class_and_new {
104     my ($class, $name, %args) = @_;
105
106     my ( $new_class, @traits ) = $class->interpolate_class(\%args);
107
108     $new_class->new($name, %args, ( scalar(@traits) ? ( traits => \@traits ) : () ) );
109 }
110
111 sub interpolate_class {
112     my ($class, $options) = @_;
113
114     $class = ref($class) || $class;
115
116     if ( my $metaclass_name = delete $options->{metaclass} ) {
117         my $new_class = Moose::Util::resolve_metaclass_alias( Attribute => $metaclass_name );
118
119         if ( $class ne $new_class ) {
120             if ( $new_class->can("interpolate_class") ) {
121                 return $new_class->interpolate_class($options);
122             } else {
123                 $class = $new_class;
124             }
125         }
126     }
127
128     my @traits;
129
130     if (my $traits = $options->{traits}) {
131         my $i = 0;
132         my $has_foreign_options = 0;
133
134         while ($i < @$traits) {
135             my $trait = $traits->[$i++];
136             next if ref($trait); # options to a trait we discarded
137
138             $trait = Moose::Util::resolve_metatrait_alias(Attribute => $trait)
139                   || $trait;
140
141             next if $class->does($trait);
142
143             push @traits, $trait;
144
145             # are there options?
146             if ($traits->[$i] && ref($traits->[$i])) {
147                 $has_foreign_options = 1
148                     if any { $_ ne '-alias' && $_ ne '-excludes' } keys %{ $traits->[$i] };
149
150                 push @traits, $traits->[$i++];
151             }
152         }
153
154         if (@traits) {
155             my %options = (
156                 superclasses => [ $class ],
157                 roles        => [ @traits ],
158             );
159
160             if ($has_foreign_options) {
161                 $options{weaken} = 0;
162             }
163             else {
164                 $options{cache} = 1;
165             }
166
167             my $anon_class = Moose::Meta::Class->create_anon_class(%options);
168             $class = $anon_class->name;
169         }
170     }
171
172     return ( wantarray ? ( $class, @traits ) : $class );
173 }
174
175 # ...
176
177 # method-generating options shouldn't be overridden
178 sub illegal_options_for_inheritance {
179     qw(reader writer accessor clearer predicate)
180 }
181
182 # NOTE/TODO
183 # This method *must* be able to handle
184 # Class::MOP::Attribute instances as
185 # well. Yes, I know that is wrong, but
186 # apparently we didn't realize it was
187 # doing that and now we have some code
188 # which is dependent on it. The real
189 # solution of course is to push this
190 # feature back up into Class::MOP::Attribute
191 # but I not right now, I am too lazy.
192 # However if you are reading this and
193 # looking for something to do,.. please
194 # be my guest.
195 # - stevan
196 sub clone_and_inherit_options {
197     my ($self, %options) = @_;
198
199     # NOTE:
200     # we may want to extends a Class::MOP::Attribute
201     # in which case we need to be able to use the
202     # core set of legal options that have always
203     # been here. But we allows Moose::Meta::Attribute
204     # instances to changes them.
205     # - SL
206     my @illegal_options = $self->can('illegal_options_for_inheritance')
207         ? $self->illegal_options_for_inheritance
208         : ();
209
210     my @found_illegal_options = grep { exists $options{$_} && exists $self->{$_} ? $_ : undef } @illegal_options;
211     (scalar @found_illegal_options == 0)
212         || $self->throw_error("Illegal inherited options => (" . (join ', ' => @found_illegal_options) . ")", data => \%options);
213
214     if ($options{isa}) {
215         my $type_constraint;
216         if (blessed($options{isa}) && $options{isa}->isa('Moose::Meta::TypeConstraint')) {
217             $type_constraint = $options{isa};
218         }
219         else {
220             $type_constraint = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($options{isa}, { package_defined_in => $options{definition_context}->{package} });
221             (defined $type_constraint)
222                 || $self->throw_error("Could not find the type constraint '" . $options{isa} . "'", data => $options{isa});
223         }
224
225         $options{type_constraint} = $type_constraint;
226     }
227
228     if ($options{does}) {
229         my $type_constraint;
230         if (blessed($options{does}) && $options{does}->isa('Moose::Meta::TypeConstraint')) {
231             $type_constraint = $options{does};
232         }
233         else {
234             $type_constraint = Moose::Util::TypeConstraints::find_or_create_does_type_constraint($options{does}, { package_defined_in => $options{definition_context}->{package} });
235             (defined $type_constraint)
236                 || $self->throw_error("Could not find the type constraint '" . $options{does} . "'", data => $options{does});
237         }
238
239         $options{type_constraint} = $type_constraint;
240     }
241
242     # NOTE:
243     # this doesn't apply to Class::MOP::Attributes,
244     # so we can ignore it for them.
245     # - SL
246     if ($self->can('interpolate_class')) {
247         ( $options{metaclass}, my @traits ) = $self->interpolate_class(\%options);
248
249         my %seen;
250         my @all_traits = grep { $seen{$_}++ } @{ $self->applied_traits || [] }, @traits;
251         $options{traits} = \@all_traits if @all_traits;
252     }
253
254     # This method can be called on a CMOP::Attribute object, so we need to
255     # make sure we can call this method.
256     $self->_process_lazy_build_option( $self->name, \%options )
257         if $self->can('_process_lazy_build_option');
258
259     $self->clone(%options);
260 }
261
262 sub clone {
263     my ( $self, %params ) = @_;
264
265     my $class = delete $params{metaclass} || ref $self;
266
267     my ( @init, @non_init );
268
269     foreach my $attr ( grep { $_->has_value($self) } Class::MOP::class_of($self)->get_all_attributes ) {
270         push @{ $attr->has_init_arg ? \@init : \@non_init }, $attr;
271     }
272
273     my %new_params = ( ( map { $_->init_arg => $_->get_value($self) } @init ), %params );
274
275     my $name = delete $new_params{name};
276
277     my $clone = $class->new($name, %new_params, __hack_no_process_options => 1 );
278
279     foreach my $attr ( @non_init ) {
280         $attr->set_value($clone, $attr->get_value($self));
281     }
282
283     return $clone;
284 }
285
286 sub _process_options {
287     my ( $class, $name, $options ) = @_;
288
289     $class->_process_is_option( $name, $options );
290     $class->_process_isa_option( $name, $options );
291     $class->_process_does_option( $name, $options );
292     $class->_process_coerce_option( $name, $options );
293     $class->_process_trigger_option( $name, $options );
294     $class->_process_auto_deref_option( $name, $options );
295     $class->_process_lazy_build_option( $name, $options );
296     $class->_process_lazy_option( $name, $options );
297     $class->_process_required_option( $name, $options );
298 }
299
300 sub _process_is_option {
301     my ( $class, $name, $options ) = @_;
302
303     return unless $options->{is};
304
305     ### -------------------------
306     ## is => ro, writer => _foo    # turns into (reader => foo, writer => _foo) as before
307     ## is => rw, writer => _foo    # turns into (reader => foo, writer => _foo)
308     ## is => rw, accessor => _foo  # turns into (accessor => _foo)
309     ## is => ro, accessor => _foo  # error, accesor is rw
310     ### -------------------------
311
312     if ( $options->{is} eq 'ro' ) {
313         $class->throw_error(
314             "Cannot define an accessor name on a read-only attribute, accessors are read/write",
315             data => $options )
316             if exists $options->{accessor};
317         $options->{reader} ||= $name;
318     }
319     elsif ( $options->{is} eq 'rw' ) {
320         if ( $options->{writer} ) {
321             $options->{reader} ||= $name;
322         }
323         else {
324             $options->{accessor} ||= $name;
325         }
326     }
327     elsif ( $options->{is} eq 'bare' ) {
328         return;
329         # do nothing, but don't complain (later) about missing methods
330     }
331     else {
332         $class->throw_error( "I do not understand this option (is => "
333                 . $options->{is}
334                 . ") on attribute ($name)", data => $options->{is} );
335     }
336 }
337
338 sub _process_isa_option {
339     my ( $class, $name, $options ) = @_;
340
341     return unless exists $options->{isa};
342
343     if ( exists $options->{does} ) {
344         if ( try { $options->{isa}->can('does') } ) {
345             ( $options->{isa}->does( $options->{does} ) )
346                 || $class->throw_error(
347                 "Cannot have an isa option and a does option if the isa does not do the does on attribute ($name)",
348                 data => $options );
349         }
350         else {
351             $class->throw_error(
352                 "Cannot have an isa option which cannot ->does() on attribute ($name)",
353                 data => $options );
354         }
355     }
356
357     # allow for anon-subtypes here ...
358     if ( blessed( $options->{isa} )
359         && $options->{isa}->isa('Moose::Meta::TypeConstraint') ) {
360         $options->{type_constraint} = $options->{isa};
361     }
362     else {
363         $options->{type_constraint}
364             = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint(
365             $options->{isa},
366             { package_defined_in => $options->{definition_context}->{package} }
367         );
368     }
369 }
370
371 sub _process_does_option {
372     my ( $class, $name, $options ) = @_;
373
374     return unless exists $options->{does} && ! exists $options->{isa};
375
376     # allow for anon-subtypes here ...
377     if ( blessed( $options->{does} )
378         && $options->{does}->isa('Moose::Meta::TypeConstraint') ) {
379         $options->{type_constraint} = $options->{does};
380     }
381     else {
382         $options->{type_constraint}
383             = Moose::Util::TypeConstraints::find_or_create_does_type_constraint(
384             $options->{does},
385             { package_defined_in => $options->{definition_context}->{package} }
386         );
387     }
388 }
389
390 sub _process_coerce_option {
391     my ( $class, $name, $options ) = @_;
392
393     return unless $options->{coerce};
394
395     ( exists $options->{type_constraint} )
396         || $class->throw_error(
397         "You cannot have coercion without specifying a type constraint on attribute ($name)",
398         data => $options );
399
400     $class->throw_error(
401         "You cannot have a weak reference to a coerced value on attribute ($name)",
402         data => $options )
403         if $options->{weak_ref};
404
405     unless ( $options->{type_constraint}->has_coercion ) {
406         my $type = $options->{type_constraint}->name;
407
408         Moose::Deprecated::deprecated(
409             feature => 'coerce without coercion',
410             message =>
411                 "You cannot coerce an attribute ($name) unless its type ($type) has a coercion"
412         );
413     }
414 }
415
416 sub _process_trigger_option {
417     my ( $class, $name, $options ) = @_;
418
419     return unless exists $options->{trigger};
420
421     ( 'CODE' eq ref $options->{trigger} )
422         || $class->throw_error("Trigger must be a CODE ref on attribute ($name)", data => $options->{trigger});
423 }
424
425 sub _process_auto_deref_option {
426     my ( $class, $name, $options ) = @_;
427
428     return unless $options->{auto_deref};
429
430     ( exists $options->{type_constraint} )
431         || $class->throw_error(
432         "You cannot auto-dereference without specifying a type constraint on attribute ($name)",
433         data => $options );
434
435     ( $options->{type_constraint}->is_a_type_of('ArrayRef')
436       || $options->{type_constraint}->is_a_type_of('HashRef') )
437         || $class->throw_error(
438         "You cannot auto-dereference anything other than a ArrayRef or HashRef on attribute ($name)",
439         data => $options );
440 }
441
442 sub _process_lazy_build_option {
443     my ( $class, $name, $options ) = @_;
444
445     return unless $options->{lazy_build};
446
447     $class->throw_error(
448         "You can not use lazy_build and default for the same attribute ($name)",
449         data => $options )
450         if exists $options->{default};
451
452     $options->{lazy} = 1;
453     $options->{builder} ||= "_build_${name}";
454
455     if ( $name =~ /^_/ ) {
456         $options->{clearer}   ||= "_clear${name}";
457         $options->{predicate} ||= "_has${name}";
458     }
459     else {
460         $options->{clearer}   ||= "clear_${name}";
461         $options->{predicate} ||= "has_${name}";
462     }
463 }
464
465 sub _process_lazy_option {
466     my ( $class, $name, $options ) = @_;
467
468     return unless $options->{lazy};
469
470     ( exists $options->{default} || defined $options->{builder} )
471         || $class->throw_error(
472         "You cannot have a lazy attribute ($name) without specifying a default value for it",
473         data => $options );
474 }
475
476 sub _process_required_option {
477     my ( $class, $name, $options ) = @_;
478
479     if (
480         $options->{required}
481         && !(
482             ( !exists $options->{init_arg} || defined $options->{init_arg} )
483             || exists $options->{default}
484             || defined $options->{builder}
485         )
486         ) {
487         $class->throw_error(
488             "You cannot have a required attribute ($name) without a default, builder, or an init_arg",
489             data => $options );
490     }
491 }
492
493 sub initialize_instance_slot {
494     my ($self, $meta_instance, $instance, $params) = @_;
495     my $init_arg = $self->init_arg();
496     # try to fetch the init arg from the %params ...
497
498     my $val;
499     my $value_is_set;
500     if ( defined($init_arg) and exists $params->{$init_arg}) {
501         $val = $params->{$init_arg};
502         $value_is_set = 1;
503     }
504     else {
505         # skip it if it's lazy
506         return if $self->is_lazy;
507         # and die if it's required and doesn't have a default value
508         $self->throw_error("Attribute (" . $self->name . ") is required", object => $instance, data => $params)
509             if $self->is_required && !$self->has_default && !$self->has_builder;
510
511         # if nothing was in the %params, we can use the
512         # attribute's default value (if it has one)
513         if ($self->has_default) {
514             $val = $self->default($instance);
515             $value_is_set = 1;
516         }
517         elsif ($self->has_builder) {
518             $val = $self->_call_builder($instance);
519             $value_is_set = 1;
520         }
521     }
522
523     return unless $value_is_set;
524
525     $val = $self->_coerce_and_verify( $val, $instance );
526
527     $self->set_initial_value($instance, $val);
528
529     if ( ref $val && $self->is_weak_ref ) {
530         $self->_weaken_value($instance);
531     }
532 }
533
534 sub _call_builder {
535     my ( $self, $instance ) = @_;
536
537     my $builder = $self->builder();
538
539     return $instance->$builder()
540         if $instance->can( $self->builder );
541
542     $self->throw_error(  blessed($instance)
543             . " does not support builder method '"
544             . $self->builder
545             . "' for attribute '"
546             . $self->name
547             . "'",
548             object => $instance,
549      );
550 }
551
552 ## Slot management
553
554 sub _make_initializer_writer_callback {
555     my $self = shift;
556     my ($meta_instance, $instance, $slot_name) = @_;
557     my $old_callback = $self->SUPER::_make_initializer_writer_callback(@_);
558     return sub {
559         $old_callback->($self->_coerce_and_verify($_[0], $instance));
560     };
561 }
562
563 sub set_value {
564     my ($self, $instance, @args) = @_;
565     my $value = $args[0];
566
567     my $attr_name = quotemeta($self->name);
568
569     if ($self->is_required and not @args) {
570         $self->throw_error("Attribute ($attr_name) is required", object => $instance);
571     }
572
573     $value = $self->_coerce_and_verify( $value, $instance );
574
575     my @old;
576     if ( $self->has_trigger && $self->has_value($instance) ) {
577         @old = $self->get_value($instance, 'for trigger');
578     }
579
580     $self->SUPER::set_value($instance, $value);
581
582     if ( ref $value && $self->is_weak_ref ) {
583         $self->_weaken_value($instance);
584     }
585
586     if ($self->has_trigger) {
587         $self->trigger->($instance, $value, @old);
588     }
589 }
590
591 sub _inline_set_value {
592     my $self = shift;
593     my ($instance, $value, $tc, $coercion, $message, $for_constructor) = @_;
594
595     my $old     = '@old';
596     my $copy    = '$val';
597     $tc       ||= '$type_constraint';
598     $coercion ||= '$type_coercion';
599     $message  ||= '$type_message';
600
601     my @code;
602     if ($self->_writer_value_needs_copy) {
603         push @code, $self->_inline_copy_value($value, $copy);
604         $value = $copy;
605     }
606
607     # constructors already handle required checks
608     push @code, $self->_inline_check_required
609         unless $for_constructor;
610
611     push @code, $self->_inline_tc_code($value, $tc, $coercion, $message);
612
613     # constructors do triggers all at once at the end
614     push @code, $self->_inline_get_old_value_for_trigger($instance, $old)
615         unless $for_constructor;
616
617     push @code, (
618         $self->SUPER::_inline_set_value($instance, $value),
619         $self->_inline_weaken_value($instance, $value),
620     );
621
622     # constructors do triggers all at once at the end
623     push @code, $self->_inline_trigger($instance, $value, $old)
624         unless $for_constructor;
625
626     return @code;
627 }
628
629 sub _writer_value_needs_copy {
630     my $self = shift;
631     return $self->should_coerce;
632 }
633
634 sub _inline_copy_value {
635     my $self = shift;
636     my ($value, $copy) = @_;
637
638     return 'my ' . $copy . ' = ' . $value . ';'
639 }
640
641 sub _inline_check_required {
642     my $self = shift;
643
644     return unless $self->is_required;
645
646     my $attr_name = quotemeta($self->name);
647
648     return (
649         'if (@_ < 2) {',
650             $self->_inline_throw_error(
651                 '"Attribute (' . $attr_name . ') is required"'
652             ) . ';',
653         '}',
654     );
655 }
656
657 sub _inline_tc_code {
658     my $self = shift;
659     my ($value, $tc, $coercion, $message, $is_lazy) = @_;
660     return (
661         $self->_inline_check_coercion(
662             $value, $tc, $coercion, $is_lazy,
663         ),
664         $self->_inline_check_constraint(
665             $value, $tc, $message, $is_lazy,
666         ),
667     );
668 }
669
670 sub _inline_check_coercion {
671     my $self = shift;
672     my ($value, $tc, $coercion) = @_;
673
674     return unless $self->should_coerce && $self->type_constraint->has_coercion;
675
676     if ( $self->type_constraint->can_be_inlined ) {
677         return (
678             'if (! (' . $self->type_constraint->_inline_check($value) . ')) {',
679                 $value . ' = ' . $coercion . '->(' . $value . ');',
680             '}',
681         );
682     }
683     else {
684         return (
685             'if (!' . $tc . '->(' . $value . ')) {',
686                 $value . ' = ' . $coercion . '->(' . $value . ');',
687             '}',
688         );
689     }
690 }
691
692 sub _inline_check_constraint {
693     my $self = shift;
694     my ($value, $tc, $message) = @_;
695
696     return unless $self->has_type_constraint;
697
698     my $attr_name = quotemeta($self->name);
699
700     if ( $self->type_constraint->can_be_inlined ) {
701         return (
702             'if (! (' . $self->type_constraint->_inline_check($value) . ')) {',
703                 $self->_inline_throw_error(
704                     '"Attribute (' . $attr_name . ') does not pass the type '
705                   . 'constraint because: " . '
706                   . 'do { local $_ = ' . $value . '; '
707                       . $message . '->(' . $value . ')'
708                   . '}',
709                     'data => ' . $value
710                 ) . ';',
711             '}',
712         );
713     }
714     else {
715         return (
716             'if (!' . $tc . '->(' . $value . ')) {',
717                 $self->_inline_throw_error(
718                     '"Attribute (' . $attr_name . ') does not pass the type '
719                   . 'constraint because: " . '
720                   . 'do { local $_ = ' . $value . '; '
721                       . $message . '->(' . $value . ')'
722                   . '}',
723                     'data => ' . $value
724                 ) . ';',
725             '}',
726         );
727     }
728 }
729
730 sub _inline_get_old_value_for_trigger {
731     my $self = shift;
732     my ($instance, $old) = @_;
733
734     return unless $self->has_trigger;
735
736     return (
737         'my ' . $old . ' = ' . $self->_inline_instance_has($instance),
738             '? ' . $self->_inline_instance_get($instance),
739             ': ();',
740     );
741 }
742
743 sub _inline_weaken_value {
744     my $self = shift;
745     my ($instance, $value) = @_;
746
747     return unless $self->is_weak_ref;
748
749     my $mi = $self->associated_class->get_meta_instance;
750     return (
751         $mi->inline_weaken_slot_value($instance, $self->name),
752             'if ref ' . $value . ';',
753     );
754 }
755
756 sub _inline_trigger {
757     my $self = shift;
758     my ($instance, $value, $old) = @_;
759
760     return unless $self->has_trigger;
761
762     return '$trigger->(' . $instance . ', ' . $value . ', ' . $old . ');';
763 }
764
765 sub _eval_environment {
766     my $self = shift;
767
768     my $env = { };
769
770     $env->{'$trigger'} = \($self->trigger)
771         if $self->has_trigger;
772     $env->{'$attr_default'} = \($self->default)
773         if $self->has_default;
774
775     if ($self->has_type_constraint) {
776         my $tc_obj = $self->type_constraint;
777
778         $env->{'$type_constraint'} = \(
779             $tc_obj->_compiled_type_constraint
780         ) unless $tc_obj->can_be_inlined;
781         # these two could probably get inlined versions too
782         $env->{'$type_coercion'} = \(
783             $tc_obj->coercion->_compiled_type_coercion
784         ) if $tc_obj->has_coercion;
785         $env->{'$type_message'} = \(
786             $tc_obj->has_message ? $tc_obj->message : $tc_obj->_default_message
787         );
788
789         $env = { %$env, %{ $tc_obj->inline_environment } };
790     }
791
792     # XXX ugh, fix these
793     $env->{'$attr'} = \$self
794         if $self->has_initializer && $self->is_lazy;
795     # pretty sure this is only going to be closed over if you use a custom
796     # error class at this point, but we should still get rid of this
797     # at some point
798     $env->{'$meta'} = \($self->associated_class);
799
800     return $env;
801 }
802
803 sub _weaken_value {
804     my ( $self, $instance ) = @_;
805
806     my $meta_instance = Class::MOP::Class->initialize( blessed($instance) )
807         ->get_meta_instance;
808
809     $meta_instance->weaken_slot_value( $instance, $self->name );
810 }
811
812 sub get_value {
813     my ($self, $instance, $for_trigger) = @_;
814
815     if ($self->is_lazy) {
816         unless ($self->has_value($instance)) {
817             my $value;
818             if ($self->has_default) {
819                 $value = $self->default($instance);
820             } elsif ( $self->has_builder ) {
821                 $value = $self->_call_builder($instance);
822             }
823
824             $value = $self->_coerce_and_verify( $value, $instance );
825
826             $self->set_initial_value($instance, $value);
827
828             if ( ref $value && $self->is_weak_ref ) {
829                 $self->_weaken_value($instance);
830             }
831         }
832     }
833
834     if ( $self->should_auto_deref && ! $for_trigger ) {
835
836         my $type_constraint = $self->type_constraint;
837
838         if ($type_constraint->is_a_type_of('ArrayRef')) {
839             my $rv = $self->SUPER::get_value($instance);
840             return unless defined $rv;
841             return wantarray ? @{ $rv } : $rv;
842         }
843         elsif ($type_constraint->is_a_type_of('HashRef')) {
844             my $rv = $self->SUPER::get_value($instance);
845             return unless defined $rv;
846             return wantarray ? %{ $rv } : $rv;
847         }
848         else {
849             $self->throw_error("Can not auto de-reference the type constraint '" . $type_constraint->name . "'", object => $instance, type_constraint => $type_constraint);
850         }
851
852     }
853     else {
854
855         return $self->SUPER::get_value($instance);
856     }
857 }
858
859 sub _inline_get_value {
860     my $self = shift;
861     my ($instance, $tc, $coercion, $message) = @_;
862
863     my $slot_access = $self->_inline_instance_get($instance);
864     $tc           ||= '$type_constraint';
865     $coercion     ||= '$type_coercion';
866     $message      ||= '$type_message';
867
868     return (
869         $self->_inline_check_lazy($instance, $tc, $coercion, $message),
870         $self->_inline_return_auto_deref($slot_access),
871     );
872 }
873
874 sub _inline_check_lazy {
875     my $self = shift;
876     my ($instance, $tc, $coercion, $message) = @_;
877
878     return unless $self->is_lazy;
879
880     my $slot_exists = $self->_inline_instance_has($instance);
881
882     return (
883         'if (!' . $slot_exists . ') {',
884             $self->_inline_init_from_default($instance, '$default', $tc, $coercion, $message, 'lazy'),
885         '}',
886     );
887 }
888
889 sub _inline_init_from_default {
890     my $self = shift;
891     my ($instance, $default, $tc, $coercion, $message, $for_lazy) = @_;
892
893     if (!($self->has_default || $self->has_builder)) {
894         $self->throw_error(
895             'You cannot have a lazy attribute '
896           . '(' . $self->name . ') '
897           . 'without specifying a default value for it',
898             attr => $self,
899         );
900     }
901
902     return (
903         $self->_inline_generate_default($instance, $default),
904         # intentionally not using _inline_tc_code, since that can be overridden
905         # to do things like possibly only do member tc checks, which isn't
906         # appropriate for checking the result of a default
907         $self->has_type_constraint
908             ? ($self->_inline_check_coercion($default, $tc, $coercion, $for_lazy),
909                $self->_inline_check_constraint($default, $tc, $message, $for_lazy))
910             : (),
911         $self->_inline_init_slot($instance, $default),
912         $self->_inline_weaken_value($instance, $default),
913     );
914 }
915
916 sub _inline_generate_default {
917     my $self = shift;
918     my ($instance, $default) = @_;
919
920     if ($self->has_default) {
921         my $source = 'my ' . $default . ' = $attr_default';
922         $source .= '->(' . $instance . ')'
923             if $self->is_default_a_coderef;
924         return $source . ';';
925     }
926     elsif ($self->has_builder) {
927         my $builder = B::perlstring($self->builder);
928         my $builder_str = quotemeta($self->builder);
929         my $attr_name_str = quotemeta($self->name);
930         return (
931             'my ' . $default . ';',
932             'if (my $builder = ' . $instance . '->can(' . $builder . ')) {',
933                 $default . ' = ' . $instance . '->$builder;',
934             '}',
935             'else {',
936                 'my $class = ref(' . $instance . ') || ' . $instance . ';',
937                 $self->_inline_throw_error(
938                     '"$class does not support builder method '
939                   . '\'' . $builder_str . '\' for attribute '
940                   . '\'' . $attr_name_str . '\'"'
941                 ) . ';',
942             '}',
943         );
944     }
945     else {
946         $self->throw_error(
947             "Can't generate a default for " . $self->name
948           . " since no default or builder was specified"
949         );
950     }
951 }
952
953 sub _inline_init_slot {
954     my $self = shift;
955     my ($inv, $value) = @_;
956
957     if ($self->has_initializer) {
958         return '$attr->set_initial_value(' . $inv . ', ' . $value . ');';
959     }
960     else {
961         return $self->_inline_instance_set($inv, $value) . ';';
962     }
963 }
964
965 sub _inline_return_auto_deref {
966     my $self = shift;
967
968     return 'return ' . $self->_auto_deref(@_) . ';';
969 }
970
971 sub _auto_deref {
972     my $self = shift;
973     my ($ref_value) = @_;
974
975     return $ref_value unless $self->should_auto_deref;
976
977     my $type_constraint = $self->type_constraint;
978
979     my $sigil;
980     if ($type_constraint->is_a_type_of('ArrayRef')) {
981         $sigil = '@';
982     }
983     elsif ($type_constraint->is_a_type_of('HashRef')) {
984         $sigil = '%';
985     }
986     else {
987         $self->throw_error(
988             'Can not auto de-reference the type constraint \''
989           . $type_constraint->name
990           . '\'',
991             type_constraint => $type_constraint,
992         );
993     }
994
995     return 'wantarray '
996              . '? ' . $sigil . '{ (' . $ref_value . ') || return } '
997              . ': (' . $ref_value . ')';
998 }
999
1000 ## installing accessors
1001
1002 sub accessor_metaclass { 'Moose::Meta::Method::Accessor' }
1003
1004 sub install_accessors {
1005     my $self = shift;
1006     $self->SUPER::install_accessors(@_);
1007     $self->install_delegation if $self->has_handles;
1008     return;
1009 }
1010
1011 sub _check_associated_methods {
1012     my $self = shift;
1013     unless (
1014         @{ $self->associated_methods }
1015         || ($self->_is_metadata || '') eq 'bare'
1016     ) {
1017         Carp::cluck(
1018             'Attribute (' . $self->name . ') of class '
1019             . $self->associated_class->name
1020             . ' has no associated methods'
1021             . ' (did you mean to provide an "is" argument?)'
1022             . "\n"
1023         )
1024     }
1025 }
1026
1027 sub _process_accessors {
1028     my $self = shift;
1029     my ($type, $accessor, $generate_as_inline_methods) = @_;
1030
1031     $accessor = ( keys %$accessor )[0] if ( ref($accessor) || '' ) eq 'HASH';
1032     my $method = $self->associated_class->get_method($accessor);
1033
1034     if (   $method
1035         && $method->isa('Class::MOP::Method::Accessor')
1036         && $method->associated_attribute->name ne $self->name ) {
1037
1038         my $other_attr_name = $method->associated_attribute->name;
1039         my $name            = $self->name;
1040
1041         Carp::cluck(
1042             "You are overwriting an accessor ($accessor) for the $other_attr_name attribute"
1043                 . " with a new accessor method for the $name attribute" );
1044     }
1045
1046     if (
1047            $method
1048         && !$method->is_stub
1049         && !$method->isa('Class::MOP::Method::Accessor')
1050         && (  !$self->definition_context
1051             || $method->package_name eq $self->definition_context->{package} )
1052         ) {
1053
1054         Carp::cluck(
1055             "You are overwriting a locally defined method ($accessor) with "
1056                 . "an accessor" );
1057     }
1058
1059     if (  !$self->associated_class->has_method($accessor)
1060         && $self->associated_class->has_package_symbol( '&' . $accessor ) ) {
1061
1062         Carp::cluck(
1063             "You are overwriting a locally defined function ($accessor) with "
1064                 . "an accessor" );
1065     }
1066
1067     $self->SUPER::_process_accessors(@_);
1068 }
1069
1070 sub remove_accessors {
1071     my $self = shift;
1072     $self->SUPER::remove_accessors(@_);
1073     $self->remove_delegation if $self->has_handles;
1074     return;
1075 }
1076
1077 sub install_delegation {
1078     my $self = shift;
1079
1080     # NOTE:
1081     # Here we canonicalize the 'handles' option
1082     # this will sort out any details and always
1083     # return an hash of methods which we want
1084     # to delagate to, see that method for details
1085     my %handles = $self->_canonicalize_handles;
1086
1087
1088     # install the delegation ...
1089     my $associated_class = $self->associated_class;
1090     foreach my $handle (sort keys %handles) {
1091         my $method_to_call = $handles{$handle};
1092         my $class_name = $associated_class->name;
1093         my $name = "${class_name}::${handle}";
1094
1095         if ( my $method = $associated_class->get_method($handle) ) {
1096             $self->throw_error(
1097                 "You cannot overwrite a locally defined method ($handle) with a delegation",
1098                 method_name => $handle
1099             ) unless $method->is_stub;
1100         }
1101
1102         # NOTE:
1103         # handles is not allowed to delegate
1104         # any of these methods, as they will
1105         # override the ones in your class, which
1106         # is almost certainly not what you want.
1107
1108         # FIXME warn when $handle was explicitly specified, but not if the source is a regex or something
1109         #cluck("Not delegating method '$handle' because it is a core method") and
1110         next if $class_name->isa("Moose::Object") and $handle =~ /^BUILD|DEMOLISH$/ || Moose::Object->can($handle);
1111
1112         my $method = $self->_make_delegation_method($handle, $method_to_call);
1113
1114         $self->associated_class->add_method($method->name, $method);
1115         $self->associate_method($method);
1116     }
1117 }
1118
1119 sub remove_delegation {
1120     my $self = shift;
1121     my %handles = $self->_canonicalize_handles;
1122     my $associated_class = $self->associated_class;
1123     foreach my $handle (keys %handles) {
1124         next unless any { $handle eq $_ }
1125                     map { $_->name }
1126                     @{ $self->associated_methods };
1127         $self->associated_class->remove_method($handle);
1128     }
1129 }
1130
1131 # private methods to help delegation ...
1132
1133 sub _canonicalize_handles {
1134     my $self    = shift;
1135     my $handles = $self->handles;
1136     if (my $handle_type = ref($handles)) {
1137         if ($handle_type eq 'HASH') {
1138             return %{$handles};
1139         }
1140         elsif ($handle_type eq 'ARRAY') {
1141             return map { $_ => $_ } @{$handles};
1142         }
1143         elsif ($handle_type eq 'Regexp') {
1144             ($self->has_type_constraint)
1145                 || $self->throw_error("Cannot delegate methods based on a Regexp without a type constraint (isa)", data => $handles);
1146             return map  { ($_ => $_) }
1147                    grep { /$handles/ } $self->_get_delegate_method_list;
1148         }
1149         elsif ($handle_type eq 'CODE') {
1150             return $handles->($self, $self->_find_delegate_metaclass);
1151         }
1152         elsif (blessed($handles) && $handles->isa('Moose::Meta::TypeConstraint::DuckType')) {
1153             return map { $_ => $_ } @{ $handles->methods };
1154         }
1155         elsif (blessed($handles) && $handles->isa('Moose::Meta::TypeConstraint::Role')) {
1156             $handles = $handles->role;
1157         }
1158         else {
1159             $self->throw_error("Unable to canonicalize the 'handles' option with $handles", data => $handles);
1160         }
1161     }
1162
1163     load_class($handles);
1164     my $role_meta = Class::MOP::class_of($handles);
1165
1166     (blessed $role_meta && $role_meta->isa('Moose::Meta::Role'))
1167         || $self->throw_error("Unable to canonicalize the 'handles' option with $handles because its metaclass is not a Moose::Meta::Role", data => $handles);
1168
1169     return map { $_ => $_ }
1170         map { $_->name }
1171         grep { !$_->isa('Class::MOP::Method::Meta') } (
1172         $role_meta->_get_local_methods,
1173         $role_meta->get_required_method_list,
1174         );
1175 }
1176
1177 sub _get_delegate_method_list {
1178     my $self = shift;
1179     my $meta = $self->_find_delegate_metaclass;
1180     if ($meta->isa('Class::MOP::Class')) {
1181         return map  { $_->name }  # NOTE: !never! delegate &meta
1182                grep { $_->package_name ne 'Moose::Object' && !$_->isa('Class::MOP::Method::Meta') }
1183                     $meta->get_all_methods;
1184     }
1185     elsif ($meta->isa('Moose::Meta::Role')) {
1186         return $meta->get_method_list;
1187     }
1188     else {
1189         $self->throw_error("Unable to recognize the delegate metaclass '$meta'", data => $meta);
1190     }
1191 }
1192
1193 sub _find_delegate_metaclass {
1194     my $self = shift;
1195     if (my $class = $self->_isa_metadata) {
1196         unless ( is_class_loaded($class) ) {
1197             $self->throw_error(
1198                 sprintf(
1199                     'The %s attribute is trying to delegate to a class which has not been loaded - %s',
1200                     $self->name, $class
1201                 )
1202             );
1203         }
1204         # we might be dealing with a non-Moose class,
1205         # and need to make our own metaclass. if there's
1206         # already a metaclass, it will be returned
1207         return Class::MOP::Class->initialize($class);
1208     }
1209     elsif (my $role = $self->_does_metadata) {
1210         unless ( is_class_loaded($class) ) {
1211             $self->throw_error(
1212                 sprintf(
1213                     'The %s attribute is trying to delegate to a role which has not been loaded - %s',
1214                     $self->name, $role
1215                 )
1216             );
1217         }
1218
1219         return Class::MOP::class_of($role);
1220     }
1221     else {
1222         $self->throw_error("Cannot find delegate metaclass for attribute " . $self->name);
1223     }
1224 }
1225
1226 sub delegation_metaclass { 'Moose::Meta::Method::Delegation' }
1227
1228 sub _make_delegation_method {
1229     my ( $self, $handle_name, $method_to_call ) = @_;
1230
1231     my @curried_arguments;
1232
1233     ($method_to_call, @curried_arguments) = @$method_to_call
1234         if 'ARRAY' eq ref($method_to_call);
1235
1236     return $self->delegation_metaclass->new(
1237         name               => $handle_name,
1238         package_name       => $self->associated_class->name,
1239         attribute          => $self,
1240         delegate_to_method => $method_to_call,
1241         curried_arguments  => \@curried_arguments,
1242     );
1243 }
1244
1245 sub _coerce_and_verify {
1246     my $self     = shift;
1247     my $val      = shift;
1248     my $instance = shift;
1249
1250     return $val unless $self->has_type_constraint;
1251
1252     $val = $self->type_constraint->coerce($val)
1253         if $self->should_coerce && $self->type_constraint->has_coercion;
1254
1255     $self->verify_against_type_constraint($val, instance => $instance);
1256
1257     return $val;
1258 }
1259
1260 sub verify_against_type_constraint {
1261     my $self = shift;
1262     my $val  = shift;
1263
1264     return 1 if !$self->has_type_constraint;
1265
1266     my $type_constraint = $self->type_constraint;
1267
1268     $type_constraint->check($val)
1269         || $self->throw_error("Attribute ("
1270                  . $self->name
1271                  . ") does not pass the type constraint because: "
1272                  . $type_constraint->get_message($val), data => $val, @_);
1273 }
1274
1275 package Moose::Meta::Attribute::Custom::Moose;
1276 sub register_implementation { 'Moose::Meta::Attribute' }
1277
1278 1;
1279
1280 # ABSTRACT: The Moose attribute metaclass
1281
1282 __END__
1283
1284 =pod
1285
1286 =head1 DESCRIPTION
1287
1288 This class is a subclass of L<Class::MOP::Attribute> that provides
1289 additional Moose-specific functionality.
1290
1291 To really understand this class, you will need to start with the
1292 L<Class::MOP::Attribute> documentation. This class can be understood
1293 as a set of additional features on top of the basic feature provided
1294 by that parent class.
1295
1296 =head1 INHERITANCE
1297
1298 C<Moose::Meta::Attribute> is a subclass of L<Class::MOP::Attribute>.
1299
1300 =head1 METHODS
1301
1302 Many of the documented below override methods in
1303 L<Class::MOP::Attribute> and add Moose specific features.
1304
1305 =head2 Creation
1306
1307 =over 4
1308
1309 =item B<< Moose::Meta::Attribute->new($name, %options) >>
1310
1311 This method overrides the L<Class::MOP::Attribute> constructor.
1312
1313 Many of the options below are described in more detail in the
1314 L<Moose::Manual::Attributes> document.
1315
1316 It adds the following options to the constructor:
1317
1318 =over 8
1319
1320 =item * is => 'ro', 'rw', 'bare'
1321
1322 This provides a shorthand for specifying the C<reader>, C<writer>, or
1323 C<accessor> names. If the attribute is read-only ('ro') then it will
1324 have a C<reader> method with the same attribute as the name.
1325
1326 If it is read-write ('rw') then it will have an C<accessor> method
1327 with the same name. If you provide an explicit C<writer> for a
1328 read-write attribute, then you will have a C<reader> with the same
1329 name as the attribute, and a C<writer> with the name you provided.
1330
1331 Use 'bare' when you are deliberately not installing any methods
1332 (accessor, reader, etc.) associated with this attribute; otherwise,
1333 Moose will issue a deprecation warning when this attribute is added to a
1334 metaclass.
1335
1336 =item * isa => $type
1337
1338 This option accepts a type. The type can be a string, which should be
1339 a type name. If the type name is unknown, it is assumed to be a class
1340 name.
1341
1342 This option can also accept a L<Moose::Meta::TypeConstraint> object.
1343
1344 If you I<also> provide a C<does> option, then your C<isa> option must
1345 be a class name, and that class must do the role specified with
1346 C<does>.
1347
1348 =item * does => $role
1349
1350 This is short-hand for saying that the attribute's type must be an
1351 object which does the named role.
1352
1353 =item * coerce => $bool
1354
1355 This option is only valid for objects with a type constraint
1356 (C<isa>) that defined a coercion. If this is true, then coercions will be applied whenever
1357 this attribute is set.
1358
1359 You can make both this and the C<weak_ref> option true.
1360
1361 =item * trigger => $sub
1362
1363 This option accepts a subroutine reference, which will be called after
1364 the attribute is set.
1365
1366 =item * required => $bool
1367
1368 An attribute which is required must be provided to the constructor. An
1369 attribute which is required can also have a C<default> or C<builder>,
1370 which will satisfy its required-ness.
1371
1372 A required attribute must have a C<default>, C<builder> or a
1373 non-C<undef> C<init_arg>
1374
1375 =item * lazy => $bool
1376
1377 A lazy attribute must have a C<default> or C<builder>. When an
1378 attribute is lazy, the default value will not be calculated until the
1379 attribute is read.
1380
1381 =item * weak_ref => $bool
1382
1383 If this is true, the attribute's value will be stored as a weak
1384 reference.
1385
1386 =item * auto_deref => $bool
1387
1388 If this is true, then the reader will dereference the value when it is
1389 called. The attribute must have a type constraint which defines the
1390 attribute as an array or hash reference.
1391
1392 =item * lazy_build => $bool
1393
1394 Setting this to true makes the attribute lazy and provides a number of
1395 default methods.
1396
1397   has 'size' => (
1398       is         => 'ro',
1399       lazy_build => 1,
1400   );
1401
1402 is equivalent to this:
1403
1404   has 'size' => (
1405       is        => 'ro',
1406       lazy      => 1,
1407       builder   => '_build_size',
1408       clearer   => 'clear_size',
1409       predicate => 'has_size',
1410   );
1411
1412
1413 If your attribute name starts with an underscore (C<_>), then the clearer
1414 and predicate will as well:
1415
1416   has '_size' => (
1417       is         => 'ro',
1418       lazy_build => 1,
1419   );
1420
1421 becomes:
1422
1423   has '_size' => (
1424       is        => 'ro',
1425       lazy      => 1,
1426       builder   => '_build__size',
1427       clearer   => '_clear_size',
1428       predicate => '_has_size',
1429   );
1430
1431 Note the doubled underscore in the builder name. Internally, Moose
1432 simply prepends the attribute name with "_build_" to come up with the
1433 builder name.
1434
1435 =item * documentation
1436
1437 An arbitrary string that can be retrieved later by calling C<<
1438 $attr->documentation >>.
1439
1440 =back
1441
1442 =item B<< $attr->clone(%options) >>
1443
1444 This creates a new attribute based on attribute being cloned. You must
1445 supply a C<name> option to provide a new name for the attribute.
1446
1447 The C<%options> can only specify options handled by
1448 L<Class::MOP::Attribute>.
1449
1450 =back
1451
1452 =head2 Value management
1453
1454 =over 4
1455
1456 =item B<< $attr->initialize_instance_slot($meta_instance, $instance, $params) >>
1457
1458 This method is used internally to initialize the attribute's slot in
1459 the object C<$instance>.
1460
1461 This overrides the L<Class::MOP::Attribute> method to handle lazy
1462 attributes, weak references, and type constraints.
1463
1464 =item B<get_value>
1465
1466 =item B<set_value>
1467
1468   eval { $point->meta->get_attribute('x')->set_value($point, 'forty-two') };
1469   if($@) {
1470     print "Oops: $@\n";
1471   }
1472
1473 I<Attribute (x) does not pass the type constraint (Int) with 'forty-two'>
1474
1475 Before setting the value, a check is made on the type constraint of
1476 the attribute, if it has one, to see if the value passes it. If the
1477 value fails to pass, the set operation dies.
1478
1479 Any coercion to convert values is done before checking the type constraint.
1480
1481 To check a value against a type constraint before setting it, fetch the
1482 attribute instance using L<Class::MOP::Class/find_attribute_by_name>,
1483 fetch the type_constraint from the attribute using L<Moose::Meta::Attribute/type_constraint>
1484 and call L<Moose::Meta::TypeConstraint/check>. See L<Moose::Cookbook::Basics::Company_Subtypes>
1485 for an example.
1486
1487 =back
1488
1489 =head2 Attribute Accessor generation
1490
1491 =over 4
1492
1493 =item B<< $attr->install_accessors >>
1494
1495 This method overrides the parent to also install delegation methods.
1496
1497 If, after installing all methods, the attribute object has no associated
1498 methods, it throws an error unless C<< is => 'bare' >> was passed to the
1499 attribute constructor.  (Trying to add an attribute that has no associated
1500 methods is almost always an error.)
1501
1502 =item B<< $attr->remove_accessors >>
1503
1504 This method overrides the parent to also remove delegation methods.
1505
1506 =item B<< $attr->inline_set($instance_var, $value_var) >>
1507
1508 This method return a code snippet suitable for inlining the relevant
1509 operation. It expect strings containing variable names to be used in the
1510 inlining, like C<'$self'> or C<'$_[1]'>.
1511
1512 =item B<< $attr->install_delegation >>
1513
1514 This method adds its delegation methods to the attribute's associated
1515 class, if it has any to add.
1516
1517 =item B<< $attr->remove_delegation >>
1518
1519 This method remove its delegation methods from the attribute's
1520 associated class.
1521
1522 =item B<< $attr->accessor_metaclass >>
1523
1524 Returns the accessor metaclass name, which defaults to
1525 L<Moose::Meta::Method::Accessor>.
1526
1527 =item B<< $attr->delegation_metaclass >>
1528
1529 Returns the delegation metaclass name, which defaults to
1530 L<Moose::Meta::Method::Delegation>.
1531
1532 =back
1533
1534 =head2 Additional Moose features
1535
1536 These methods are not found in the superclass. They support features
1537 provided by Moose.
1538
1539 =over 4
1540
1541 =item B<< $attr->does($role) >>
1542
1543 This indicates whether the I<attribute itself> does the given
1544 role. The role can be given as a full class name, or as a resolvable
1545 trait name.
1546
1547 Note that this checks the attribute itself, not its type constraint,
1548 so it is checking the attribute's metaclass and any traits applied to
1549 the attribute.
1550
1551 =item B<< Moose::Meta::Class->interpolate_class_and_new($name, %options) >>
1552
1553 This is an alternate constructor that handles the C<metaclass> and
1554 C<traits> options.
1555
1556 Effectively, this method is a factory that finds or creates the
1557 appropriate class for the given C<metaclass> and/or C<traits>.
1558
1559 Once it has the appropriate class, it will call C<< $class->new($name,
1560 %options) >> on that class.
1561
1562 =item B<< $attr->clone_and_inherit_options(%options) >>
1563
1564 This method supports the C<has '+foo'> feature. It does various bits
1565 of processing on the supplied C<%options> before ultimately calling
1566 the C<clone> method.
1567
1568 One of its main tasks is to make sure that the C<%options> provided
1569 does not include the options returned by the
1570 C<illegal_options_for_inheritance> method.
1571
1572 =item B<< $attr->illegal_options_for_inheritance >>
1573
1574 This returns a blacklist of options that can not be overridden in a
1575 subclass's attribute definition.
1576
1577 This exists to allow a custom metaclass to change or add to the list
1578 of options which can not be changed.
1579
1580 =item B<< $attr->type_constraint >>
1581
1582 Returns the L<Moose::Meta::TypeConstraint> object for this attribute,
1583 if it has one.
1584
1585 =item B<< $attr->has_type_constraint >>
1586
1587 Returns true if this attribute has a type constraint.
1588
1589 =item B<< $attr->verify_against_type_constraint($value) >>
1590
1591 Given a value, this method returns true if the value is valid for the
1592 attribute's type constraint. If the value is not valid, it throws an
1593 error.
1594
1595 =item B<< $attr->handles >>
1596
1597 This returns the value of the C<handles> option passed to the
1598 constructor.
1599
1600 =item B<< $attr->has_handles >>
1601
1602 Returns true if this attribute performs delegation.
1603
1604 =item B<< $attr->is_weak_ref >>
1605
1606 Returns true if this attribute stores its value as a weak reference.
1607
1608 =item B<< $attr->is_required >>
1609
1610 Returns true if this attribute is required to have a value.
1611
1612 =item B<< $attr->is_lazy >>
1613
1614 Returns true if this attribute is lazy.
1615
1616 =item B<< $attr->is_lazy_build >>
1617
1618 Returns true if the C<lazy_build> option was true when passed to the
1619 constructor.
1620
1621 =item B<< $attr->should_coerce >>
1622
1623 Returns true if the C<coerce> option passed to the constructor was
1624 true.
1625
1626 =item B<< $attr->should_auto_deref >>
1627
1628 Returns true if the C<auto_deref> option passed to the constructor was
1629 true.
1630
1631 =item B<< $attr->trigger >>
1632
1633 This is the subroutine reference that was in the C<trigger> option
1634 passed to the constructor, if any.
1635
1636 =item B<< $attr->has_trigger >>
1637
1638 Returns true if this attribute has a trigger set.
1639
1640 =item B<< $attr->documentation >>
1641
1642 Returns the value that was in the C<documentation> option passed to
1643 the constructor, if any.
1644
1645 =item B<< $attr->has_documentation >>
1646
1647 Returns true if this attribute has any documentation.
1648
1649 =item B<< $attr->applied_traits >>
1650
1651 This returns an array reference of all the traits which were applied
1652 to this attribute. If none were applied, this returns C<undef>.
1653
1654 =item B<< $attr->has_applied_traits >>
1655
1656 Returns true if this attribute has any traits applied.
1657
1658 =back
1659
1660 =head1 BUGS
1661
1662 See L<Moose/BUGS> for details on reporting bugs.
1663
1664 =cut