1 package Class::MOP::Mixin::HasAttributes;
7 use Scalar::Util 'blessed';
9 use base 'Class::MOP::Mixin';
15 = blessed( $_[0] ) ? $_[0] : $self->attribute_metaclass->new(@_);
17 ( $attribute->isa('Class::MOP::Mixin::AttributeCore') )
19 "Your attribute must be an instance of Class::MOP::Mixin::AttributeCore (or a subclass)";
21 $self->_attach_attribute($attribute);
23 my $attr_name = $attribute->name;
25 $self->remove_attribute($attr_name)
26 if $self->has_attribute($attr_name);
28 my $order = ( scalar keys %{ $self->_attribute_map } );
29 $attribute->_set_insertion_order($order);
31 $self->_attribute_map->{$attr_name} = $attribute;
33 # This method is called to allow for installing accessors. Ideally, we'd
34 # use method overriding, but then the subclass would be responsible for
35 # making the attribute, which would end up with lots of code
36 # duplication. Even more ideally, we'd use augment/inner, but this is
38 $self->_post_add_attribute($attribute)
39 if $self->can('_post_add_attribute');
45 my ( $self, $attribute_name ) = @_;
47 ( defined $attribute_name )
48 || confess "You must define an attribute name";
50 exists $self->_attribute_map->{$attribute_name};
54 my ( $self, $attribute_name ) = @_;
56 ( defined $attribute_name )
57 || confess "You must define an attribute name";
59 return $self->_attribute_map->{$attribute_name};
62 sub remove_attribute {
63 my ( $self, $attribute_name ) = @_;
65 ( defined $attribute_name )
66 || confess "You must define an attribute name";
68 my $removed_attribute = $self->_attribute_map->{$attribute_name};
69 return unless defined $removed_attribute;
71 delete $self->_attribute_map->{$attribute_name};
73 return $removed_attribute;
76 sub get_attribute_list {
78 keys %{ $self->_attribute_map };
81 sub _restore_metaattributes_from {
85 for my $attr (sort { $a->insertion_order <=> $b->insertion_order }
86 map { $old_meta->get_attribute($_) }
87 $old_meta->get_attribute_list) {
88 $attr->_make_compatible_with($self->attribute_metaclass);
89 $self->add_attribute($attr);
95 # ABSTRACT: Methods for metaclasses which have attributes
103 This class implements methods for metaclasses which have attributes
104 (L<Class::MOP::Class> and L<Moose::Meta::Role>). See L<Class::MOP::Class> for