1 package Class::MOP::Mixin::HasAttributes;
6 our $AUTHORITY = 'cpan:STEVAN';
9 use Scalar::Util 'blessed';
11 use base 'Class::MOP::Mixin';
17 = blessed( $_[0] ) ? $_[0] : $self->attribute_metaclass->new(@_);
19 ( $attribute->isa('Class::MOP::Mixin::AttributeCore') )
21 "Your attribute must be an instance of Class::MOP::Mixin::AttributeCore (or a subclass)";
23 $self->_attach_attribute($attribute);
25 my $attr_name = $attribute->name;
27 $self->remove_attribute($attr_name)
28 if $self->has_attribute($attr_name);
30 my $order = ( scalar keys %{ $self->_attribute_map } );
31 $attribute->_set_insertion_order($order);
33 $self->_attribute_map->{$attr_name} = $attribute;
35 # This method is called to allow for installing accessors. Ideally, we'd
36 # use method overriding, but then the subclass would be responsible for
37 # making the attribute, which would end up with lots of code
38 # duplication. Even more ideally, we'd use augment/inner, but this is
40 $self->_post_add_attribute($attribute)
41 if $self->can('_post_add_attribute');
47 my ( $self, $attribute_name ) = @_;
49 ( defined $attribute_name )
50 || confess "You must define an attribute name";
52 exists $self->_attribute_map->{$attribute_name};
56 my ( $self, $attribute_name ) = @_;
58 ( defined $attribute_name )
59 || confess "You must define an attribute name";
61 return $self->_attribute_map->{$attribute_name};
64 sub remove_attribute {
65 my ( $self, $attribute_name ) = @_;
67 ( defined $attribute_name )
68 || confess "You must define an attribute name";
70 my $removed_attribute = $self->_attribute_map->{$attribute_name};
71 return unless defined $removed_attribute;
73 delete $self->_attribute_map->{$attribute_name};
75 return $removed_attribute;
78 sub get_attribute_list {
80 keys %{ $self->_attribute_map };
83 sub _restore_metaattributes_from {
87 for my $attr (sort { $a->insertion_order <=> $b->insertion_order }
88 map { $old_meta->get_attribute($_) }
89 $old_meta->get_attribute_list) {
90 $attr->_make_compatible_with($self->attribute_metaclass);
91 $self->add_attribute($attr);
97 # ABSTRACT: Methods for metaclasses which have attributes
105 This class implements methods for metaclasses which have attributes
106 (L<Class::MOP::Class> and L<Moose::Meta::Role>). See L<Class::MOP::Class> for