X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMooseX%2FClassAttribute%2FRole%2FMeta%2FMixin%2FHasClassAttributes.pm;h=1b7eb493db9c9288328e2facb2da3d17cb2b16be;hb=ad109c62903c1bf4d05c93b57c059a1392e1d8b5;hp=82144d828221114854f38e3b67e8a1054a2166c8;hpb=aa6390299985f34313f45864361226b8e72ec272;p=gitmo%2FMooseX-ClassAttribute.git diff --git a/lib/MooseX/ClassAttribute/Role/Meta/Mixin/HasClassAttributes.pm b/lib/MooseX/ClassAttribute/Role/Meta/Mixin/HasClassAttributes.pm index 82144d8..1b7eb49 100644 --- a/lib/MooseX/ClassAttribute/Role/Meta/Mixin/HasClassAttributes.pm +++ b/lib/MooseX/ClassAttribute/Role/Meta/Mixin/HasClassAttributes.pm @@ -21,8 +21,55 @@ has _class_attribute_map => ( init_arg => undef, ); +# deprecated sub get_class_attribute_map { return $_[0]->_class_attribute_map(); } +sub add_class_attribute { + my $self = shift; + my $attribute = shift; + + ( $attribute->isa('Class::MOP::Mixin::AttributeCore') ) + || confess + "Your attribute must be an instance of Class::MOP::Mixin::AttributeCore (or a subclass)"; + + $self->_attach_class_attribute($attribute); + + my $attr_name = $attribute->name; + + $self->remove_class_attribute($attr_name) + if $self->has_class_attribute($attr_name); + + my $order = ( scalar keys %{ $self->_attribute_map } ); + $attribute->_set_insertion_order($order); + + $self->_add_class_attribute( $attr_name => $attribute ); + + # This method is called to allow for installing accessors. Ideally, we'd + # use method overriding, but then the subclass would be responsible for + # making the attribute, which would end up with lots of code + # duplication. Even more ideally, we'd use augment/inner, but this is + # Class::MOP! + $self->_post_add_class_attribute($attribute) + if $self->can('_post_add_class_attribute'); + + return $attribute; +} + +sub remove_class_attribute { + my $self = shift; + my $name = shift; + + ( defined $name && $name ) + || confess 'You must provide an attribute name'; + + my $removed_attr = $self->get_class_attribute($name); + return unless $removed_attr; + + $self->_remove_class_attribute($name); + + return $removed_attr; +} + 1;