1 package Class::MOP::Mixin::HasAttributes;
7 $VERSION = eval $VERSION;
8 our $AUTHORITY = 'cpan:STEVAN';
11 use Scalar::Util 'blessed';
13 use base 'Class::MOP::Mixin';
15 sub _attribute_map { $_[0]->{'attributes'} }
16 sub attribute_metaclass { $_[0]->{'attribute_metaclass'} }
22 = blessed( $_[0] ) ? $_[0] : $self->attribute_metaclass->new(@_);
24 ( $attribute->isa('Class::MOP::Mixin::AttributeCore') )
26 "Your attribute must be an instance of Class::MOP::Mixin::AttributeCore (or a subclass)";
28 $self->_attach_attribute($attribute);
30 my $attr_name = $attribute->name;
32 $self->remove_attribute($attr_name)
33 if $self->has_attribute($attr_name);
35 my $order = ( scalar keys %{ $self->_attribute_map } );
36 $attribute->_set_insertion_order($order);
38 $self->_attribute_map->{$attr_name} = $attribute;
40 # This method is called to allow for installing accessors. Ideally, we'd
41 # use method overriding, but then the subclass would be responsible for
42 # making the attribute, which would end up with lots of code
43 # duplication. Even more ideally, we'd use augment/inner, but this is
45 $self->_post_add_attribute($attribute)
46 if $self->can('_post_add_attribute');
52 my ( $self, $attribute_name ) = @_;
54 ( defined $attribute_name )
55 || confess "You must define an attribute name";
57 exists $self->_attribute_map->{$attribute_name};
61 my ( $self, $attribute_name ) = @_;
63 ( defined $attribute_name )
64 || confess "You must define an attribute name";
66 return $self->_attribute_map->{$attribute_name};
69 sub remove_attribute {
70 my ( $self, $attribute_name ) = @_;
72 ( defined $attribute_name )
73 || confess "You must define an attribute name";
75 my $removed_attribute = $self->_attribute_map->{$attribute_name};
76 return unless defined $removed_attribute;
78 delete $self->_attribute_map->{$attribute_name};
80 return $removed_attribute;
83 sub get_attribute_list {
85 keys %{ $self->_attribute_map };
88 sub _restore_metaattributes_from {
92 for my $attr (sort { $a->insertion_order <=> $b->insertion_order }
93 map { $old_meta->get_attribute($_) }
94 $old_meta->get_attribute_list) {
95 $attr->_make_compatible_with($self->attribute_metaclass);
96 $self->add_attribute($attr);
108 Class::MOP::Mixin::HasMethods - Methods for metaclasses which have attributes
112 This class implements methods for metaclasses which have attributes
113 (L<Class::MOP::Class> and L<Moose::Meta::Role>). See L<Class::MOP::Class> for
118 Dave Rolsky E<lt>autarch@urth.orgE<gt>
120 =head1 COPYRIGHT AND LICENSE
122 Copyright 2006-2010 by Infinity Interactive, Inc.
124 L<http://www.iinteractive.com>
126 This library is free software; you can redistribute it and/or modify
127 it under the same terms as Perl itself.