2 package Moose::Meta::Attribute;
9 use Scalar::Util 'blessed', 'weaken';
10 use List::MoreUtils 'any';
14 use Moose::Deprecated;
15 use Moose::Meta::Method::Accessor;
16 use Moose::Meta::Method::Delegation;
18 use Moose::Util::TypeConstraints ();
19 use Class::MOP::MiniTrait;
21 use base 'Class::MOP::Attribute', 'Moose::Meta::Mixin::AttributeCore';
23 Class::MOP::MiniTrait::apply(__PACKAGE__, 'Moose::Meta::Object::Trait');
25 __PACKAGE__->meta->add_attribute('traits' => (
26 reader => 'applied_traits',
27 predicate => 'has_applied_traits',
30 # we need to have a ->does method in here to
31 # more easily support traits, and the introspection
32 # of those traits. We extend the does check to look
33 # for metatrait aliases.
35 my ($self, $role_name) = @_;
37 Moose::Util::resolve_metatrait_alias(Attribute => $role_name)
39 return 0 if !defined($name); # failed to load class
40 return $self->Moose::Object::does($name);
45 my $class = ( ref $self && $self->associated_class ) || "Moose::Meta::Class";
46 unshift @_, "message" if @_ % 2 == 1;
47 unshift @_, attr => $self if ref $self;
49 my $handler = $class->can("throw_error"); # to avoid incrementing depth by 1
53 sub _inline_throw_error {
54 my ( $self, $msg, $args ) = @_;
55 "\$meta->throw_error($msg" . ($args ? ", $args" : "") . ")"; # FIXME makes deparsing *REALLY* hard
59 my ($class, $name, %options) = @_;
60 $class->_process_options($name, \%options) unless $options{__hack_no_process_options}; # used from clone()... YECHKKK FIXME ICKY YUCK GROSS
62 delete $options{__hack_no_process_options};
67 map { $_->init_arg() }
68 $class->meta()->get_all_attributes()
71 my @bad = sort grep { ! $attrs{$_} } keys %options;
75 Carp::cluck "Found unknown argument(s) passed to '$name' attribute constructor in '$class': @bad";
78 return $class->SUPER::new($name, %options);
81 sub interpolate_class_and_new {
82 my ($class, $name, %args) = @_;
84 my ( $new_class, @traits ) = $class->interpolate_class(\%args);
86 $new_class->new($name, %args, ( scalar(@traits) ? ( traits => \@traits ) : () ) );
89 sub interpolate_class {
90 my ($class, $options) = @_;
92 $class = ref($class) || $class;
94 if ( my $metaclass_name = delete $options->{metaclass} ) {
95 my $new_class = Moose::Util::resolve_metaclass_alias( Attribute => $metaclass_name );
97 if ( $class ne $new_class ) {
98 if ( $new_class->can("interpolate_class") ) {
99 return $new_class->interpolate_class($options);
108 if (my $traits = $options->{traits}) {
110 while ($i < @$traits) {
111 my $trait = $traits->[$i++];
112 next if ref($trait); # options to a trait we discarded
114 $trait = Moose::Util::resolve_metatrait_alias(Attribute => $trait)
117 next if $class->does($trait);
119 push @traits, $trait;
122 push @traits, $traits->[$i++]
123 if $traits->[$i] && ref($traits->[$i]);
127 my $anon_class = Moose::Meta::Class->create_anon_class(
128 superclasses => [ $class ],
129 roles => [ @traits ],
133 $class = $anon_class->name;
137 return ( wantarray ? ( $class, @traits ) : $class );
142 # method-generating options shouldn't be overridden
143 sub illegal_options_for_inheritance {
144 qw(reader writer accessor clearer predicate)
148 # This method *must* be able to handle
149 # Class::MOP::Attribute instances as
150 # well. Yes, I know that is wrong, but
151 # apparently we didn't realize it was
152 # doing that and now we have some code
153 # which is dependent on it. The real
154 # solution of course is to push this
155 # feature back up into Class::MOP::Attribute
156 # but I not right now, I am too lazy.
157 # However if you are reading this and
158 # looking for something to do,.. please
161 sub clone_and_inherit_options {
162 my ($self, %options) = @_;
165 # we may want to extends a Class::MOP::Attribute
166 # in which case we need to be able to use the
167 # core set of legal options that have always
168 # been here. But we allows Moose::Meta::Attribute
169 # instances to changes them.
171 my @illegal_options = $self->can('illegal_options_for_inheritance')
172 ? $self->illegal_options_for_inheritance
175 my @found_illegal_options = grep { exists $options{$_} && exists $self->{$_} ? $_ : undef } @illegal_options;
176 (scalar @found_illegal_options == 0)
177 || $self->throw_error("Illegal inherited options => (" . (join ', ' => @found_illegal_options) . ")", data => \%options);
181 if (blessed($options{isa}) && $options{isa}->isa('Moose::Meta::TypeConstraint')) {
182 $type_constraint = $options{isa};
185 $type_constraint = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($options{isa});
186 (defined $type_constraint)
187 || $self->throw_error("Could not find the type constraint '" . $options{isa} . "'", data => $options{isa});
190 $options{type_constraint} = $type_constraint;
193 if ($options{does}) {
195 if (blessed($options{does}) && $options{does}->isa('Moose::Meta::TypeConstraint')) {
196 $type_constraint = $options{does};
199 $type_constraint = Moose::Util::TypeConstraints::find_or_create_does_type_constraint($options{does});
200 (defined $type_constraint)
201 || $self->throw_error("Could not find the type constraint '" . $options{does} . "'", data => $options{does});
204 $options{type_constraint} = $type_constraint;
208 # this doesn't apply to Class::MOP::Attributes,
209 # so we can ignore it for them.
211 if ($self->can('interpolate_class')) {
212 ( $options{metaclass}, my @traits ) = $self->interpolate_class(\%options);
215 my @all_traits = grep { $seen{$_}++ } @{ $self->applied_traits || [] }, @traits;
216 $options{traits} = \@all_traits if @all_traits;
219 # This method can be called on a CMOP::Attribute object, so we need to
220 # make sure we can call this method.
221 $self->_process_lazy_build_option( $self->name, \%options )
222 if $self->can('_process_lazy_build_option');
224 $self->clone(%options);
228 my ( $self, %params ) = @_;
230 my $class = delete $params{metaclass} || ref $self;
232 my ( @init, @non_init );
234 foreach my $attr ( grep { $_->has_value($self) } Class::MOP::class_of($self)->get_all_attributes ) {
235 push @{ $attr->has_init_arg ? \@init : \@non_init }, $attr;
238 my %new_params = ( ( map { $_->init_arg => $_->get_value($self) } @init ), %params );
240 my $name = delete $new_params{name};
242 my $clone = $class->new($name, %new_params, __hack_no_process_options => 1 );
244 foreach my $attr ( @non_init ) {
245 $attr->set_value($clone, $attr->get_value($self));
251 sub _process_options {
252 my ( $class, $name, $options ) = @_;
254 $class->_process_is_option( $name, $options );
255 $class->_process_isa_option( $name, $options );
256 $class->_process_does_option( $name, $options );
257 $class->_process_coerce_option( $name, $options );
258 $class->_process_trigger_option( $name, $options );
259 $class->_process_auto_deref_option( $name, $options );
260 $class->_process_lazy_build_option( $name, $options );
261 $class->_process_lazy_option( $name, $options );
262 $class->_process_required_option( $name, $options );
265 sub _process_is_option {
266 my ( $class, $name, $options ) = @_;
268 return unless $options->{is};
270 ### -------------------------
271 ## is => ro, writer => _foo # turns into (reader => foo, writer => _foo) as before
272 ## is => rw, writer => _foo # turns into (reader => foo, writer => _foo)
273 ## is => rw, accessor => _foo # turns into (accessor => _foo)
274 ## is => ro, accessor => _foo # error, accesor is rw
275 ### -------------------------
277 if ( $options->{is} eq 'ro' ) {
279 "Cannot define an accessor name on a read-only attribute, accessors are read/write",
281 if exists $options->{accessor};
282 $options->{reader} ||= $name;
284 elsif ( $options->{is} eq 'rw' ) {
285 if ( $options->{writer} ) {
286 $options->{reader} ||= $name;
289 $options->{accessor} ||= $name;
292 elsif ( $options->{is} eq 'bare' ) {
294 # do nothing, but don't complain (later) about missing methods
297 $class->throw_error( "I do not understand this option (is => "
299 . ") on attribute ($name)", data => $options->{is} );
303 sub _process_isa_option {
304 my ( $class, $name, $options ) = @_;
306 return unless exists $options->{isa};
308 if ( exists $options->{does} ) {
309 if ( try { $options->{isa}->can('does') } ) {
310 ( $options->{isa}->does( $options->{does} ) )
311 || $class->throw_error(
312 "Cannot have an isa option and a does option if the isa does not do the does on attribute ($name)",
317 "Cannot have an isa option which cannot ->does() on attribute ($name)",
322 # allow for anon-subtypes here ...
323 if ( blessed( $options->{isa} )
324 && $options->{isa}->isa('Moose::Meta::TypeConstraint') ) {
325 $options->{type_constraint} = $options->{isa};
328 $options->{type_constraint}
329 = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint(
334 sub _process_does_option {
335 my ( $class, $name, $options ) = @_;
337 return unless exists $options->{does} && ! exists $options->{isa};
339 # allow for anon-subtypes here ...
340 if ( blessed( $options->{does} )
341 && $options->{does}->isa('Moose::Meta::TypeConstraint') ) {
342 $options->{type_constraint} = $options->{does};
345 $options->{type_constraint}
346 = Moose::Util::TypeConstraints::find_or_create_does_type_constraint(
351 sub _process_coerce_option {
352 my ( $class, $name, $options ) = @_;
354 return unless $options->{coerce};
356 ( exists $options->{type_constraint} )
357 || $class->throw_error(
358 "You cannot have coercion without specifying a type constraint on attribute ($name)",
362 "You cannot have a weak reference to a coerced value on attribute ($name)",
364 if $options->{weak_ref};
366 unless ( $options->{type_constraint}->has_coercion ) {
367 my $type = $options->{type_constraint}->name;
369 Moose::Deprecated::deprecated(
370 feature => 'coerce without coercion',
372 "You cannot coerce an attribute ($name) unless its type ($type) has a coercion"
377 sub _process_trigger_option {
378 my ( $class, $name, $options ) = @_;
380 return unless exists $options->{trigger};
382 ( 'CODE' eq ref $options->{trigger} )
383 || $class->throw_error("Trigger must be a CODE ref on attribute ($name)", data => $options->{trigger});
386 sub _process_auto_deref_option {
387 my ( $class, $name, $options ) = @_;
389 return unless $options->{auto_deref};
391 ( exists $options->{type_constraint} )
392 || $class->throw_error(
393 "You cannot auto-dereference without specifying a type constraint on attribute ($name)",
396 ( $options->{type_constraint}->is_a_type_of('ArrayRef')
397 || $options->{type_constraint}->is_a_type_of('HashRef') )
398 || $class->throw_error(
399 "You cannot auto-dereference anything other than a ArrayRef or HashRef on attribute ($name)",
403 sub _process_lazy_build_option {
404 my ( $class, $name, $options ) = @_;
406 return unless $options->{lazy_build};
409 "You can not use lazy_build and default for the same attribute ($name)",
411 if exists $options->{default};
413 $options->{lazy} = 1;
414 $options->{builder} ||= "_build_${name}";
416 if ( $name =~ /^_/ ) {
417 $options->{clearer} ||= "_clear${name}";
418 $options->{predicate} ||= "_has${name}";
421 $options->{clearer} ||= "clear_${name}";
422 $options->{predicate} ||= "has_${name}";
426 sub _process_lazy_option {
427 my ( $class, $name, $options ) = @_;
429 return unless $options->{lazy};
431 ( exists $options->{default} || defined $options->{builder} )
432 || $class->throw_error(
433 "You cannot have a lazy attribute ($name) without specifying a default value for it",
437 sub _process_required_option {
438 my ( $class, $name, $options ) = @_;
443 ( !exists $options->{init_arg} || defined $options->{init_arg} )
444 || exists $options->{default}
445 || defined $options->{builder}
449 "You cannot have a required attribute ($name) without a default, builder, or an init_arg",
454 sub initialize_instance_slot {
455 my ($self, $meta_instance, $instance, $params) = @_;
456 my $init_arg = $self->init_arg();
457 # try to fetch the init arg from the %params ...
461 if ( defined($init_arg) and exists $params->{$init_arg}) {
462 $val = $params->{$init_arg};
466 # skip it if it's lazy
467 return if $self->is_lazy;
468 # and die if it's required and doesn't have a default value
469 $self->throw_error("Attribute (" . $self->name . ") is required", object => $instance, data => $params)
470 if $self->is_required && !$self->has_default && !$self->has_builder;
472 # if nothing was in the %params, we can use the
473 # attribute's default value (if it has one)
474 if ($self->has_default) {
475 $val = $self->default($instance);
478 elsif ($self->has_builder) {
479 $val = $self->_call_builder($instance);
484 return unless $value_is_set;
486 $val = $self->_coerce_and_verify( $val, $instance );
488 $self->set_initial_value($instance, $val);
490 if ( ref $val && $self->is_weak_ref ) {
491 $self->_weaken_value($instance);
496 my ( $self, $instance ) = @_;
498 my $builder = $self->builder();
500 return $instance->$builder()
501 if $instance->can( $self->builder );
503 $self->throw_error( blessed($instance)
504 . " does not support builder method '"
506 . "' for attribute '"
515 sub _make_initializer_writer_callback {
517 my ($meta_instance, $instance, $slot_name) = @_;
518 my $old_callback = $self->SUPER::_make_initializer_writer_callback(@_);
520 $old_callback->($self->_coerce_and_verify($_[0], $instance));
525 my ($self, $instance, @args) = @_;
526 my $value = $args[0];
528 my $attr_name = quotemeta($self->name);
530 if ($self->is_required and not @args) {
531 $self->throw_error("Attribute ($attr_name) is required", object => $instance);
534 $value = $self->_coerce_and_verify( $value, $instance );
537 if ( $self->has_trigger && $self->has_value($instance) ) {
538 @old = $self->get_value($instance, 'for trigger');
541 $self->SUPER::set_value($instance, $value);
543 if ( ref $value && $self->is_weak_ref ) {
544 $self->_weaken_value($instance);
547 if ($self->has_trigger) {
548 $self->trigger->($instance, $value, @old);
552 sub _inline_set_value {
554 my ($instance, $value, $tc, $coercion, $tc_obj, $for_constructor) = @_;
558 $tc ||= '$type_constraint';
559 $coercion ||= '$type_coercion';
560 $tc_obj ||= '$type_constraint_obj';
563 if ($self->_writer_value_needs_copy) {
564 push @code, $self->_inline_copy_value($value, $copy);
568 # constructors already handle required checks
569 push @code, $self->_inline_check_required
570 unless $for_constructor;
572 push @code, $self->_inline_tc_code($value, $tc, $coercion, $tc_obj);
574 # constructors do triggers all at once at the end
575 push @code, $self->_inline_get_old_value_for_trigger($instance, $old)
576 unless $for_constructor;
579 $self->SUPER::_inline_set_value($instance, $value),
580 $self->_inline_weaken_value($instance, $value),
583 # constructors do triggers all at once at the end
584 push @code, $self->_inline_trigger($instance, $value, $old)
585 unless $for_constructor;
590 sub _writer_value_needs_copy {
592 return $self->should_coerce;
595 sub _inline_copy_value {
597 my ($value, $copy) = @_;
599 return 'my ' . $copy . ' = ' . $value . ';'
602 sub _inline_check_required {
605 return unless $self->is_required;
607 my $attr_name = quotemeta($self->name);
611 $self->_inline_throw_error(
612 '"Attribute (' . $attr_name . ') is required, so cannot '
613 . 'be set to undef"' # defined $_[1] is not good enough
619 sub _inline_tc_code {
621 my ($value, $tc, $coercion, $tc_obj, $is_lazy) = @_;
623 $self->_inline_check_coercion(
624 $value, $tc, $coercion, $is_lazy,
626 $self->_inline_check_constraint(
627 $value, $tc, $tc_obj, $is_lazy,
632 sub _inline_check_coercion {
634 my ($value, $tc, $coercion) = @_;
636 return unless $self->should_coerce && $self->type_constraint->has_coercion;
638 if ( $self->type_constraint->can_be_inlined ) {
640 'if (! (' . $self->type_constraint->_inline_check($value) . ')) {',
641 $value . ' = ' . $coercion . '->(' . $value . ');',
647 'if (!' . $tc . '->(' . $value . ')) {',
648 $value . ' = ' . $coercion . '->(' . $value . ');',
654 sub _inline_check_constraint {
656 my ($value, $tc, $tc_obj) = @_;
658 return unless $self->has_type_constraint;
660 my $attr_name = quotemeta($self->name);
662 if ( $self->type_constraint->can_be_inlined ) {
664 'if (! (' . $self->type_constraint->_inline_check($value) . ')) {',
665 $self->_inline_throw_error(
666 '"Attribute (' . $attr_name . ') does not pass the type '
667 . 'constraint because: " . '
668 . $tc_obj . '->get_message(' . $value . ')',
676 'if (!' . $tc . '->(' . $value . ')) {',
677 $self->_inline_throw_error(
678 '"Attribute (' . $attr_name . ') does not pass the type '
679 . 'constraint because: " . '
680 . $tc_obj . '->get_message(' . $value . ')',
688 sub _inline_get_old_value_for_trigger {
690 my ($instance, $old) = @_;
692 return unless $self->has_trigger;
695 'my ' . $old . ' = ' . $self->_inline_instance_has($instance),
696 '? ' . $self->_inline_instance_get($instance),
701 sub _inline_weaken_value {
703 my ($instance, $value) = @_;
705 return unless $self->is_weak_ref;
707 my $mi = $self->associated_class->get_meta_instance;
709 $mi->inline_weaken_slot_value($instance, $self->name, $value),
710 'if ref ' . $value . ';',
714 sub _inline_trigger {
716 my ($instance, $value, $old) = @_;
718 return unless $self->has_trigger;
720 return '$trigger->(' . $instance . ', ' . $value . ', ' . $old . ');';
724 my ( $self, $instance ) = @_;
726 my $meta_instance = Class::MOP::Class->initialize( blessed($instance) )
729 $meta_instance->weaken_slot_value( $instance, $self->name );
733 my ($self, $instance, $for_trigger) = @_;
735 if ($self->is_lazy) {
736 unless ($self->has_value($instance)) {
738 if ($self->has_default) {
739 $value = $self->default($instance);
740 } elsif ( $self->has_builder ) {
741 $value = $self->_call_builder($instance);
744 $value = $self->_coerce_and_verify( $value, $instance );
746 $self->set_initial_value($instance, $value);
750 if ( $self->should_auto_deref && ! $for_trigger ) {
752 my $type_constraint = $self->type_constraint;
754 if ($type_constraint->is_a_type_of('ArrayRef')) {
755 my $rv = $self->SUPER::get_value($instance);
756 return unless defined $rv;
757 return wantarray ? @{ $rv } : $rv;
759 elsif ($type_constraint->is_a_type_of('HashRef')) {
760 my $rv = $self->SUPER::get_value($instance);
761 return unless defined $rv;
762 return wantarray ? %{ $rv } : $rv;
765 $self->throw_error("Can not auto de-reference the type constraint '" . $type_constraint->name . "'", object => $instance, type_constraint => $type_constraint);
771 return $self->SUPER::get_value($instance);
775 sub _inline_get_value {
777 my ($instance, $tc, $coercion, $tc_obj) = @_;
779 my $slot_access = $self->_inline_instance_get($instance);
780 $tc ||= '$type_constraint';
781 $coercion ||= '$type_coercion';
782 $tc_obj ||= '$type_constraint_obj';
785 $self->_inline_check_lazy($instance, $tc, $coercion, $tc_obj),
786 $self->_inline_return_auto_deref($slot_access),
790 sub _inline_check_lazy {
792 my ($instance, $tc, $coercion, $tc_obj) = @_;
794 return unless $self->is_lazy;
796 my $slot_exists = $self->_inline_instance_has($instance);
799 'if (!' . $slot_exists . ') {',
800 $self->_inline_init_from_default($instance, '$default', $tc, $coercion, $tc_obj, 'lazy'),
805 sub _inline_init_from_default {
807 my ($instance, $default, $tc, $coercion, $tc_obj, $for_lazy) = @_;
809 if (!($self->has_default || $self->has_builder)) {
811 'You cannot have a lazy attribute '
812 . '(' . $self->name . ') '
813 . 'without specifying a default value for it',
819 $self->_inline_generate_default($instance, $default),
820 # intentionally not using _inline_tc_code, since that can be overridden
821 # to do things like possibly only do member tc checks, which isn't
822 # appropriate for checking the result of a default
823 $self->has_type_constraint
824 ? ($self->_inline_check_coercion($default, $tc, $coercion, $for_lazy),
825 $self->_inline_check_constraint($default, $tc, $tc_obj, $for_lazy))
827 $self->_inline_init_slot($instance, $default),
831 sub _inline_generate_default {
833 my ($instance, $default) = @_;
835 if ($self->has_default) {
836 my $source = 'my ' . $default . ' = $default';
837 $source .= '->(' . $instance . ')'
838 if $self->is_default_a_coderef;
839 return $source . ';';
841 elsif ($self->has_builder) {
842 my $builder = B::perlstring($self->builder);
843 my $builder_str = quotemeta($self->builder);
844 my $attr_name_str = quotemeta($self->name);
846 'my ' . $default . ';',
847 'if (my $builder = ' . $instance . '->can(' . $builder . ')) {',
848 $default . ' = ' . $instance . '->$builder;',
851 'my $class = ref(' . $instance . ') || ' . $instance . ';',
852 $self->_inline_throw_error(
853 '"$class does not support builder method '
854 . '\'' . $builder_str . '\' for attribute '
855 . '\'' . $attr_name_str . '\'"'
862 "Can't generate a default for " . $self->name
863 . " since no default or builder was specified"
868 sub _inline_init_slot {
870 my ($inv, $value) = @_;
872 if ($self->has_initializer) {
873 return '$attr->set_initial_value(' . $inv . ', ' . $value . ');';
876 return $self->_inline_instance_set($inv, $value) . ';';
880 sub _inline_return_auto_deref {
883 return 'return ' . $self->_auto_deref(@_) . ';';
888 my ($ref_value) = @_;
890 return $ref_value unless $self->should_auto_deref;
892 my $type_constraint = $self->type_constraint;
895 if ($type_constraint->is_a_type_of('ArrayRef')) {
898 elsif ($type_constraint->is_a_type_of('HashRef')) {
903 'Can not auto de-reference the type constraint \''
904 . $type_constraint->name
906 type_constraint => $type_constraint,
911 . '? ' . $sigil . '{ (' . $ref_value . ') || return } '
912 . ': (' . $ref_value . ')';
915 ## installing accessors
917 sub accessor_metaclass { 'Moose::Meta::Method::Accessor' }
919 sub install_accessors {
921 $self->SUPER::install_accessors(@_);
922 $self->install_delegation if $self->has_handles;
926 sub _check_associated_methods {
929 @{ $self->associated_methods }
930 || ($self->_is_metadata || '') eq 'bare'
933 'Attribute (' . $self->name . ') of class '
934 . $self->associated_class->name
935 . ' has no associated methods'
936 . ' (did you mean to provide an "is" argument?)'
942 sub _process_accessors {
944 my ($type, $accessor, $generate_as_inline_methods) = @_;
946 $accessor = ( keys %$accessor )[0] if ( ref($accessor) || '' ) eq 'HASH';
947 my $method = $self->associated_class->get_method($accessor);
950 && $method->isa('Class::MOP::Method::Accessor')
951 && $method->associated_attribute->name ne $self->name ) {
953 my $other_attr_name = $method->associated_attribute->name;
954 my $name = $self->name;
957 "You are overwriting an accessor ($accessor) for the $other_attr_name attribute"
958 . " with a new accessor method for the $name attribute" );
963 && !$method->isa('Class::MOP::Method::Accessor')
964 && ( !$self->definition_context
965 || $method->package_name eq $self->definition_context->{package} )
969 "You are overwriting a locally defined method ($accessor) with "
973 if ( !$self->associated_class->has_method($accessor)
974 && $self->associated_class->has_package_symbol( '&' . $accessor ) ) {
977 "You are overwriting a locally defined function ($accessor) with "
981 $self->SUPER::_process_accessors(@_);
984 sub remove_accessors {
986 $self->SUPER::remove_accessors(@_);
987 $self->remove_delegation if $self->has_handles;
991 sub install_delegation {
995 # Here we canonicalize the 'handles' option
996 # this will sort out any details and always
997 # return an hash of methods which we want
998 # to delagate to, see that method for details
999 my %handles = $self->_canonicalize_handles;
1002 # install the delegation ...
1003 my $associated_class = $self->associated_class;
1004 foreach my $handle (keys %handles) {
1005 my $method_to_call = $handles{$handle};
1006 my $class_name = $associated_class->name;
1007 my $name = "${class_name}::${handle}";
1009 (!$associated_class->has_method($handle))
1010 || $self->throw_error("You cannot overwrite a locally defined method ($handle) with a delegation", method_name => $handle);
1013 # handles is not allowed to delegate
1014 # any of these methods, as they will
1015 # override the ones in your class, which
1016 # is almost certainly not what you want.
1018 # FIXME warn when $handle was explicitly specified, but not if the source is a regex or something
1019 #cluck("Not delegating method '$handle' because it is a core method") and
1020 next if $class_name->isa("Moose::Object") and $handle =~ /^BUILD|DEMOLISH$/ || Moose::Object->can($handle);
1022 my $method = $self->_make_delegation_method($handle, $method_to_call);
1024 $self->associated_class->add_method($method->name, $method);
1025 $self->associate_method($method);
1029 sub remove_delegation {
1031 my %handles = $self->_canonicalize_handles;
1032 my $associated_class = $self->associated_class;
1033 foreach my $handle (keys %handles) {
1034 next unless any { $handle eq $_ }
1036 @{ $self->associated_methods };
1037 $self->associated_class->remove_method($handle);
1041 # private methods to help delegation ...
1043 sub _canonicalize_handles {
1045 my $handles = $self->handles;
1046 if (my $handle_type = ref($handles)) {
1047 if ($handle_type eq 'HASH') {
1050 elsif ($handle_type eq 'ARRAY') {
1051 return map { $_ => $_ } @{$handles};
1053 elsif ($handle_type eq 'Regexp') {
1054 ($self->has_type_constraint)
1055 || $self->throw_error("Cannot delegate methods based on a Regexp without a type constraint (isa)", data => $handles);
1056 return map { ($_ => $_) }
1057 grep { /$handles/ } $self->_get_delegate_method_list;
1059 elsif ($handle_type eq 'CODE') {
1060 return $handles->($self, $self->_find_delegate_metaclass);
1062 elsif (blessed($handles) && $handles->isa('Moose::Meta::TypeConstraint::DuckType')) {
1063 return map { $_ => $_ } @{ $handles->methods };
1065 elsif (blessed($handles) && $handles->isa('Moose::Meta::TypeConstraint::Role')) {
1066 $handles = $handles->role;
1069 $self->throw_error("Unable to canonicalize the 'handles' option with $handles", data => $handles);
1073 Class::MOP::load_class($handles);
1074 my $role_meta = Class::MOP::class_of($handles);
1076 (blessed $role_meta && $role_meta->isa('Moose::Meta::Role'))
1077 || $self->throw_error("Unable to canonicalize the 'handles' option with $handles because its metaclass is not a Moose::Meta::Role", data => $handles);
1079 return map { $_ => $_ }
1081 grep { !$_->isa('Class::MOP::Method::Meta') } (
1082 $role_meta->_get_local_methods,
1083 $role_meta->get_required_method_list,
1087 sub _get_delegate_method_list {
1089 my $meta = $self->_find_delegate_metaclass;
1090 if ($meta->isa('Class::MOP::Class')) {
1091 return map { $_->name } # NOTE: !never! delegate &meta
1092 grep { $_->package_name ne 'Moose::Object' && !$_->isa('Class::MOP::Method::Meta') }
1093 $meta->get_all_methods;
1095 elsif ($meta->isa('Moose::Meta::Role')) {
1096 return $meta->get_method_list;
1099 $self->throw_error("Unable to recognize the delegate metaclass '$meta'", data => $meta);
1103 sub _find_delegate_metaclass {
1105 if (my $class = $self->_isa_metadata) {
1106 unless ( Class::MOP::is_class_loaded($class) ) {
1109 'The %s attribute is trying to delegate to a class which has not been loaded - %s',
1114 # we might be dealing with a non-Moose class,
1115 # and need to make our own metaclass. if there's
1116 # already a metaclass, it will be returned
1117 return Class::MOP::Class->initialize($class);
1119 elsif (my $role = $self->_does_metadata) {
1120 unless ( Class::MOP::is_class_loaded($class) ) {
1123 'The %s attribute is trying to delegate to a role which has not been loaded - %s',
1129 return Class::MOP::class_of($role);
1132 $self->throw_error("Cannot find delegate metaclass for attribute " . $self->name);
1136 sub delegation_metaclass { 'Moose::Meta::Method::Delegation' }
1138 sub _make_delegation_method {
1139 my ( $self, $handle_name, $method_to_call ) = @_;
1141 my @curried_arguments;
1143 ($method_to_call, @curried_arguments) = @$method_to_call
1144 if 'ARRAY' eq ref($method_to_call);
1146 return $self->delegation_metaclass->new(
1147 name => $handle_name,
1148 package_name => $self->associated_class->name,
1150 delegate_to_method => $method_to_call,
1151 curried_arguments => \@curried_arguments,
1155 sub _coerce_and_verify {
1158 my $instance = shift;
1160 return $val unless $self->has_type_constraint;
1162 $val = $self->type_constraint->coerce($val)
1163 if $self->should_coerce && $self->type_constraint->has_coercion;
1165 $self->verify_against_type_constraint($val, instance => $instance);
1170 sub verify_against_type_constraint {
1174 return 1 if !$self->has_type_constraint;
1176 my $type_constraint = $self->type_constraint;
1178 $type_constraint->check($val)
1179 || $self->throw_error("Attribute ("
1181 . ") does not pass the type constraint because: "
1182 . $type_constraint->get_message($val), data => $val, @_);
1185 package Moose::Meta::Attribute::Custom::Moose;
1186 sub register_implementation { 'Moose::Meta::Attribute' }
1190 # ABSTRACT: The Moose attribute metaclass
1198 This class is a subclass of L<Class::MOP::Attribute> that provides
1199 additional Moose-specific functionality.
1201 To really understand this class, you will need to start with the
1202 L<Class::MOP::Attribute> documentation. This class can be understood
1203 as a set of additional features on top of the basic feature provided
1204 by that parent class.
1208 C<Moose::Meta::Attribute> is a subclass of L<Class::MOP::Attribute>.
1212 Many of the documented below override methods in
1213 L<Class::MOP::Attribute> and add Moose specific features.
1219 =item B<< Moose::Meta::Attribute->new(%options) >>
1221 This method overrides the L<Class::MOP::Attribute> constructor.
1223 Many of the options below are described in more detail in the
1224 L<Moose::Manual::Attributes> document.
1226 It adds the following options to the constructor:
1230 =item * is => 'ro', 'rw', 'bare'
1232 This provides a shorthand for specifying the C<reader>, C<writer>, or
1233 C<accessor> names. If the attribute is read-only ('ro') then it will
1234 have a C<reader> method with the same attribute as the name.
1236 If it is read-write ('rw') then it will have an C<accessor> method
1237 with the same name. If you provide an explicit C<writer> for a
1238 read-write attribute, then you will have a C<reader> with the same
1239 name as the attribute, and a C<writer> with the name you provided.
1241 Use 'bare' when you are deliberately not installing any methods
1242 (accessor, reader, etc.) associated with this attribute; otherwise,
1243 Moose will issue a deprecation warning when this attribute is added to a
1246 =item * isa => $type
1248 This option accepts a type. The type can be a string, which should be
1249 a type name. If the type name is unknown, it is assumed to be a class
1252 This option can also accept a L<Moose::Meta::TypeConstraint> object.
1254 If you I<also> provide a C<does> option, then your C<isa> option must
1255 be a class name, and that class must do the role specified with
1258 =item * does => $role
1260 This is short-hand for saying that the attribute's type must be an
1261 object which does the named role.
1263 =item * coerce => $bool
1265 This option is only valid for objects with a type constraint
1266 (C<isa>) that defined a coercion. If this is true, then coercions will be applied whenever
1267 this attribute is set.
1269 You can make both this and the C<weak_ref> option true.
1271 =item * trigger => $sub
1273 This option accepts a subroutine reference, which will be called after
1274 the attribute is set.
1276 =item * required => $bool
1278 An attribute which is required must be provided to the constructor. An
1279 attribute which is required can also have a C<default> or C<builder>,
1280 which will satisfy its required-ness.
1282 A required attribute must have a C<default>, C<builder> or a
1283 non-C<undef> C<init_arg>
1285 =item * lazy => $bool
1287 A lazy attribute must have a C<default> or C<builder>. When an
1288 attribute is lazy, the default value will not be calculated until the
1291 =item * weak_ref => $bool
1293 If this is true, the attribute's value will be stored as a weak
1296 =item * auto_deref => $bool
1298 If this is true, then the reader will dereference the value when it is
1299 called. The attribute must have a type constraint which defines the
1300 attribute as an array or hash reference.
1302 =item * lazy_build => $bool
1304 Setting this to true makes the attribute lazy and provides a number of
1312 is equivalent to this:
1317 builder => '_build_size',
1318 clearer => 'clear_size',
1319 predicate => 'has_size',
1323 If your attribute name starts with an underscore (C<_>), then the clearer
1324 and predicate will as well:
1336 builder => '_build__size',
1337 clearer => '_clear_size',
1338 predicate => '_has_size',
1341 Note the doubled underscore in the builder name. Internally, Moose
1342 simply prepends the attribute name with "_build_" to come up with the
1345 =item * documentation
1347 An arbitrary string that can be retrieved later by calling C<<
1348 $attr->documentation >>.
1352 =item B<< $attr->clone(%options) >>
1354 This creates a new attribute based on attribute being cloned. You must
1355 supply a C<name> option to provide a new name for the attribute.
1357 The C<%options> can only specify options handled by
1358 L<Class::MOP::Attribute>.
1362 =head2 Value management
1366 =item B<< $attr->initialize_instance_slot($meta_instance, $instance, $params) >>
1368 This method is used internally to initialize the attribute's slot in
1369 the object C<$instance>.
1371 This overrides the L<Class::MOP::Attribute> method to handle lazy
1372 attributes, weak references, and type constraints.
1378 eval { $point->meta->get_attribute('x')->set_value($point, 'forty-two') };
1383 I<Attribute (x) does not pass the type constraint (Int) with 'forty-two'>
1385 Before setting the value, a check is made on the type constraint of
1386 the attribute, if it has one, to see if the value passes it. If the
1387 value fails to pass, the set operation dies with a L</throw_error>.
1389 Any coercion to convert values is done before checking the type constraint.
1391 To check a value against a type constraint before setting it, fetch the
1392 attribute instance using L<Class::MOP::Class/find_attribute_by_name>,
1393 fetch the type_constraint from the attribute using L<Moose::Meta::Attribute/type_constraint>
1394 and call L<Moose::Meta::TypeConstraint/check>. See L<Moose::Cookbook::Basics::Recipe4>
1399 =head2 Attribute Accessor generation
1403 =item B<< $attr->install_accessors >>
1405 This method overrides the parent to also install delegation methods.
1407 If, after installing all methods, the attribute object has no associated
1408 methods, it throws an error unless C<< is => 'bare' >> was passed to the
1409 attribute constructor. (Trying to add an attribute that has no associated
1410 methods is almost always an error.)
1412 =item B<< $attr->remove_accessors >>
1414 This method overrides the parent to also remove delegation methods.
1416 =item B<< $attr->inline_set($instance_var, $value_var) >>
1418 This method return a code snippet suitable for inlining the relevant
1419 operation. It expect strings containing variable names to be used in the
1420 inlining, like C<'$self'> or C<'$_[1]'>.
1422 =item B<< $attr->install_delegation >>
1424 This method adds its delegation methods to the attribute's associated
1425 class, if it has any to add.
1427 =item B<< $attr->remove_delegation >>
1429 This method remove its delegation methods from the attribute's
1432 =item B<< $attr->accessor_metaclass >>
1434 Returns the accessor metaclass name, which defaults to
1435 L<Moose::Meta::Method::Accessor>.
1437 =item B<< $attr->delegation_metaclass >>
1439 Returns the delegation metaclass name, which defaults to
1440 L<Moose::Meta::Method::Delegation>.
1444 =head2 Additional Moose features
1446 These methods are not found in the superclass. They support features
1451 =item B<< $attr->does($role) >>
1453 This indicates whether the I<attribute itself> does the given
1454 role. The role can be given as a full class name, or as a resolvable
1457 Note that this checks the attribute itself, not its type constraint,
1458 so it is checking the attribute's metaclass and any traits applied to
1461 =item B<< Moose::Meta::Class->interpolate_class_and_new($name, %options) >>
1463 This is an alternate constructor that handles the C<metaclass> and
1466 Effectively, this method is a factory that finds or creates the
1467 appropriate class for the given C<metaclass> and/or C<traits>.
1469 Once it has the appropriate class, it will call C<< $class->new($name,
1470 %options) >> on that class.
1472 =item B<< $attr->clone_and_inherit_options(%options) >>
1474 This method supports the C<has '+foo'> feature. It does various bits
1475 of processing on the supplied C<%options> before ultimately calling
1476 the C<clone> method.
1478 One of its main tasks is to make sure that the C<%options> provided
1479 does not include the options returned by the
1480 C<illegal_options_for_inheritance> method.
1482 =item B<< $attr->illegal_options_for_inheritance >>
1484 This returns a blacklist of options that can not be overridden in a
1485 subclass's attribute definition.
1487 This exists to allow a custom metaclass to change or add to the list
1488 of options which can not be changed.
1490 =item B<< $attr->type_constraint >>
1492 Returns the L<Moose::Meta::TypeConstraint> object for this attribute,
1495 =item B<< $attr->has_type_constraint >>
1497 Returns true if this attribute has a type constraint.
1499 =item B<< $attr->verify_against_type_constraint($value) >>
1501 Given a value, this method returns true if the value is valid for the
1502 attribute's type constraint. If the value is not valid, it throws an
1505 =item B<< $attr->handles >>
1507 This returns the value of the C<handles> option passed to the
1510 =item B<< $attr->has_handles >>
1512 Returns true if this attribute performs delegation.
1514 =item B<< $attr->is_weak_ref >>
1516 Returns true if this attribute stores its value as a weak reference.
1518 =item B<< $attr->is_required >>
1520 Returns true if this attribute is required to have a value.
1522 =item B<< $attr->is_lazy >>
1524 Returns true if this attribute is lazy.
1526 =item B<< $attr->is_lazy_build >>
1528 Returns true if the C<lazy_build> option was true when passed to the
1531 =item B<< $attr->should_coerce >>
1533 Returns true if the C<coerce> option passed to the constructor was
1536 =item B<< $attr->should_auto_deref >>
1538 Returns true if the C<auto_deref> option passed to the constructor was
1541 =item B<< $attr->trigger >>
1543 This is the subroutine reference that was in the C<trigger> option
1544 passed to the constructor, if any.
1546 =item B<< $attr->has_trigger >>
1548 Returns true if this attribute has a trigger set.
1550 =item B<< $attr->documentation >>
1552 Returns the value that was in the C<documentation> option passed to
1553 the constructor, if any.
1555 =item B<< $attr->has_documentation >>
1557 Returns true if this attribute has any documentation.
1559 =item B<< $attr->applied_traits >>
1561 This returns an array reference of all the traits which were applied
1562 to this attribute. If none were applied, this returns C<undef>.
1564 =item B<< $attr->has_applied_traits >>
1566 Returns true if this attribute has any traits applied.
1572 See L<Moose/BUGS> for details on reporting bugs.