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