X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMoose%2FMeta%2FAttribute.pm;h=632da0209e34bed321428ca83fdded28e7bacaeb;hb=8aeb5bfd7b7da4622832804b39ee0179e816f7a1;hp=51099d4a1a3e85cb6595392e598f75177481a46c;hpb=b2df9268ec9fe2dafce9522eefc8d1dcd456fdd1;p=gitmo%2FMoose.git diff --git a/lib/Moose/Meta/Attribute.pm b/lib/Moose/Meta/Attribute.pm index 51099d4..632da02 100644 --- a/lib/Moose/Meta/Attribute.pm +++ b/lib/Moose/Meta/Attribute.pm @@ -5,9 +5,11 @@ use strict; use warnings; use Scalar::Util 'blessed', 'weaken'; +use List::MoreUtils 'any'; +use Try::Tiny; use overload (); -our $VERSION = '0.72_01'; +our $VERSION = '0.94'; our $AUTHORITY = 'cpan:STEVAN'; use Moose::Meta::Method::Accessor; @@ -15,49 +17,20 @@ use Moose::Meta::Method::Delegation; use Moose::Util (); use Moose::Util::TypeConstraints (); -use base 'Class::MOP::Attribute'; - -# options which are not directly used -# but we store them for metadata purposes -__PACKAGE__->meta->add_attribute('isa' => (reader => '_isa_metadata')); -__PACKAGE__->meta->add_attribute('does' => (reader => '_does_metadata')); -__PACKAGE__->meta->add_attribute('is' => (reader => '_is_metadata')); - -# these are actual options for the attrs -__PACKAGE__->meta->add_attribute('required' => (reader => 'is_required' )); -__PACKAGE__->meta->add_attribute('lazy' => (reader => 'is_lazy' )); -__PACKAGE__->meta->add_attribute('lazy_build' => (reader => 'is_lazy_build' )); -__PACKAGE__->meta->add_attribute('coerce' => (reader => 'should_coerce' )); -__PACKAGE__->meta->add_attribute('weak_ref' => (reader => 'is_weak_ref' )); -__PACKAGE__->meta->add_attribute('auto_deref' => (reader => 'should_auto_deref')); -__PACKAGE__->meta->add_attribute('type_constraint' => ( - reader => 'type_constraint', - predicate => 'has_type_constraint', -)); -__PACKAGE__->meta->add_attribute('trigger' => ( - reader => 'trigger', - predicate => 'has_trigger', -)); -__PACKAGE__->meta->add_attribute('handles' => ( - reader => 'handles', - predicate => 'has_handles', -)); -__PACKAGE__->meta->add_attribute('documentation' => ( - reader => 'documentation', - predicate => 'has_documentation', -)); +use base 'Class::MOP::Attribute', 'Moose::Meta::Mixin::AttributeCore'; + __PACKAGE__->meta->add_attribute('traits' => ( reader => 'applied_traits', predicate => 'has_applied_traits', )); -# we need to have a ->does method in here to -# more easily support traits, and the introspection +# we need to have a ->does method in here to +# more easily support traits, and the introspection # of those traits. We extend the does check to look # for metatrait aliases. sub does { my ($self, $role_name) = @_; - my $name = eval { + my $name = try { Moose::Util::resolve_metatrait_alias(Attribute => $role_name) }; return 0 if !defined($name); # failed to load class @@ -77,28 +50,45 @@ sub throw_error { sub new { my ($class, $name, %options) = @_; $class->_process_options($name, \%options) unless $options{__hack_no_process_options}; # used from clone()... YECHKKK FIXME ICKY YUCK GROSS + + delete $options{__hack_no_process_options}; + + my %attrs = + ( map { $_ => 1 } + grep { defined } + map { $_->init_arg() } + $class->meta()->get_all_attributes() + ); + + my @bad = sort grep { ! $attrs{$_} } keys %options; + + if (@bad) + { + Carp::cluck "Found unknown argument(s) passed to '$name' attribute constructor in '$class': @bad"; + } + return $class->SUPER::new($name, %options); } sub interpolate_class_and_new { - my ($class, $name, @args) = @_; + my ($class, $name, %args) = @_; - my ( $new_class, @traits ) = $class->interpolate_class(@args); - - $new_class->new($name, @args, ( scalar(@traits) ? ( traits => \@traits ) : () ) ); + my ( $new_class, @traits ) = $class->interpolate_class(\%args); + + $new_class->new($name, %args, ( scalar(@traits) ? ( traits => \@traits ) : () ) ); } sub interpolate_class { - my ($class, %options) = @_; + my ($class, $options) = @_; $class = ref($class) || $class; - if ( my $metaclass_name = delete $options{metaclass} ) { + if ( my $metaclass_name = delete $options->{metaclass} ) { my $new_class = Moose::Util::resolve_metaclass_alias( Attribute => $metaclass_name ); - + if ( $class ne $new_class ) { if ( $new_class->can("interpolate_class") ) { - return $new_class->interpolate_class(%options); + return $new_class->interpolate_class($options); } else { $class = $new_class; } @@ -107,7 +97,7 @@ sub interpolate_class { my @traits; - if (my $traits = $options{traits}) { + if (my $traits = $options->{traits}) { my $i = 0; while ($i < @$traits) { my $trait = $traits->[$i++]; @@ -142,53 +132,53 @@ sub interpolate_class { # ... my @legal_options_for_inheritance = qw( - default coerce required - documentation lazy handles + default coerce required + documentation lazy handles builder type_constraint definition_context - lazy_build + lazy_build weak_ref ); sub legal_options_for_inheritance { @legal_options_for_inheritance } # NOTE/TODO -# This method *must* be able to handle -# Class::MOP::Attribute instances as -# well. Yes, I know that is wrong, but -# apparently we didn't realize it was -# doing that and now we have some code -# which is dependent on it. The real -# solution of course is to push this +# This method *must* be able to handle +# Class::MOP::Attribute instances as +# well. Yes, I know that is wrong, but +# apparently we didn't realize it was +# doing that and now we have some code +# which is dependent on it. The real +# solution of course is to push this # feature back up into Class::MOP::Attribute # but I not right now, I am too lazy. -# However if you are reading this and -# looking for something to do,.. please +# However if you are reading this and +# looking for something to do,.. please # be my guest. # - stevan sub clone_and_inherit_options { my ($self, %options) = @_; - + my %copy = %options; - + my %actual_options; - + # NOTE: # we may want to extends a Class::MOP::Attribute - # in which case we need to be able to use the - # core set of legal options that have always + # in which case we need to be able to use the + # core set of legal options that have always # been here. But we allows Moose::Meta::Attribute # instances to changes them. # - SL my @legal_options = $self->can('legal_options_for_inheritance') ? $self->legal_options_for_inheritance : @legal_options_for_inheritance; - + foreach my $legal_option (@legal_options) { if (exists $options{$legal_option}) { $actual_options{$legal_option} = $options{$legal_option}; delete $options{$legal_option}; } - } + } if ($options{isa}) { my $type_constraint; @@ -204,7 +194,7 @@ sub clone_and_inherit_options { $actual_options{type_constraint} = $type_constraint; delete $options{isa}; } - + if ($options{does}) { my $type_constraint; if (blessed($options{does}) && $options{does}->isa('Moose::Meta::TypeConstraint')) { @@ -218,14 +208,14 @@ sub clone_and_inherit_options { $actual_options{type_constraint} = $type_constraint; delete $options{does}; - } + } # NOTE: - # this doesn't apply to Class::MOP::Attributes, + # this doesn't apply to Class::MOP::Attributes, # so we can ignore it for them. # - SL if ($self->can('interpolate_class')) { - ( $actual_options{metaclass}, my @traits ) = $self->interpolate_class(%options); + ( $actual_options{metaclass}, my @traits ) = $self->interpolate_class(\%options); my %seen; my @all_traits = grep { $seen{$_}++ } @{ $self->applied_traits || [] }, @traits; @@ -244,11 +234,11 @@ sub clone_and_inherit_options { sub clone { my ( $self, %params ) = @_; - my $class = $params{metaclass} || ref $self; + my $class = delete $params{metaclass} || ref $self; my ( @init, @non_init ); - foreach my $attr ( grep { $_->has_value($self) } $self->meta->get_all_attributes ) { + foreach my $attr ( grep { $_->has_value($self) } Class::MOP::class_of($self)->get_all_attributes ) { push @{ $attr->has_init_arg ? \@init : \@non_init }, $attr; } @@ -276,7 +266,7 @@ sub _process_options { ## is => rw, accessor => _foo # turns into (accessor => _foo) ## is => ro, accessor => _foo # error, accesor is rw ### ------------------------- - + if ($options->{is} eq 'ro') { $class->throw_error("Cannot define an accessor name on a read-only attribute, accessors are read/write", data => $options) if exists $options->{accessor}; @@ -290,6 +280,9 @@ sub _process_options { $options->{accessor} ||= $name; } } + elsif ($options->{is} eq 'bare') { + # do nothing, but don't complain (later) about missing methods + } else { $class->throw_error("I do not understand this option (is => " . $options->{is} . ") on attribute ($name)", data => $options->{is}); } @@ -297,7 +290,7 @@ sub _process_options { if (exists $options->{isa}) { if (exists $options->{does}) { - if (eval { $options->{isa}->can('does') }) { + if (try { $options->{isa}->can('does') }) { ($options->{isa}->does($options->{does})) || $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); } @@ -348,12 +341,11 @@ sub _process_options { $class->throw_error("You can not use lazy_build and default for the same attribute ($name)", data => $options) if exists $options->{default}; $options->{lazy} = 1; - $options->{required} = 1; $options->{builder} ||= "_build_${name}"; if ($name =~ /^_/) { $options->{clearer} ||= "_clear${name}"; $options->{predicate} ||= "_has${name}"; - } + } else { $options->{clearer} ||= "clear_${name}"; $options->{predicate} ||= "has_${name}"; @@ -380,7 +372,7 @@ sub initialize_instance_slot { my $value_is_set; if ( defined($init_arg) and exists $params->{$init_arg}) { $val = $params->{$init_arg}; - $value_is_set = 1; + $value_is_set = 1; } else { # skip it if it's lazy @@ -394,7 +386,7 @@ sub initialize_instance_slot { if ($self->has_default) { $val = $self->default($instance); $value_is_set = 1; - } + } elsif ($self->has_builder) { $val = $self->_call_builder($instance); $value_is_set = 1; @@ -403,17 +395,13 @@ sub initialize_instance_slot { return unless $value_is_set; - if ($self->has_type_constraint) { - my $type_constraint = $self->type_constraint; - if ($self->should_coerce && $type_constraint->has_coercion) { - $val = $type_constraint->coerce($val); - } - $self->verify_against_type_constraint($val, instance => $instance); - } + $val = $self->_coerce_and_verify( $val, $instance ); $self->set_initial_value($instance, $val); - $meta_instance->weaken_slot_value($instance, $self->name) - if ref $val && $self->is_weak_ref; + + if ( ref $val && $self->is_weak_ref ) { + $self->_weaken_value($instance); + } } sub _call_builder { @@ -437,8 +425,8 @@ sub _call_builder { ## Slot management # FIXME: -# this duplicates too much code from -# Class::MOP::Attribute, we need to +# this duplicates too much code from +# Class::MOP::Attribute, we need to # refactor these bits eventually. # - SL sub _set_initial_slot_value { @@ -456,15 +444,11 @@ sub _set_initial_slot_value { } my $callback = sub { - my $val = shift; - if ($type_constraint) { - $val = $type_constraint->coerce($val) - if $can_coerce; - $self->verify_against_type_constraint($val, object => $instance); - } + my $val = $self->_coerce_and_verify( shift, $instance );; + $meta_instance->set_slot_value($instance, $slot_name, $val); }; - + my $initializer = $self->initializer; # most things will just want to set a value, so make it first arg @@ -481,36 +465,35 @@ sub set_value { $self->throw_error("Attribute ($attr_name) is required", object => $instance); } - if ($self->has_type_constraint) { + $value = $self->_coerce_and_verify( $value, $instance ); - my $type_constraint = $self->type_constraint; - - if ($self->should_coerce) { - $value = $type_constraint->coerce($value); - } - $type_constraint->_compiled_type_constraint->($value) - || $self->throw_error("Attribute (" - . $self->name - . ") does not pass the type constraint because " - . $type_constraint->get_message($value), object => $instance, data => $value); + my @old; + if ( $self->has_trigger && $self->has_value($instance) ) { + @old = $self->get_value($instance, 'for trigger'); } - my $meta_instance = Class::MOP::Class->initialize(blessed($instance)) - ->get_meta_instance; - - $meta_instance->set_slot_value($instance, $attr_name, $value); + $self->SUPER::set_value($instance, $value); - if (ref $value && $self->is_weak_ref) { - $meta_instance->weaken_slot_value($instance, $attr_name); + if ( ref $value && $self->is_weak_ref ) { + $self->_weaken_value($instance); } if ($self->has_trigger) { - $self->trigger->($instance, $value); + $self->trigger->($instance, $value, @old); } } +sub _weaken_value { + my ( $self, $instance ) = @_; + + my $meta_instance = Class::MOP::Class->initialize( blessed($instance) ) + ->get_meta_instance; + + $meta_instance->weaken_slot_value( $instance, $self->name ); +} + sub get_value { - my ($self, $instance) = @_; + my ($self, $instance, $for_trigger) = @_; if ($self->is_lazy) { unless ($self->has_value($instance)) { @@ -520,17 +503,14 @@ sub get_value { } elsif ( $self->has_builder ) { $value = $self->_call_builder($instance); } - if ($self->has_type_constraint) { - my $type_constraint = $self->type_constraint; - $value = $type_constraint->coerce($value) - if ($self->should_coerce); - $self->verify_against_type_constraint($value); - } + + $value = $self->_coerce_and_verify( $value, $instance ); + $self->set_initial_value($instance, $value); } } - if ($self->should_auto_deref) { + if ( $self->should_auto_deref && ! $for_trigger ) { my $type_constraint = $self->type_constraint; @@ -562,10 +542,42 @@ sub accessor_metaclass { 'Moose::Meta::Method::Accessor' } sub install_accessors { my $self = shift; $self->SUPER::install_accessors(@_); - $self->install_delegation if $self->has_handles; + $self->install_delegation(@_); return; } +sub _check_associated_methods { + my $self = shift; + unless ( + @{ $self->associated_methods } + || ($self->_is_metadata || '') eq 'bare' + ) { + Carp::cluck( + 'Attribute (' . $self->name . ') of class ' + . $self->associated_class->name + . ' has no associated methods' + . ' (did you mean to provide an "is" argument?)' + . "\n" + ) + } +} + +sub _process_accessors { + my $self = shift; + my ($type, $accessor, $generate_as_inline_methods) = @_; + $accessor = (keys %$accessor)[0] if (ref($accessor)||'') eq 'HASH'; + my $method = $self->associated_class->get_method($accessor); + if ($method && !$method->isa('Class::MOP::Method::Accessor') + && (!$self->definition_context + || $method->package_name eq $self->definition_context->{package})) { + Carp::cluck( + "You are overwriting a locally defined method ($accessor) with " + . "an accessor" + ); + } + $self->SUPER::_process_accessors(@_); +} + sub remove_accessors { my $self = shift; $self->SUPER::remove_accessors(@_); @@ -575,6 +587,9 @@ sub remove_accessors { sub install_delegation { my $self = shift; + my $inline = shift; + + return unless $self->has_handles; # NOTE: # Here we canonicalize the 'handles' option @@ -591,8 +606,12 @@ sub install_delegation { my $class_name = $associated_class->name; my $name = "${class_name}::${handle}"; - (!$associated_class->has_method($handle)) - || $self->throw_error("You cannot overwrite a locally defined method ($handle) with a delegation", method_name => $handle); + if ($associated_class->has_method($handle)) { + $self->throw_error( + "You cannot overwrite a locally defined method ($handle) with a delegation", + method_name => $handle + ) unless $inline; + } # NOTE: # handles is not allowed to delegate @@ -604,10 +623,11 @@ sub install_delegation { #cluck("Not delegating method '$handle' because it is a core method") and next if $class_name->isa("Moose::Object") and $handle =~ /^BUILD|DEMOLISH$/ || Moose::Object->can($handle); - my $method = $self->_make_delegation_method($handle, $method_to_call); + my $method = $self->_make_delegation_method($handle, $method_to_call, $inline); $self->associated_class->add_method($method->name, $method); - } + $self->associate_method($method); + } } sub remove_delegation { @@ -615,6 +635,9 @@ sub remove_delegation { my %handles = $self->_canonicalize_handles; my $associated_class = $self->associated_class; foreach my $handle (keys %handles) { + next unless any { $handle eq $_ } + map { $_->name } + @{ $self->associated_methods }; $self->associated_class->remove_method($handle); } } @@ -640,45 +663,38 @@ sub _canonicalize_handles { elsif ($handle_type eq 'CODE') { return $handles->($self, $self->_find_delegate_metaclass); } + elsif (blessed($handles) && $handles->isa('Moose::Meta::TypeConstraint::DuckType')) { + return map { $_ => $_ } @{ $handles->methods }; + } else { $self->throw_error("Unable to canonicalize the 'handles' option with $handles", data => $handles); } } else { - Class::MOP::load_class($handles) - unless Class::MOP::is_class_loaded($handles); - - my $role_meta = eval { $handles->meta }; - if ($@) { - $self->throw_error("Unable to canonicalize the 'handles' option with $handles because : $@", data => $handles, error => $@); - } + Class::MOP::load_class($handles); + my $role_meta = Class::MOP::class_of($handles); (blessed $role_meta && $role_meta->isa('Moose::Meta::Role')) - || $self->throw_error("Unable to canonicalize the 'handles' option with $handles because ->meta is not a Moose::Meta::Role", data => $handles); - - return map { $_ => $_ } ( + || $self->throw_error("Unable to canonicalize the 'handles' option with $handles because its metaclass is not a Moose::Meta::Role", data => $handles); + + return map { $_ => $_ } + grep { $_ ne 'meta' } ( $role_meta->get_method_list, - $role_meta->get_required_method_list - ); + map { $_->name } $role_meta->get_required_method_list, + ); } } sub _find_delegate_metaclass { my $self = shift; if (my $class = $self->_isa_metadata) { - # if the class does have - # a meta method, use it - return $class->meta if $class->can('meta'); - # otherwise we might be - # dealing with a non-Moose - # class, and need to make - # our own metaclass + # we might be dealing with a non-Moose class, + # and need to make our own metaclass. if there's + # already a metaclass, it will be returned return Moose::Meta::Class->initialize($class); } elsif (my $role = $self->_does_metadata) { - # our role will always have - # a meta method - return $role->meta; + return Class::MOP::class_of($role); } else { $self->throw_error("Cannot find delegate metaclass for attribute " . $self->name); @@ -704,21 +720,40 @@ sub _get_delegate_method_list { sub delegation_metaclass { 'Moose::Meta::Method::Delegation' } sub _make_delegation_method { - my ( $self, $handle_name, $method_to_call ) = @_; + my ( $self, $handle_name, $method_to_call, $is_inline ) = @_; - my $method_body; + my @curried_arguments; - $method_body = $method_to_call - if 'CODE' eq ref($method_to_call); + ($method_to_call, @curried_arguments) = @$method_to_call + if 'ARRAY' eq ref($method_to_call); return $self->delegation_metaclass->new( name => $handle_name, package_name => $self->associated_class->name, attribute => $self, delegate_to_method => $method_to_call, + curried_arguments => \@curried_arguments, + is_inline => $is_inline, ); } +sub _coerce_and_verify { + my $self = shift; + my $val = shift; + my $instance = shift; + + return $val unless $self->has_type_constraint; + + my $type_constraint = $self->type_constraint; + if ($self->should_coerce && $type_constraint->has_coercion) { + $val = $type_constraint->coerce($val); + } + + $self->verify_against_type_constraint($val, instance => $instance); + + return $val; +} + sub verify_against_type_constraint { my $self = shift; my $val = shift; @@ -781,7 +816,7 @@ It adds the following options to the constructor: =over 8 -=item * is => 'ro' or 'rw' +=item * is => 'ro', 'rw', 'bare' This provides a shorthand for specifying the C, C, or C names. If the attribute is read-only ('ro') then it will @@ -792,6 +827,11 @@ with the same name. If you provide an explicit C for a read-write attribute, then you will have a C with the same name as the attribute, and a C with the name you provided. +Use 'bare' when you are deliberately not installing any methods +(accessor, reader, etc.) associated with this attribute; otherwise, +Moose will issue a deprecation warning when this attribute is added to a +metaclass. + =item * isa => $type This option accepts a type. The type can be a string, which should be @@ -930,6 +970,11 @@ for an example. This method overrides the parent to also install delegation methods. +If, after installing all methods, the attribute object has no associated +methods, it throws an error unless C<< is => 'bare' >> was passed to the +attribute constructor. (Trying to add an attribute that has no associated +methods is almost always an error.) + =item B<< $attr->remove_accessors >> This method overrides the parent to also remove delegation methods. @@ -1084,9 +1129,7 @@ Returns true if this attribute has any traits applied. =head1 BUGS -All complex software has bugs lurking in it, and this module is no -exception. If you find a bug please either email me, or add the bug -to cpan-RT. +See L for details on reporting bugs. =head1 AUTHOR @@ -1096,7 +1139,7 @@ Yuval Kogman Enothingmuch@woobling.comE =head1 COPYRIGHT AND LICENSE -Copyright 2006-2009 by Infinity Interactive, Inc. +Copyright 2006-2010 by Infinity Interactive, Inc. L