2 package Moose::Meta::Attribute;
7 use Scalar::Util 'blessed', 'weaken';
11 our $VERSION = '0.92';
12 our $AUTHORITY = 'cpan:STEVAN';
14 use Moose::Meta::Method::Accessor;
15 use Moose::Meta::Method::Delegation;
17 use Moose::Util::TypeConstraints ();
19 use base 'Class::MOP::Attribute';
21 # options which are not directly used
22 # but we store them for metadata purposes
23 __PACKAGE__->meta->add_attribute('isa' => (reader => '_isa_metadata'));
24 __PACKAGE__->meta->add_attribute('does' => (reader => '_does_metadata'));
25 __PACKAGE__->meta->add_attribute('is' => (reader => '_is_metadata'));
27 # these are actual options for the attrs
28 __PACKAGE__->meta->add_attribute('required' => (reader => 'is_required' ));
29 __PACKAGE__->meta->add_attribute('lazy' => (reader => 'is_lazy' ));
30 __PACKAGE__->meta->add_attribute('lazy_build' => (reader => 'is_lazy_build' ));
31 __PACKAGE__->meta->add_attribute('coerce' => (reader => 'should_coerce' ));
32 __PACKAGE__->meta->add_attribute('weak_ref' => (reader => 'is_weak_ref' ));
33 __PACKAGE__->meta->add_attribute('auto_deref' => (reader => 'should_auto_deref'));
34 __PACKAGE__->meta->add_attribute('type_constraint' => (
35 reader => 'type_constraint',
36 predicate => 'has_type_constraint',
38 __PACKAGE__->meta->add_attribute('trigger' => (
40 predicate => 'has_trigger',
42 __PACKAGE__->meta->add_attribute('handles' => (
44 writer => '_set_handles',
45 predicate => 'has_handles',
47 __PACKAGE__->meta->add_attribute('documentation' => (
48 reader => 'documentation',
49 predicate => 'has_documentation',
51 __PACKAGE__->meta->add_attribute('traits' => (
52 reader => 'applied_traits',
53 predicate => 'has_applied_traits',
56 # we need to have a ->does method in here to
57 # more easily support traits, and the introspection
58 # of those traits. We extend the does check to look
59 # for metatrait aliases.
61 my ($self, $role_name) = @_;
63 Moose::Util::resolve_metatrait_alias(Attribute => $role_name)
65 return 0 if !defined($name); # failed to load class
66 return $self->Moose::Object::does($name);
71 my $class = ( ref $self && $self->associated_class ) || "Moose::Meta::Class";
72 unshift @_, "message" if @_ % 2 == 1;
73 unshift @_, attr => $self if ref $self;
75 my $handler = $class->can("throw_error"); # to avoid incrementing depth by 1
80 my ($class, $name, %options) = @_;
81 $class->_process_options($name, \%options) unless $options{__hack_no_process_options}; # used from clone()... YECHKKK FIXME ICKY YUCK GROSS
83 delete $options{__hack_no_process_options};
88 map { $_->init_arg() }
89 $class->meta()->get_all_attributes()
92 my @bad = sort grep { ! $attrs{$_} } keys %options;
96 Carp::cluck "Found unknown argument(s) passed to '$name' attribute constructor in '$class': @bad";
99 return $class->SUPER::new($name, %options);
102 sub interpolate_class_and_new {
103 my ($class, $name, %args) = @_;
105 my ( $new_class, @traits ) = $class->interpolate_class(\%args);
107 $new_class->new($name, %args, ( scalar(@traits) ? ( traits => \@traits ) : () ) );
110 sub interpolate_class {
111 my ($class, $options) = @_;
113 $class = ref($class) || $class;
115 if ( my $metaclass_name = delete $options->{metaclass} ) {
116 my $new_class = Moose::Util::resolve_metaclass_alias( Attribute => $metaclass_name );
118 if ( $class ne $new_class ) {
119 if ( $new_class->can("interpolate_class") ) {
120 return $new_class->interpolate_class($options);
129 if (my $traits = $options->{traits}) {
131 while ($i < @$traits) {
132 my $trait = $traits->[$i++];
133 next if ref($trait); # options to a trait we discarded
135 $trait = Moose::Util::resolve_metatrait_alias(Attribute => $trait)
138 next if $class->does($trait);
140 push @traits, $trait;
143 push @traits, $traits->[$i++]
144 if $traits->[$i] && ref($traits->[$i]);
148 my $anon_class = Moose::Meta::Class->create_anon_class(
149 superclasses => [ $class ],
150 roles => [ @traits ],
154 $class = $anon_class->name;
158 return ( wantarray ? ( $class, @traits ) : $class );
163 my @legal_options_for_inheritance = qw(
164 default coerce required
165 documentation lazy handles
166 builder type_constraint
171 sub legal_options_for_inheritance { @legal_options_for_inheritance }
174 # This method *must* be able to handle
175 # Class::MOP::Attribute instances as
176 # well. Yes, I know that is wrong, but
177 # apparently we didn't realize it was
178 # doing that and now we have some code
179 # which is dependent on it. The real
180 # solution of course is to push this
181 # feature back up into Class::MOP::Attribute
182 # but I not right now, I am too lazy.
183 # However if you are reading this and
184 # looking for something to do,.. please
187 sub clone_and_inherit_options {
188 my ($self, %options) = @_;
195 # we may want to extends a Class::MOP::Attribute
196 # in which case we need to be able to use the
197 # core set of legal options that have always
198 # been here. But we allows Moose::Meta::Attribute
199 # instances to changes them.
201 my @legal_options = $self->can('legal_options_for_inheritance')
202 ? $self->legal_options_for_inheritance
203 : @legal_options_for_inheritance;
205 foreach my $legal_option (@legal_options) {
206 if (exists $options{$legal_option}) {
207 $actual_options{$legal_option} = $options{$legal_option};
208 delete $options{$legal_option};
214 if (blessed($options{isa}) && $options{isa}->isa('Moose::Meta::TypeConstraint')) {
215 $type_constraint = $options{isa};
218 $type_constraint = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($options{isa});
219 (defined $type_constraint)
220 || $self->throw_error("Could not find the type constraint '" . $options{isa} . "'", data => $options{isa});
223 $actual_options{type_constraint} = $type_constraint;
224 delete $options{isa};
227 if ($options{does}) {
229 if (blessed($options{does}) && $options{does}->isa('Moose::Meta::TypeConstraint')) {
230 $type_constraint = $options{does};
233 $type_constraint = Moose::Util::TypeConstraints::find_or_create_does_type_constraint($options{does});
234 (defined $type_constraint)
235 || $self->throw_error("Could not find the type constraint '" . $options{does} . "'", data => $options{does});
238 $actual_options{type_constraint} = $type_constraint;
239 delete $options{does};
243 # this doesn't apply to Class::MOP::Attributes,
244 # so we can ignore it for them.
246 if ($self->can('interpolate_class')) {
247 ( $actual_options{metaclass}, my @traits ) = $self->interpolate_class(\%options);
250 my @all_traits = grep { $seen{$_}++ } @{ $self->applied_traits || [] }, @traits;
251 $actual_options{traits} = \@all_traits if @all_traits;
253 delete @options{qw(metaclass traits)};
256 (scalar keys %options == 0)
257 || $self->throw_error("Illegal inherited options => (" . (join ', ' => keys %options) . ")", data => \%options);
260 $self->clone(%actual_options);
264 my ( $self, %params ) = @_;
266 my $class = delete $params{metaclass} || ref $self;
268 my ( @init, @non_init );
270 foreach my $attr ( grep { $_->has_value($self) } Class::MOP::class_of($self)->get_all_attributes ) {
271 push @{ $attr->has_init_arg ? \@init : \@non_init }, $attr;
274 my %new_params = ( ( map { $_->init_arg => $_->get_value($self) } @init ), %params );
276 my $name = delete $new_params{name};
278 my $clone = $class->new($name, %new_params, __hack_no_process_options => 1 );
280 foreach my $attr ( @non_init ) {
281 $attr->set_value($clone, $attr->get_value($self));
287 sub _process_options {
288 my ($class, $name, $options) = @_;
290 if (exists $options->{is}) {
292 ### -------------------------
293 ## is => ro, writer => _foo # turns into (reader => foo, writer => _foo) as before
294 ## is => rw, writer => _foo # turns into (reader => foo, writer => _foo)
295 ## is => rw, accessor => _foo # turns into (accessor => _foo)
296 ## is => ro, accessor => _foo # error, accesor is rw
297 ### -------------------------
299 if ($options->{is} eq 'ro') {
300 $class->throw_error("Cannot define an accessor name on a read-only attribute, accessors are read/write", data => $options)
301 if exists $options->{accessor};
302 $options->{reader} ||= $name;
304 elsif ($options->{is} eq 'rw') {
305 if ($options->{writer}) {
306 $options->{reader} ||= $name;
309 $options->{accessor} ||= $name;
312 elsif ($options->{is} eq 'bare') {
313 # do nothing, but don't complain (later) about missing methods
316 $class->throw_error("I do not understand this option (is => " . $options->{is} . ") on attribute ($name)", data => $options->{is});
320 if (exists $options->{isa}) {
321 if (exists $options->{does}) {
322 if (try { $options->{isa}->can('does') }) {
323 ($options->{isa}->does($options->{does}))
324 || $class->throw_error("Cannot have an isa option and a does option if the isa does not do the does on attribute ($name)", data => $options);
327 $class->throw_error("Cannot have an isa option which cannot ->does() on attribute ($name)", data => $options);
331 # allow for anon-subtypes here ...
332 if (blessed($options->{isa}) && $options->{isa}->isa('Moose::Meta::TypeConstraint')) {
333 $options->{type_constraint} = $options->{isa};
336 $options->{type_constraint} = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($options->{isa});
339 elsif (exists $options->{does}) {
340 # allow for anon-subtypes here ...
341 if (blessed($options->{does}) && $options->{does}->isa('Moose::Meta::TypeConstraint')) {
342 $options->{type_constraint} = $options->{does};
345 $options->{type_constraint} = Moose::Util::TypeConstraints::find_or_create_does_type_constraint($options->{does});
349 if (exists $options->{coerce} && $options->{coerce}) {
350 (exists $options->{type_constraint})
351 || $class->throw_error("You cannot have coercion without specifying a type constraint on attribute ($name)", data => $options);
352 $class->throw_error("You cannot have a weak reference to a coerced value on attribute ($name)", data => $options)
353 if $options->{weak_ref};
356 if (exists $options->{trigger}) {
357 ('CODE' eq ref $options->{trigger})
358 || $class->throw_error("Trigger must be a CODE ref on attribute ($name)", data => $options->{trigger});
361 if (exists $options->{auto_deref} && $options->{auto_deref}) {
362 (exists $options->{type_constraint})
363 || $class->throw_error("You cannot auto-dereference without specifying a type constraint on attribute ($name)", data => $options);
364 ($options->{type_constraint}->is_a_type_of('ArrayRef') ||
365 $options->{type_constraint}->is_a_type_of('HashRef'))
366 || $class->throw_error("You cannot auto-dereference anything other than a ArrayRef or HashRef on attribute ($name)", data => $options);
369 if (exists $options->{lazy_build} && $options->{lazy_build} == 1) {
370 $class->throw_error("You can not use lazy_build and default for the same attribute ($name)", data => $options)
371 if exists $options->{default};
372 $options->{lazy} = 1;
373 $options->{builder} ||= "_build_${name}";
375 $options->{clearer} ||= "_clear${name}";
376 $options->{predicate} ||= "_has${name}";
379 $options->{clearer} ||= "clear_${name}";
380 $options->{predicate} ||= "has_${name}";
384 if (exists $options->{lazy} && $options->{lazy}) {
385 (exists $options->{default} || defined $options->{builder} )
386 || $class->throw_error("You cannot have lazy attribute ($name) without specifying a default value for it", data => $options);
389 if ( $options->{required} && !( ( !exists $options->{init_arg} || defined $options->{init_arg} ) || exists $options->{default} || defined $options->{builder} ) ) {
390 $class->throw_error("You cannot have a required attribute ($name) without a default, builder, or an init_arg", data => $options);
395 sub initialize_instance_slot {
396 my ($self, $meta_instance, $instance, $params) = @_;
397 my $init_arg = $self->init_arg();
398 # try to fetch the init arg from the %params ...
402 if ( defined($init_arg) and exists $params->{$init_arg}) {
403 $val = $params->{$init_arg};
407 # skip it if it's lazy
408 return if $self->is_lazy;
409 # and die if it's required and doesn't have a default value
410 $self->throw_error("Attribute (" . $self->name . ") is required", object => $instance, data => $params)
411 if $self->is_required && !$self->has_default && !$self->has_builder;
413 # if nothing was in the %params, we can use the
414 # attribute's default value (if it has one)
415 if ($self->has_default) {
416 $val = $self->default($instance);
419 elsif ($self->has_builder) {
420 $val = $self->_call_builder($instance);
425 return unless $value_is_set;
427 $val = $self->_coerce_and_verify( $val, $instance );
429 $self->set_initial_value($instance, $val);
431 if ( ref $val && $self->is_weak_ref ) {
432 $self->_weaken_value($instance);
437 my ( $self, $instance ) = @_;
439 my $builder = $self->builder();
441 return $instance->$builder()
442 if $instance->can( $self->builder );
444 $self->throw_error( blessed($instance)
445 . " does not support builder method '"
447 . "' for attribute '"
457 # this duplicates too much code from
458 # Class::MOP::Attribute, we need to
459 # refactor these bits eventually.
461 sub _set_initial_slot_value {
462 my ($self, $meta_instance, $instance, $value) = @_;
464 my $slot_name = $self->name;
466 return $meta_instance->set_slot_value($instance, $slot_name, $value)
467 unless $self->has_initializer;
469 my ($type_constraint, $can_coerce);
470 if ($self->has_type_constraint) {
471 $type_constraint = $self->type_constraint;
472 $can_coerce = ($self->should_coerce && $type_constraint->has_coercion);
476 my $val = $self->_coerce_and_verify( shift, $instance );;
478 $meta_instance->set_slot_value($instance, $slot_name, $val);
481 my $initializer = $self->initializer;
483 # most things will just want to set a value, so make it first arg
484 $instance->$initializer($value, $callback, $self);
488 my ($self, $instance, @args) = @_;
489 my $value = $args[0];
491 my $attr_name = $self->name;
493 if ($self->is_required and not @args) {
494 $self->throw_error("Attribute ($attr_name) is required", object => $instance);
497 $value = $self->_coerce_and_verify( $value, $instance );
500 if ( $self->has_trigger && $self->has_value($instance) ) {
501 @old = $self->get_value($instance, 'for trigger');
504 $self->SUPER::set_value($instance, $value);
506 if ( ref $value && $self->is_weak_ref ) {
507 $self->_weaken_value($instance);
510 if ($self->has_trigger) {
511 $self->trigger->($instance, $value, @old);
516 my ( $self, $instance ) = @_;
518 my $meta_instance = Class::MOP::Class->initialize( blessed($instance) )
521 $meta_instance->weaken_slot_value( $instance, $self->name );
525 my ($self, $instance, $for_trigger) = @_;
527 if ($self->is_lazy) {
528 unless ($self->has_value($instance)) {
530 if ($self->has_default) {
531 $value = $self->default($instance);
532 } elsif ( $self->has_builder ) {
533 $value = $self->_call_builder($instance);
536 $value = $self->_coerce_and_verify( $value, $instance );
538 $self->set_initial_value($instance, $value);
542 if ( $self->should_auto_deref && ! $for_trigger ) {
544 my $type_constraint = $self->type_constraint;
546 if ($type_constraint->is_a_type_of('ArrayRef')) {
547 my $rv = $self->SUPER::get_value($instance);
548 return unless defined $rv;
549 return wantarray ? @{ $rv } : $rv;
551 elsif ($type_constraint->is_a_type_of('HashRef')) {
552 my $rv = $self->SUPER::get_value($instance);
553 return unless defined $rv;
554 return wantarray ? %{ $rv } : $rv;
557 $self->throw_error("Can not auto de-reference the type constraint '" . $type_constraint->name . "'", object => $instance, type_constraint => $type_constraint);
563 return $self->SUPER::get_value($instance);
567 ## installing accessors
569 sub accessor_metaclass { 'Moose::Meta::Method::Accessor' }
571 sub install_accessors {
573 $self->SUPER::install_accessors(@_);
574 $self->install_delegation if $self->has_handles;
578 sub _check_associated_methods {
581 @{ $self->associated_methods }
582 || ($self->_is_metadata || '') eq 'bare'
585 'Attribute (' . $self->name . ') of class '
586 . $self->associated_class->name
587 . ' has no associated methods'
588 . ' (did you mean to provide an "is" argument?)'
594 sub _process_accessors {
596 my ($type, $accessor, $generate_as_inline_methods) = @_;
597 $accessor = (keys %$accessor)[0] if (ref($accessor)||'') eq 'HASH';
598 my $method = $self->associated_class->get_method($accessor);
599 if ($method && !$method->isa('Class::MOP::Method::Accessor')
600 && (!$self->definition_context
601 || $method->package_name eq $self->definition_context->{package})) {
603 "You are overwriting a locally defined method ($accessor) with "
607 $self->SUPER::_process_accessors(@_);
610 sub remove_accessors {
612 $self->SUPER::remove_accessors(@_);
613 $self->remove_delegation if $self->has_handles;
617 sub install_delegation {
621 # Here we canonicalize the 'handles' option
622 # this will sort out any details and always
623 # return an hash of methods which we want
624 # to delagate to, see that method for details
625 my %handles = $self->_canonicalize_handles;
628 # install the delegation ...
629 my $associated_class = $self->associated_class;
630 foreach my $handle (keys %handles) {
631 my $method_to_call = $handles{$handle};
632 my $class_name = $associated_class->name;
633 my $name = "${class_name}::${handle}";
635 (!$associated_class->has_method($handle))
636 || $self->throw_error("You cannot overwrite a locally defined method ($handle) with a delegation", method_name => $handle);
639 # handles is not allowed to delegate
640 # any of these methods, as they will
641 # override the ones in your class, which
642 # is almost certainly not what you want.
644 # FIXME warn when $handle was explicitly specified, but not if the source is a regex or something
645 #cluck("Not delegating method '$handle' because it is a core method") and
646 next if $class_name->isa("Moose::Object") and $handle =~ /^BUILD|DEMOLISH$/ || Moose::Object->can($handle);
648 my $method = $self->_make_delegation_method($handle, $method_to_call);
650 $self->associated_class->add_method($method->name, $method);
651 $self->associate_method($method);
655 sub remove_delegation {
657 my %handles = $self->_canonicalize_handles;
658 my $associated_class = $self->associated_class;
659 foreach my $handle (keys %handles) {
660 $self->associated_class->remove_method($handle);
664 # private methods to help delegation ...
666 sub _canonicalize_handles {
668 my $handles = $self->handles;
669 if (my $handle_type = ref($handles)) {
670 if ($handle_type eq 'HASH') {
673 elsif ($handle_type eq 'ARRAY') {
674 return map { $_ => $_ } @{$handles};
676 elsif ($handle_type eq 'Regexp') {
677 ($self->has_type_constraint)
678 || $self->throw_error("Cannot delegate methods based on a Regexp without a type constraint (isa)", data => $handles);
679 return map { ($_ => $_) }
680 grep { /$handles/ } $self->_get_delegate_method_list;
682 elsif ($handle_type eq 'CODE') {
683 return $handles->($self, $self->_find_delegate_metaclass);
685 elsif (blessed($handles) && $handles->isa('Moose::Meta::TypeConstraint::DuckType')) {
686 return map { $_ => $_ } @{ $handles->methods };
689 $self->throw_error("Unable to canonicalize the 'handles' option with $handles", data => $handles);
693 Class::MOP::load_class($handles);
694 my $role_meta = Class::MOP::class_of($handles);
696 (blessed $role_meta && $role_meta->isa('Moose::Meta::Role'))
697 || $self->throw_error("Unable to canonicalize the 'handles' option with $handles because its metaclass is not a Moose::Meta::Role", data => $handles);
699 return map { $_ => $_ }
700 grep { $_ ne 'meta' } (
701 $role_meta->get_method_list,
702 map { $_->name } $role_meta->get_required_method_list,
707 sub _find_delegate_metaclass {
709 if (my $class = $self->_isa_metadata) {
710 # we might be dealing with a non-Moose class,
711 # and need to make our own metaclass. if there's
712 # already a metaclass, it will be returned
713 return Moose::Meta::Class->initialize($class);
715 elsif (my $role = $self->_does_metadata) {
716 return Class::MOP::class_of($role);
719 $self->throw_error("Cannot find delegate metaclass for attribute " . $self->name);
723 sub _get_delegate_method_list {
725 my $meta = $self->_find_delegate_metaclass;
726 if ($meta->isa('Class::MOP::Class')) {
727 return map { $_->name } # NOTE: !never! delegate &meta
728 grep { $_->package_name ne 'Moose::Object' && $_->name ne 'meta' }
729 $meta->get_all_methods;
731 elsif ($meta->isa('Moose::Meta::Role')) {
732 return $meta->get_method_list;
735 $self->throw_error("Unable to recognize the delegate metaclass '$meta'", data => $meta);
739 sub delegation_metaclass { 'Moose::Meta::Method::Delegation' }
741 sub _make_delegation_method {
742 my ( $self, $handle_name, $method_to_call ) = @_;
744 my @curried_arguments;
746 ($method_to_call, @curried_arguments) = @$method_to_call
747 if 'ARRAY' eq ref($method_to_call);
749 return $self->delegation_metaclass->new(
750 name => $handle_name,
751 package_name => $self->associated_class->name,
753 delegate_to_method => $method_to_call,
754 curried_arguments => \@curried_arguments,
758 sub _coerce_and_verify {
761 my $instance = shift;
763 return $val unless $self->has_type_constraint;
765 my $type_constraint = $self->type_constraint;
766 if ($self->should_coerce && $type_constraint->has_coercion) {
767 $val = $type_constraint->coerce($val);
770 $self->verify_against_type_constraint($val, instance => $instance);
775 sub verify_against_type_constraint {
779 return 1 if !$self->has_type_constraint;
781 my $type_constraint = $self->type_constraint;
783 $type_constraint->check($val)
784 || $self->throw_error("Attribute ("
786 . ") does not pass the type constraint because: "
787 . $type_constraint->get_message($val), data => $val, @_);
790 package Moose::Meta::Attribute::Custom::Moose;
791 sub register_implementation { 'Moose::Meta::Attribute' }
801 Moose::Meta::Attribute - The Moose attribute metaclass
805 This class is a subclass of L<Class::MOP::Attribute> that provides
806 additional Moose-specific functionality.
808 To really understand this class, you will need to start with the
809 L<Class::MOP::Attribute> documentation. This class can be understood
810 as a set of additional features on top of the basic feature provided
811 by that parent class.
815 C<Moose::Meta::Attribute> is a subclass of L<Class::MOP::Attribute>.
819 Many of the documented below override methods in
820 L<Class::MOP::Attribute> and add Moose specific features.
826 =item B<< Moose::Meta::Attribute->new(%options) >>
828 This method overrides the L<Class::MOP::Attribute> constructor.
830 Many of the options below are described in more detail in the
831 L<Moose::Manual::Attributes> document.
833 It adds the following options to the constructor:
837 =item * is => 'ro', 'rw', 'bare'
839 This provides a shorthand for specifying the C<reader>, C<writer>, or
840 C<accessor> names. If the attribute is read-only ('ro') then it will
841 have a C<reader> method with the same attribute as the name.
843 If it is read-write ('rw') then it will have an C<accessor> method
844 with the same name. If you provide an explicit C<writer> for a
845 read-write attribute, then you will have a C<reader> with the same
846 name as the attribute, and a C<writer> with the name you provided.
848 Use 'bare' when you are deliberately not installing any methods
849 (accessor, reader, etc.) associated with this attribute; otherwise,
850 Moose will issue a deprecation warning when this attribute is added to a
855 This option accepts a type. The type can be a string, which should be
856 a type name. If the type name is unknown, it is assumed to be a class
859 This option can also accept a L<Moose::Meta::TypeConstraint> object.
861 If you I<also> provide a C<does> option, then your C<isa> option must
862 be a class name, and that class must do the role specified with
865 =item * does => $role
867 This is short-hand for saying that the attribute's type must be an
868 object which does the named role.
870 =item * coerce => $bool
872 This option is only valid for objects with a type constraint
873 (C<isa>). If this is true, then coercions will be applied whenever
874 this attribute is set.
876 You can make both this and the C<weak_ref> option true.
878 =item * trigger => $sub
880 This option accepts a subroutine reference, which will be called after
881 the attribute is set.
883 =item * required => $bool
885 An attribute which is required must be provided to the constructor. An
886 attribute which is required can also have a C<default> or C<builder>,
887 which will satisfy its required-ness.
889 A required attribute must have a C<default>, C<builder> or a
890 non-C<undef> C<init_arg>
892 =item * lazy => $bool
894 A lazy attribute must have a C<default> or C<builder>. When an
895 attribute is lazy, the default value will not be calculated until the
898 =item * weak_ref => $bool
900 If this is true, the attribute's value will be stored as a weak
903 =item * auto_deref => $bool
905 If this is true, then the reader will dereference the value when it is
906 called. The attribute must have a type constraint which defines the
907 attribute as an array or hash reference.
909 =item * lazy_build => $bool
911 Setting this to true makes the attribute lazy and provides a number of
919 is equivalent to this:
924 builder => '_build_size',
925 clearer => 'clear_size',
926 predicate => 'has_size',
929 =item * documentation
931 An arbitrary string that can be retrieved later by calling C<<
932 $attr->documentation >>.
936 =item B<< $attr->clone(%options) >>
938 This creates a new attribute based on attribute being cloned. You must
939 supply a C<name> option to provide a new name for the attribute.
941 The C<%options> can only specify options handled by
942 L<Class::MOP::Attribute>.
946 =head2 Value management
950 =item B<< $attr->initialize_instance_slot($meta_instance, $instance, $params) >>
952 This method is used internally to initialize the attribute's slot in
953 the object C<$instance>.
955 This overrides the L<Class::MOP::Attribute> method to handle lazy
956 attributes, weak references, and type constraints.
962 eval { $point->meta->get_attribute('x')->set_value($point, 'forty-two') };
967 I<Attribute (x) does not pass the type constraint (Int) with 'forty-two'>
969 Before setting the value, a check is made on the type constraint of
970 the attribute, if it has one, to see if the value passes it. If the
971 value fails to pass, the set operation dies with a L<throw_error>.
973 Any coercion to convert values is done before checking the type constraint.
975 To check a value against a type constraint before setting it, fetch the
976 attribute instance using L<Class::MOP::Class/find_attribute_by_name>,
977 fetch the type_constraint from the attribute using L<Moose::Meta::Attribute/type_constraint>
978 and call L<Moose::Meta::TypeConstraint/check>. See L<Moose::Cookbook::Basics::Recipe4>
983 =head2 Attribute Accessor generation
987 =item B<< $attr->install_accessors >>
989 This method overrides the parent to also install delegation methods.
991 If, after installing all methods, the attribute object has no associated
992 methods, it throws an error unless C<< is => 'bare' >> was passed to the
993 attribute constructor. (Trying to add an attribute that has no associated
994 methods is almost always an error.)
996 =item B<< $attr->remove_accessors >>
998 This method overrides the parent to also remove delegation methods.
1000 =item B<< $attr->install_delegation >>
1002 This method adds its delegation methods to the attribute's associated
1003 class, if it has any to add.
1005 =item B<< $attr->remove_delegation >>
1007 This method remove its delegation methods from the attribute's
1010 =item B<< $attr->accessor_metaclass >>
1012 Returns the accessor metaclass name, which defaults to
1013 L<Moose::Meta::Method::Accessor>.
1015 =item B<< $attr->delegation_metaclass >>
1017 Returns the delegation metaclass name, which defaults to
1018 L<Moose::Meta::Method::Delegation>.
1022 =head2 Additional Moose features
1024 These methods are not found in the superclass. They support features
1029 =item B<< $attr->does($role) >>
1031 This indicates whether the I<attribute itself> does the given
1032 role. The role can be given as a full class name, or as a resolvable
1035 Note that this checks the attribute itself, not its type constraint,
1036 so it is checking the attribute's metaclass and any traits applied to
1039 =item B<< Moose::Meta::Class->interpolate_class_and_new($name, %options) >>
1041 This is an alternate constructor that handles the C<metaclass> and
1044 Effectively, this method is a factory that finds or creates the
1045 appropriate class for the given C<metaclass> and/or C<traits>.
1047 Once it has the appropriate class, it will call C<< $class->new($name,
1048 %options) >> on that class.
1050 =item B<< $attr->clone_and_inherit_options(%options) >>
1052 This method supports the C<has '+foo'> feature. It does various bits
1053 of processing on the supplied C<%options> before ultimately calling
1054 the C<clone> method.
1056 One of its main tasks is to make sure that the C<%options> provided
1057 only includes the options returned by the
1058 C<legal_options_for_inheritance> method.
1060 =item B<< $attr->legal_options_for_inheritance >>
1062 This returns a whitelist of options that can be overridden in a
1063 subclass's attribute definition.
1065 This exists to allow a custom metaclass to change or add to the list
1066 of options which can be changed.
1068 =item B<< $attr->type_constraint >>
1070 Returns the L<Moose::Meta::TypeConstraint> object for this attribute,
1073 =item B<< $attr->has_type_constraint >>
1075 Returns true if this attribute has a type constraint.
1077 =item B<< $attr->verify_against_type_constraint($value) >>
1079 Given a value, this method returns true if the value is valid for the
1080 attribute's type constraint. If the value is not valid, it throws an
1083 =item B<< $attr->handles >>
1085 This returns the value of the C<handles> option passed to the
1088 =item B<< $attr->has_handles >>
1090 Returns true if this attribute performs delegation.
1092 =item B<< $attr->is_weak_ref >>
1094 Returns true if this attribute stores its value as a weak reference.
1096 =item B<< $attr->is_required >>
1098 Returns true if this attribute is required to have a value.
1100 =item B<< $attr->is_lazy >>
1102 Returns true if this attribute is lazy.
1104 =item B<< $attr->is_lazy_build >>
1106 Returns true if the C<lazy_build> option was true when passed to the
1109 =item B<< $attr->should_coerce >>
1111 Returns true if the C<coerce> option passed to the constructor was
1114 =item B<< $attr->should_auto_deref >>
1116 Returns true if the C<auto_deref> option passed to the constructor was
1119 =item B<< $attr->trigger >>
1121 This is the subroutine reference that was in the C<trigger> option
1122 passed to the constructor, if any.
1124 =item B<< $attr->has_trigger >>
1126 Returns true if this attribute has a trigger set.
1128 =item B<< $attr->documentation >>
1130 Returns the value that was in the C<documentation> option passed to
1131 the constructor, if any.
1133 =item B<< $attr->has_documentation >>
1135 Returns true if this attribute has any documentation.
1137 =item B<< $attr->applied_traits >>
1139 This returns an array reference of all the traits which were applied
1140 to this attribute. If none were applied, this returns C<undef>.
1142 =item B<< $attr->has_applied_traits >>
1144 Returns true if this attribute has any traits applied.
1150 All complex software has bugs lurking in it, and this module is no
1151 exception. If you find a bug please either email me, or add the bug
1156 Stevan Little E<lt>stevan@iinteractive.comE<gt>
1158 Yuval Kogman E<lt>nothingmuch@woobling.comE<gt>
1160 =head1 COPYRIGHT AND LICENSE
1162 Copyright 2006-2009 by Infinity Interactive, Inc.
1164 L<http://www.iinteractive.com>
1166 This library is free software; you can redistribute it and/or modify
1167 it under the same terms as Perl itself.