1 package Class::MOP::Mixin::HasAttributes;
7 use Scalar::Util 'blessed';
9 use base 'Class::MOP::Object';
11 sub _attribute_map { $_[0]->{'attributes'} }
12 sub attribute_metaclass { $_[0]->{'attribute_metaclass'} }
18 = blessed( $_[0] ) ? $_[0] : $self->attribute_metaclass->new(@_);
20 ( $attribute->isa('Class::MOP::Attribute') )
22 "Your attribute must be an instance of Class::MOP::Attribute (or a subclass)";
24 $self->_attach_attribute($attribute);
26 my $attr_name = $attribute->name;
28 $self->remove_attribute($attr_name)
29 if $self->has_attribute($attr_name);
31 my $order = ( scalar keys %{ $self->_attribute_map } );
32 $attribute->_set_insertion_order($order);
34 $self->_attribute_map->{$attr_name} = $attribute;
36 # This method is called to allow for installing accessors. Ideally, we'd
37 # use method overriding, but then the subclass would be responsible for
38 # making the attribute, which would end up with lots of code
39 # duplication. Even more ideally, we'd use augment/inner, but this is
41 $self->_post_add_attribute($attribute)
42 if $self->can('_post_add_attribute');
48 my ( $self, $attribute_name ) = @_;
50 ( defined $attribute_name )
51 || confess "You must define an attribute name";
53 exists $self->_attribute_map->{$attribute_name};
57 my ( $self, $attribute_name ) = @_;
59 ( defined $attribute_name )
60 || confess "You must define an attribute name";
62 return $self->_attribute_map->{$attribute_name};
65 sub remove_attribute {
66 my ( $self, $attribute_name ) = @_;
68 ( defined $attribute_name )
69 || confess "You must define an attribute name";
71 my $removed_attribute = $self->_attribute_map->{$attribute_name};
72 return unless defined $removed_attribute;
74 delete $self->_attribute_map->{$attribute_name};
76 return $removed_attribute;
79 sub get_attribute_list {
81 keys %{ $self->_attribute_map };